Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source to analytics events. #2750

Merged
merged 6 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 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"
Comment on lines -40 to -41
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into analytics/constants.go


type Params struct {
sourceInstaller string
path string
Expand Down Expand Up @@ -125,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(AnalyticsFunnelCat, "start")
an = sync.New(anaConst.SrcStateInstaller, cfg, nil, out)
an.Event(anaConst.CatInstallerFunnel, "start")

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")
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", errs.JoinMessage(err))
logging.Debug("Installer input error: " + errs.JoinMessage(err))
} else {
an.EventWithLabel(AnalyticsCat, "error", errs.JoinMessage(err))
an.EventWithLabel(anaConst.CatInstaller, "error", errs.JoinMessage(err))
multilog.Critical("Installer error: " + errs.JoinMessage(err))
}

an.EventWithLabel(AnalyticsFunnelCat, "fail", 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(AnalyticsFunnelCat, "success")
an.Event(anaConst.CatInstallerFunnel, "success")
}
}

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")

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)

// 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")
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")
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")
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")
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")

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")

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", 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")

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", 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")

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", 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
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
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
10 changes: 6 additions & 4 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 @@ -159,10 +161,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 +190,7 @@ func (r *Resolver) AnalyticsEvent(_ context.Context, category, action string, _l
return nil
})

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

return &graph.AnalyticsEventResponse{Sent: true}, nil
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/state-svc/internal/rtwatcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rtwatcher
import (
"encoding/json"
"os"
"path/filepath"
"runtime/debug"
"strconv"
"time"
Expand All @@ -14,6 +15,7 @@ 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 All @@ -30,7 +32,7 @@ type Watcher struct {
}

type analytics interface {
Event(category string, action 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 +99,11 @@ 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)
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels too error prone to me. Could we instead add the source as an argument for the ReportRuntimeUsage resolver on the svc?

The only place executors call this is here:

_, err = resolver.ReportRuntimeUsage(context.Background(), pidNum, hb.ExecPath, dimsJSON)

Any other place would be state tool. I'd be fine defaulting to state tool if the source is empty.

}

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.

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
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
Loading