Skip to content

Commit

Permalink
Move analytics source into clients.
Browse files Browse the repository at this point in the history
Also improve forwarding events on behalf of executors.
  • Loading branch information
mitchell-as committed Sep 13, 2023
1 parent 44abffa commit 1a6ad98
Show file tree
Hide file tree
Showing 38 changed files with 198 additions and 138 deletions.
40 changes: 20 additions & 20 deletions cmd/state-installer/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func main() {
logging.Debug("Original Args: %v", os.Args)
logging.Debug("Processed Args: %v", processedArgs)

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

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

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

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

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

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

// 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(anaConst.CatInstallerFunnel, "already-installed", anaConst.SrcStateTool)
an.Event(anaConst.CatInstallerFunnel, "already-installed")
params.isUpdate = true
return postInstallEvents(out, cfg, an, params)
}
Expand All @@ -293,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(anaConst.CatInstallerFunnel, "local-source", anaConst.SrcStateTool)
an.Event(anaConst.CatInstallerFunnel, "local-source")
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 @@ -322,12 +322,12 @@ func installOrUpdateFromLocalSource(out output.Outputer, cfg *config.Instance, a
}

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

if !params.isUpdate {
Expand All @@ -340,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(anaConst.CatInstallerFunnel, "post-install-events", anaConst.SrcStateTool)
an.Event(anaConst.CatInstallerFunnel, "post-install-events")

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

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(anaConst.CatInstallerFunnel, "forward-command-err", anaConst.SrcStateTool, err.Error())
an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-command-err", 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(anaConst.CatInstallerFunnel, "forward-activate", anaConst.SrcStateTool)
an.Event(anaConst.CatInstallerFunnel, "forward-activate")

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(anaConst.CatInstallerFunnel, "forward-activate-err", anaConst.SrcStateTool, err.Error())
an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-activate-err", 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(anaConst.CatInstallerFunnel, "forward-activate-default", anaConst.SrcStateTool)
an.Event(anaConst.CatInstallerFunnel, "forward-activate-default")

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(anaConst.CatInstallerFunnel, "forward-activate-default-err", anaConst.SrcStateTool, err.Error())
an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-activate-default-err", 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, ac.SrcOfflineInstaller, errs.JoinMessage(rerr), installerDimensions)
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerAbort, errs.JoinMessage(rerr), installerDimensions)
} else {
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerFailure, ac.SrcOfflineInstaller, errs.JoinMessage(rerr), installerDimensions)
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerFailure, 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", ac.SrcOfflineInstaller, installerDimensions)
r.analytics.Event(ac.CatOfflineInstaller, "start", 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, ac.SrcOfflineInstaller, installerDimensions)
r.analytics.Event(ac.CatOfflineInstaller, ac.ActOfflineInstallerSuccess, installerDimensions)

r.out.Print(fmt.Sprintf(`Installation complete.
Your language runtime has been installed in [ACTIONABLE]%s[/RESET].`, targetPath))
Expand Down
3 changes: 2 additions & 1 deletion cmd/state-offline-installer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 Down Expand Up @@ -78,7 +79,7 @@ func main() {
return
}

an = sync.New(cfg, nil, out)
an = sync.New(anaConst.SrcOfflineInstaller, cfg, nil, out)

prime := primer.New(
nil, out, nil,
Expand Down
3 changes: 2 additions & 1 deletion cmd/state-offline-uninstaller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 Down Expand Up @@ -78,7 +79,7 @@ func main() {
return
}

an = sync.New(cfg, nil, out)
an = sync.New(anaConst.SrcOfflineInstaller, cfg, nil, out)

prime := primer.New(
nil, out, nil,
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, ac.SrcOfflineInstaller, errs.JoinMessage(rerr), installerDimensions)
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerAbort, errs.JoinMessage(rerr), installerDimensions)
} else {
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerFailure, ac.SrcOfflineInstaller, errs.JoinMessage(rerr), installerDimensions)
r.analytics.EventWithLabel(ac.CatOfflineInstaller, ac.ActOfflineInstallerFailure, 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, ac.SrcOfflineInstaller, installerDimensions)
r.analytics.Event(ac.CatOfflineInstaller, ac.ActOfflineInstallerStart, 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, ac.SrcOfflineInstaller, installerDimensions)
r.analytics.Event(ac.CatRuntimeUsage, ac.ActRuntimeDelete, ac.SrcOfflineInstaller, installerDimensions)
r.analytics.Event(ac.CatOfflineInstaller, ac.ActOfflineInstallerSuccess, installerDimensions)
r.analytics.Event(ac.CatRuntimeUsage, ac.ActRuntimeDelete, installerDimensions)

