Skip to content

Commit

Permalink
Heartbeats should contain their source.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-as committed Sep 15, 2023
1 parent a6928da commit bfef23d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 30 deletions.
4 changes: 2 additions & 2 deletions cmd/state-svc/internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (r *Resolver) AnalyticsEvent(_ context.Context, category, action, source st
return &graph.AnalyticsEventResponse{Sent: true}, nil
}

func (r *Resolver) ReportRuntimeUsage(_ context.Context, pid int, exec string, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error) {
func (r *Resolver) ReportRuntimeUsage(_ context.Context, pid int, exec, source string, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error) {
defer func() { handlePanics(recover(), debug.Stack()) }()

logging.Debug("Runtime usage resolver: %d - %s", pid, exec)
Expand All @@ -204,7 +204,7 @@ func (r *Resolver) ReportRuntimeUsage(_ context.Context, pid int, exec string, d
return &graph.ReportRuntimeUsageResponse{Received: false}, errs.Wrap(err, "Could not unmarshal")
}

r.rtwatch.Watch(pid, exec, dims)
r.rtwatch.Watch(pid, exec, source, dims)

return &graph.ReportRuntimeUsageResponse{Received: true}, nil
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/state-svc/internal/rtwatcher/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

type entry struct {
PID int `json:"pid"`
Exec string `json:"exec"`
Dims *dimensions.Values `json:"dims"`
PID int `json:"pid"`
Exec string `json:"exec"`
Source string `json:"source"`
Dims *dimensions.Values `json:"dims"`
}

func (e entry) IsRunning() (bool, error) {
Expand Down
12 changes: 3 additions & 9 deletions cmd/state-svc/internal/rtwatcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rtwatcher
import (
"encoding/json"
"os"
"path/filepath"
"runtime/debug"
"strconv"
"time"
Expand All @@ -15,7 +14,6 @@ import (
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/runbits/panics"
)
Expand Down Expand Up @@ -99,11 +97,7 @@ func (w *Watcher) check() {

func (w *Watcher) RecordUsage(e entry) {
logging.Debug("Recording usage of %s (%d)", e.Exec, e.PID)
source := anaConst.SrcExecutor
if filepath.Base(e.Exec) == constants.StateCmd+osutils.ExeExt {
source = anaConst.SrcStateTool
}
w.an.EventWithSource(anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, source, e.Dims)
w.an.EventWithSource(anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, e.Source, e.Dims)
}

func (w *Watcher) Close() error {
Expand All @@ -122,10 +116,10 @@ func (w *Watcher) Close() error {
return nil
}

func (w *Watcher) Watch(pid int, exec string, dims *dimensions.Values) {
func (w *Watcher) Watch(pid int, exec, source string, dims *dimensions.Values) {
logging.Debug("Watching %s (%d)", exec, pid)
dims.Sequence = ptr.To(-1) // sequence is meaningless for heartbeat events
e := entry{pid, exec, dims}
e := entry{pid, exec, source, dims}
w.watching = append(w.watching, e)
go w.RecordUsage(e) // initial event
}
23 changes: 16 additions & 7 deletions cmd/state-svc/internal/server/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/state-svc/schema/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Query {
availableUpdate: AvailableUpdate
projects: [Project]!
analyticsEvent(category: String!, action: String!, source: String!, label: String, dimensionsJson: String!): AnalyticsEventResponse
reportRuntimeUsage(pid: Int!, exec: String!, dimensionsJson: String!): ReportRuntimeUsageResponse
reportRuntimeUsage(pid: Int!, exec: String!, source: String!, dimensionsJson: String!): ReportRuntimeUsageResponse
checkRuntimeUsage(organizationName: String!): CheckRuntimeUsageResponse
checkMessages(command: String!, flags: [String!]!): [MessageInfo!]!
configChanged(key: String!): ConfigChangedResponse
Expand Down
4 changes: 2 additions & 2 deletions internal/svcctl/comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *Comm) GetLogFileName(ctx context.Context) (string, error) {
}

type Resolver interface {
ReportRuntimeUsage(ctx context.Context, pid int, exec, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error)
ReportRuntimeUsage(ctx context.Context, pid int, exec, source string, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error)
CheckRuntimeUsage(ctx context.Context, organizationName string) (*graph.CheckRuntimeUsageResponse, error)
}

Expand Down Expand Up @@ -139,7 +139,7 @@ func HeartbeatHandler(cfg *config.Instance, resolver Resolver, analyticsReporter

logging.Debug("Firing runtime usage events for %s", metaData.Namespace)
analyticsReporter.EventWithSource(constants.CatRuntimeUsage, constants.ActRuntimeAttempt, constants.SrcExecutor, dims)
_, err = resolver.ReportRuntimeUsage(context.Background(), pidNum, hb.ExecPath, dimsJSON)
_, err = resolver.ReportRuntimeUsage(context.Background(), pidNum, hb.ExecPath, constants.SrcExecutor, dimsJSON)
if err != nil {
multilog.Critical("Heartbeat Failure: Failed to report runtime usage in heartbeat handler: %s", errs.JoinMessage(err))
return
Expand Down
9 changes: 6 additions & 3 deletions pkg/platform/api/svc/request/reportrtusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package request
type ReportRuntimeUsage struct {
pid int
exec string
source string
dimensionsJson string
}

func NewReportRuntimeUsage(pid int, exec string, dimensionsJson string) *ReportRuntimeUsage {
func NewReportRuntimeUsage(pid int, exec, source string, dimensionsJson string) *ReportRuntimeUsage {
return &ReportRuntimeUsage{
pid: pid,
exec: exec,
source: source,
dimensionsJson: dimensionsJson,
}
}

func (e *ReportRuntimeUsage) Query() string {
return `query($pid: Int!, $exec: String!, $dimensionsJson: String!) {
reportRuntimeUsage(pid: $pid, exec: $exec, dimensionsJson: $dimensionsJson) {
return `query($pid: Int!, $exec: String!, $source: String!, $dimensionsJson: String!) {
reportRuntimeUsage(pid: $pid, exec: $exec, source: $source, dimensionsJson: $dimensionsJson) {
received
}
}`
Expand All @@ -26,6 +28,7 @@ func (e *ReportRuntimeUsage) Vars() map[string]interface{} {
return map[string]interface{}{
"pid": e.pid,
"exec": e.exec,
"source": e.source,
"dimensionsJson": e.dimensionsJson,
}
}
4 changes: 2 additions & 2 deletions pkg/platform/model/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ func (m *SvcModel) AnalyticsEvent(ctx context.Context, category, action, source,
return nil
}

func (m *SvcModel) ReportRuntimeUsage(ctx context.Context, pid int, exec string, dimJson string) error {
func (m *SvcModel) ReportRuntimeUsage(ctx context.Context, pid int, exec, source string, dimJson string) error {
defer profile.Measure("svc:ReportRuntimeUsage", time.Now())

r := request.NewReportRuntimeUsage(pid, exec, dimJson)
r := request.NewReportRuntimeUsage(pid, exec, source, dimJson)
u := graph.ReportRuntimeUsageResponse{}
if err := m.request(ctx, r, &u); err != nil {
return errs.Wrap(err, "Error sending report runtime usage event via state-svc")
Expand Down
2 changes: 1 addition & 1 deletion pkg/platform/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (r *Runtime) recordUsage() {
multilog.Critical("Could not marshal dimensions for runtime-usage: %s", errs.JoinMessage(err))
}
if r.svcm != nil {
r.svcm.ReportRuntimeUsage(context.Background(), os.Getpid(), osutils.Executable(), dimsJson)
r.svcm.ReportRuntimeUsage(context.Background(), os.Getpid(), osutils.Executable(), anaConsts.SrcStateTool, dimsJson)
}
}

Expand Down

0 comments on commit bfef23d

Please sign in to comment.