Skip to content

Commit

Permalink
Add source to analytics events.
Browse files Browse the repository at this point in the history
This allows for differentiation between state tool, offline installer, etc.
  • Loading branch information
mitchell-as committed Sep 12, 2023
1 parent 2fc4049 commit 778f37a
Show file tree
Hide file tree
Showing 43 changed files with 182 additions and 168 deletions.
42 changes: 20 additions & 22 deletions cmd/state-installer/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ActiveState/cli/internal/analytics"
"github.com/ActiveState/cli/internal/analytics/client/sync"
anaConst "github.com/ActiveState/cli/internal/analytics/constants"
"github.com/ActiveState/cli/internal/captain"
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
Expand All @@ -37,9 +38,6 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

const AnalyticsCat = "installer"
const AnalyticsFunnelCat = "installer-funnel"

type Params struct {
sourceInstaller string
path string
Expand Down Expand Up @@ -126,7 +124,7 @@ func main() {
logging.Debug("Processed Args: %v", processedArgs)

an = sync.New(cfg, nil, out)
an.Event(AnalyticsFunnelCat, "start")
an.Event(anaConst.CatInstallerFunnel, "start", anaConst.SrcStateTool)

params := newParams()
cmd := captain.NewCommand(
Expand Down Expand Up @@ -193,30 +191,30 @@ func main() {
},
)

an.Event(AnalyticsFunnelCat, "pre-exec")
an.Event(anaConst.CatInstallerFunnel, "pre-exec", anaConst.SrcStateTool)
err = cmd.Execute(processedArgs[1:])
if err != nil {
errors.ReportError(err, cmd, an)
if locale.IsInputError(err) {
an.EventWithLabel(AnalyticsCat, "input-error", errs.JoinMessage(err))
an.EventWithLabel(anaConst.CatInstaller, "input-error", anaConst.SrcStateTool, errs.JoinMessage(err))
logging.Debug("Installer input error: " + errs.JoinMessage(err))
} else {
an.EventWithLabel(AnalyticsCat, "error", errs.JoinMessage(err))
an.EventWithLabel(anaConst.CatInstaller, "error", anaConst.SrcStateTool, errs.JoinMessage(err))
multilog.Critical("Installer error: " + errs.JoinMessage(err))
}

an.EventWithLabel(AnalyticsFunnelCat, "fail", errs.JoinMessage(err))
an.EventWithLabel(anaConst.CatInstallerFunnel, "fail", anaConst.SrcStateTool, errs.JoinMessage(err))
exitCode, err = errors.ParseUserFacing(err)
if err != nil {
out.Error(err)
}
} else {
an.Event(AnalyticsFunnelCat, "success")
an.Event(anaConst.CatInstallerFunnel, "success", anaConst.SrcStateTool)
}
}

func execute(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher, args []string, params *Params) error {
an.Event(AnalyticsFunnelCat, "exec")
an.Event(anaConst.CatInstallerFunnel, "exec", anaConst.SrcStateTool)

if params.path == "" {
var err error
Expand Down Expand Up @@ -274,13 +272,13 @@ func execute(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher,
if params.isUpdate {
route = "update"
}
an.Event(AnalyticsFunnelCat, route)
an.Event(anaConst.CatInstallerFunnel, route, anaConst.SrcStateTool)

// Check if state tool already installed
if !params.isUpdate && !params.force && stateToolInstalled && !targetingSameBranch {
logging.Debug("Cancelling out because State Tool is already installed")
out.Print(fmt.Sprintf("State Tool Package Manager is already installed at [NOTICE]%s[/RESET]. To reinstall use the [ACTIONABLE]--force[/RESET] flag.", installPath))
an.Event(AnalyticsFunnelCat, "already-installed")
an.Event(anaConst.CatInstallerFunnel, "already-installed", anaConst.SrcStateTool)
params.isUpdate = true
return postInstallEvents(out, cfg, an, params)
}
Expand All @@ -295,7 +293,7 @@ func execute(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher,
// installOrUpdateFromLocalSource is invoked when we're performing an installation where the payload is already provided
func installOrUpdateFromLocalSource(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher, payloadPath string, params *Params) error {
logging.Debug("Install from local source")
an.Event(AnalyticsFunnelCat, "local-source")
an.Event(anaConst.CatInstallerFunnel, "local-source", anaConst.SrcStateTool)
if !params.isUpdate {
// install.sh or install.ps1 downloaded this installer and is running it.
out.Print(output.Title("Installing State Tool Package Manager"))
Expand Down Expand Up @@ -324,12 +322,12 @@ func installOrUpdateFromLocalSource(out output.Outputer, cfg *config.Instance, a
}

// Run installer
an.Event(AnalyticsFunnelCat, "pre-installer")
an.Event(anaConst.CatInstallerFunnel, "pre-installer", anaConst.SrcStateTool)
if err := installer.Install(); err != nil {
out.Print("[ERROR]x Failed[/RESET]")
return err
}
an.Event(AnalyticsFunnelCat, "post-installer")
an.Event(anaConst.CatInstallerFunnel, "post-installer", anaConst.SrcStateTool)
out.Print("[SUCCESS]✔ Done[/RESET]")

if !params.isUpdate {
Expand All @@ -342,7 +340,7 @@ func installOrUpdateFromLocalSource(out output.Outputer, cfg *config.Instance, a
}

func postInstallEvents(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher, params *Params) error {
an.Event(AnalyticsFunnelCat, "post-install-events")
an.Event(anaConst.CatInstallerFunnel, "post-install-events", anaConst.SrcStateTool)

installPath, err := resolveInstallPath(params.path)
if err != nil {
Expand All @@ -368,30 +366,30 @@ func postInstallEvents(out output.Outputer, cfg *config.Instance, an analytics.D
switch {
// Execute provided --command
case params.command != "":
an.Event(AnalyticsFunnelCat, "forward-command")
an.Event(anaConst.CatInstallerFunnel, "forward-command", anaConst.SrcStateTool)

out.Print(fmt.Sprintf("\nRunning `[ACTIONABLE]%s[/RESET]`\n", params.command))
cmd, args := exeutils.DecodeCmd(params.command)
if _, _, err := exeutils.ExecuteAndPipeStd(cmd, args, envSlice(binPath)); err != nil {
an.EventWithLabel(AnalyticsFunnelCat, "forward-command-err", err.Error())
an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-command-err", anaConst.SrcStateTool, err.Error())
return errs.Silence(errs.Wrap(err, "Running provided command failed, error returned: %s", errs.JoinMessage(err)))
}
// Activate provided --activate Namespace
case params.activate.IsValid():
an.Event(AnalyticsFunnelCat, "forward-activate")
an.Event(anaConst.CatInstallerFunnel, "forward-activate", anaConst.SrcStateTool)

out.Print(fmt.Sprintf("\nRunning `[ACTIONABLE]state activate %s[/RESET]`\n", params.activate.String()))
if _, _, err := exeutils.ExecuteAndPipeStd(stateExe, []string{"activate", params.activate.String()}, envSlice(binPath)); err != nil {
an.EventWithLabel(AnalyticsFunnelCat, "forward-activate-err", err.Error())
an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-activate-err", anaConst.SrcStateTool, err.Error())
return errs.Silence(errs.Wrap(err, "Could not activate %s, error returned: %s", params.activate.String(), errs.JoinMessage(err)))
}
// Activate provided --activate-default Namespace
case params.activateDefault.IsValid():
an.Event(AnalyticsFunnelCat, "forward-activate-default")
an.Event(anaConst.CatInstallerFunnel, "forward-activate-default", anaConst.SrcStateTool)

out.Print(fmt.Sprintf("\nRunning `[ACTIONABLE]state activate --default %s[/RESET]`\n", params.activateDefault.String()))
if _, _, err := exeutils.ExecuteAndPipeStd(stateExe, []string{"activate", params.activateDefault.String(), "--default"}, envSlice(binPath)); err != nil {
an.EventWithLabel(AnalyticsFunnelCat, "forward-activate-default-err", err.Error())
an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-activate-default-err", anaConst.SrcStateTool, err.Error())
return errs.Silence(errs.Wrap(err, "Could not activate %s, error returned: %s", params.activateDefault.String(), errs.JoinMessage(err)))
}
case !params.isUpdate && terminal.IsTerminal(int(os.Stdin.Fd())) && os.Getenv(constants.InstallerNoSubshell) != "true" && os.Getenv("TERM") != "dumb":
Expand Down
8 changes: 4 additions & 4 deletions cmd/state-offline-installer/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func (r *runner) Run(params *Params) (rerr error) {
return
}
if locale.IsInputError(rerr) {
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerAbort, errs.JoinMessage(rerr), installerDimensions)
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerAbort, ac.SrcOfflineInstaller, errs.JoinMessage(rerr), installerDimensions)
} else {
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerFailure, errs.JoinMessage(rerr), installerDimensions)
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerFailure, ac.SrcOfflineInstaller, errs.JoinMessage(rerr), installerDimensions)
}
}()

Expand Down Expand Up @@ -122,7 +122,7 @@ func (r *runner) Run(params *Params) (rerr error) {
CommitID: &r.icfg.CommitID,
Trigger: ptr.To(target.TriggerOfflineInstaller.String()),
}
r.analytics.Event(ac.CatOfflineInstaller, "start", installerDimensions)
r.analytics.Event(ac.CatOfflineInstaller, "start", ac.SrcOfflineInstaller, installerDimensions)

// Detect target path
targetPath, err := r.getTargetPath(params.path)
Expand Down Expand Up @@ -248,7 +248,7 @@ func (r *runner) Run(params *Params) (rerr error) {
return errs.Wrap(err, "Could not configure environment")
}

r.analytics.Event(ac.CatOfflineInstaller, ac.ActOfflineInstallerSuccess, installerDimensions)
r.analytics.Event(ac.CatOfflineInstaller, ac.ActOfflineInstallerSuccess, ac.SrcOfflineInstaller, installerDimensions)

r.out.Print(fmt.Sprintf(`Installation complete.
Your language runtime has been installed in [ACTIONABLE]%s[/RESET].`, targetPath))
Expand Down
10 changes: 5 additions & 5 deletions cmd/state-offline-uninstaller/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func (r *runner) Run(params *Params) (rerr error) {
return
}
if locale.IsInputError(rerr) {
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerAbort, errs.JoinMessage(rerr), installerDimensions)
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerAbort, ac.SrcOfflineInstaller, errs.JoinMessage(rerr), installerDimensions)
} else {
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerFailure, errs.JoinMessage(rerr), installerDimensions)
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerFailure, ac.SrcOfflineInstaller, errs.JoinMessage(rerr), installerDimensions)
}
}()

Expand Down Expand Up @@ -116,7 +116,7 @@ func (r *runner) Run(params *Params) (rerr error) {
CommitID: &r.icfg.CommitID,
Trigger: ptr.To(target.TriggerOfflineUninstaller.String()),
}
r.analytics.Event(ac.CatOfflineInstaller, ac.ActOfflineInstallerStart, installerDimensions)
r.analytics.Event(ac.CatOfflineInstaller, ac.ActOfflineInstallerStart, ac.SrcOfflineInstaller, installerDimensions)

r.out.Print("Removing environment configuration")
err = r.removeEnvPaths(namespace)
Expand All @@ -130,8 +130,8 @@ func (r *runner) Run(params *Params) (rerr error) {
return errs.Wrap(err, "Error removing installation directory")
}

r.analytics.Event(ac.CatOfflineInstaller, ac.ActOfflineInstallerSuccess, installerDimensions)
r.analytics.Event(ac.CatRuntimeUsage, ac.ActRuntimeDelete, installerDimensions)
r.analytics.Event(ac.CatOfflineInstaller, ac.ActOfflineInstallerSuccess, ac.SrcOfflineInstaller, installerDimensions)
r.analytics.Event(ac.CatRuntimeUsage, ac.ActRuntimeDelete, ac.SrcOfflineInstaller, installerDimensions)

r.out.Print("Uninstall Complete")

Expand Down
12 changes: 6 additions & 6 deletions cmd/state-svc/internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (r *Resolver) Query() genserver.QueryResolver { return r }
func (r *Resolver) Version(ctx context.Context) (*graph.Version, error) {
defer func() { handlePanics(recover(), debug.Stack()) }()

r.an.EventWithLabel(anaConsts.CatStateSvc, "endpoint", "Version")
r.an.EventWithLabel(anaConsts.CatStateSvc, "endpoint", anaConsts.SrcStateTool, "Version")
logging.Debug("Version resolver")
return &graph.Version{
State: &graph.StateVersion{
Expand All @@ -118,7 +118,7 @@ func (r *Resolver) Version(ctx context.Context) (*graph.Version, error) {
func (r *Resolver) AvailableUpdate(ctx context.Context) (*graph.AvailableUpdate, error) {
defer func() { handlePanics(recover(), debug.Stack()) }()

r.an.EventWithLabel(anaConsts.CatStateSvc, "endpoint", "AvailableUpdate")
r.an.EventWithLabel(anaConsts.CatStateSvc, "endpoint", anaConsts.SrcStateTool, "AvailableUpdate")
logging.Debug("AvailableUpdate resolver")
defer logging.Debug("AvailableUpdate done")

Expand All @@ -142,7 +142,7 @@ func (r *Resolver) AvailableUpdate(ctx context.Context) (*graph.AvailableUpdate,
func (r *Resolver) Projects(ctx context.Context) ([]*graph.Project, error) {
defer func() { handlePanics(recover(), debug.Stack()) }()

r.an.EventWithLabel(anaConsts.CatStateSvc, "endpoint", "Projects")
r.an.EventWithLabel(anaConsts.CatStateSvc, "endpoint", anaConsts.SrcStateTool, "Projects")
logging.Debug("Projects resolver")
var projects []*graph.Project
localConfigProjects := projectfile.GetProjectMapping(r.cfg)
Expand All @@ -159,10 +159,10 @@ func (r *Resolver) Projects(ctx context.Context) ([]*graph.Project, error) {
return projects, nil
}

func (r *Resolver) AnalyticsEvent(_ context.Context, category, action string, _label *string, dimensionsJson string) (*graph.AnalyticsEventResponse, error) {
func (r *Resolver) AnalyticsEvent(_ context.Context, category, action, source string, _label *string, dimensionsJson string) (*graph.AnalyticsEventResponse, error) {
defer func() { handlePanics(recover(), debug.Stack()) }()

logging.Debug("Analytics event resolver: %s - %s", category, action)
logging.Debug("Analytics event resolver: %s - %s (%s)", category, action, source)

label := ""
if _label != nil {
Expand All @@ -188,7 +188,7 @@ func (r *Resolver) AnalyticsEvent(_ context.Context, category, action string, _l
return nil
})

r.anForClient.EventWithLabel(category, action, label, dims)
r.anForClient.EventWithLabel(category, action, source, label, dims)

return &graph.AnalyticsEventResponse{Sent: true}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/state-svc/internal/rtwatcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Watcher struct {
}

type analytics interface {
Event(category string, action string, dim ...*dimensions.Values)
Event(category, action, source string, dim ...*dimensions.Values)
}

func New(cfg *config.Instance, an analytics) *Watcher {
Expand Down Expand Up @@ -97,7 +97,7 @@ func (w *Watcher) check() {

func (w *Watcher) RecordUsage(e entry) {
logging.Debug("Recording usage of %s (%d)", e.Exec, e.PID)
w.an.Event(anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, e.Dims)
w.an.Event(anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, anaConst.SrcStateTool, e.Dims)
}

func (w *Watcher) Close() error {
Expand Down
31 changes: 20 additions & 11 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.

6 changes: 3 additions & 3 deletions cmd/state-svc/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ func (s *Server) Resolver() *resolver.Resolver {
}

func (s *Server) Start() error {
s.analytics.Event(constants.CatStateSvc, "start")
s.analytics.Event(constants.CatStateSvc, "start", constants.SrcStateTool)
err := s.httpServer.Start(s.listener.Addr().String())
if err != nil {
s.analytics.Event(constants.CatStateSvc, "start-failure")
s.analytics.Event(constants.CatStateSvc, "start-failure", constants.SrcStateTool)
}
return err
}

func (s *Server) Shutdown() error {
s.analytics.Event(constants.CatStateSvc, "shutdown")
s.analytics.Event(constants.CatStateSvc, "shutdown", constants.SrcStateTool)
logging.Debug("shutting down server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion cmd/state-svc/schema/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Query {
version: Version
availableUpdate: AvailableUpdate
projects: [Project]!
analyticsEvent(category: String!, action: String!, label: String, dimensionsJson: String!): AnalyticsEventResponse
analyticsEvent(category: String!, action: String!, source: String!, label: String, dimensionsJson: String!): AnalyticsEventResponse
reportRuntimeUsage(pid: Int!, exec: String!, dimensionsJson: String!): ReportRuntimeUsageResponse
checkRuntimeUsage(organizationName: String!): CheckRuntimeUsageResponse
checkMessages(command: String!, flags: [String!]!): [MessageInfo!]!
Expand Down
Loading

0 comments on commit 778f37a

Please sign in to comment.