Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/pkg/utils/trackEvent.go
2500 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package utils
6
7
import (
8
"context"
9
"encoding/json"
10
11
"github.com/gitpod-io/gitpod/common-go/analytics"
12
"golang.org/x/xerrors"
13
)
14
15
const (
16
Outcome_Success = "success"
17
Outcome_UserErr = "user_error"
18
Outcome_SystemErr = "system_error"
19
)
20
21
const (
22
// System
23
SystemErrorCode = "system_error"
24
UserErrorCode = "user_error"
25
26
// Rebuild
27
RebuildErrorCode_ImageBuildFailed = "rebuild_image_build_failed"
28
RebuildErrorCode_DockerErr = "rebuild_docker_err"
29
RebuildErrorCode_DockerNotFound = "rebuild_docker_not_found"
30
RebuildErrorCode_DockerRunFailed = "rebuild_docker_run_failed"
31
RebuildErrorCode_MalformedGitpodYaml = "rebuild_malformed_gitpod_yaml"
32
RebuildErrorCode_MissingGitpodYaml = "rebuild_missing_gitpod_yaml"
33
RebuildErrorCode_NoCustomImage = "rebuild_no_custom_image"
34
RebuildErrorCode_AlreadyInDebug = "rebuild_already_in_debug"
35
RebuildErrorCode_InvaligLogLevel = "rebuild_invalid_log_level"
36
37
// UserError
38
UserErrorCode_NeedUpgradePlan = "plan_upgrade_required"
39
UserErrorCode_InvalidArguments = "invalid_arg"
40
UserErrorCode_AlreadyAttached = "already_attached"
41
)
42
43
type TrackCommandUsageParams struct {
44
Command []string `json:"command,omitempty"`
45
Args int64 `json:"args,omitempty"`
46
Flags []string `json:"flags,omitempty"`
47
Duration int64 `json:"duration,omitempty"`
48
ErrorCode string `json:"errorCode,omitempty"`
49
WorkspaceId string `json:"workspaceId,omitempty"`
50
InstanceId string `json:"instanceId,omitempty"`
51
Timestamp int64 `json:"timestamp,omitempty"`
52
ImageBuildDuration int64 `json:"imageBuildDuration,omitempty"`
53
Outcome string `json:"outcome,omitempty"`
54
}
55
56
func (e *TrackCommandUsageParams) ExportToJson() (string, error) {
57
data, err := json.Marshal(e)
58
if err != nil {
59
return "", err
60
}
61
return string(data), nil
62
}
63
64
type analyticsEvent struct {
65
Data *TrackCommandUsageParams
66
userId string
67
w analytics.Writer
68
}
69
70
func NewAnalyticsEvent(userId string) *analyticsEvent {
71
return &analyticsEvent{
72
w: analytics.NewFromEnvironment(),
73
userId: userId,
74
}
75
}
76
77
func (e *analyticsEvent) Send(ctx context.Context) error {
78
defer e.w.Close()
79
80
data := make(map[string]interface{})
81
jsonData, err := json.Marshal(e.Data)
82
if err != nil {
83
return xerrors.Errorf("Could not marshal event data: %w", err)
84
}
85
err = json.Unmarshal(jsonData, &data)
86
if err != nil {
87
return xerrors.Errorf("Could not unmarshal event data: %w", err)
88
}
89
90
e.w.Track(analytics.TrackMessage{
91
Identity: analytics.Identity{UserID: e.userId},
92
Event: "gp_command",
93
Properties: data,
94
})
95
return nil
96
}
97
98