r.out.Print("Uninstall Complete")

Expand Down
3 changes: 2 additions & 1 deletion cmd/state-remote-installer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 Down Expand Up @@ -94,7 +95,7 @@ func main() {
return
}

an = sync.New(cfg, nil, out)
an = sync.New(anaConst.SrcStateRemoteInstaller, cfg, nil, out)

// Set up prompter
prompter := prompt.New(true, an)
Expand Down
12 changes: 7 additions & 5 deletions cmd/state-svc/internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func New(cfg *config.Instance, an *sync.Client, auth *authentication.Auth) (*Res

usageChecker := rtusage.NewChecker(cfg, auth)

anForClient := sync.New(cfg, auth, nil)
// Note: source does not matter here, as analytics sent via the resolver have a source
// (e.g. State Tool or Executor), and that source will be used.
anForClient := sync.New(anaConsts.SrcStateTool, cfg, auth, nil)
return &Resolver{
cfg,
msg,
Expand Down Expand Up @@ -102,7 +104,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", anaConsts.SrcStateTool, "Version")
r.an.EventWithLabel(anaConsts.CatStateSvc, "endpoint", "Version")
logging.Debug("Version resolver")
return &graph.Version{
State: &graph.StateVersion{
Expand All @@ -118,7 +120,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", anaConsts.SrcStateTool, "AvailableUpdate")
r.an.EventWithLabel(anaConsts.CatStateSvc, "endpoint", "AvailableUpdate")
logging.Debug("AvailableUpdate resolver")
defer logging.Debug("AvailableUpdate done")

Expand All @@ -142,7 +144,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", anaConsts.SrcStateTool, "Projects")
r.an.EventWithLabel(anaConsts.CatStateSvc, "endpoint", "Projects")
logging.Debug("Projects resolver")
var projects []*graph.Project
localConfigProjects := projectfile.GetProjectMapping(r.cfg)
Expand Down Expand Up @@ -188,7 +190,7 @@ func (r *Resolver) AnalyticsEvent(_ context.Context, category, action, source st
return nil
})

r.anForClient.EventWithLabel(category, action, source, label, dims)
r.anForClient.EventWithSourceAndLabel(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, action, source string, dim ...*dimensions.Values)
EventWithSource(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, anaConst.SrcStateTool, e.Dims)
w.an.EventWithSource(anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, anaConst.SrcExecutor, e.Dims)
}

func (w *Watcher) Close() error {
Expand Down
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", constants.SrcStateTool)
s.analytics.Event(constants.CatStateSvc, "start")
err := s.httpServer.Start(s.listener.Addr().String())
if err != nil {
s.analytics.Event(constants.CatStateSvc, "start-failure", constants.SrcStateTool)
s.analytics.Event(constants.CatStateSvc, "start-failure")
}
return err
}

func (s *Server) Shutdown() error {
s.analytics.Event(constants.CatStateSvc, "shutdown", constants.SrcStateTool)
s.analytics.Event(constants.CatStateSvc, "shutdown")
logging.Debug("shutting down server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
Expand Down
3 changes: 2 additions & 1 deletion cmd/state-svc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ActiveState/cli/cmd/state-svc/autostart"
anaSync "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 Down Expand Up @@ -98,7 +99,7 @@ func run(cfg *config.Instance) error {
}

auth := authentication.New(cfg)
an := anaSync.New(cfg, auth, out)
an := anaSync.New(anaConst.SrcStateService, cfg, auth, out)
defer an.Wait()

if err := autostart.RegisterConfigListener(cfg); err != nil {
Expand Down
Loading

0 comments on commit 1a6ad98

Please sign in to comment.