Path: blob/main/components/gitpod-cli/pkg/utils/trackEvent.go
2500 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package utils56import (7"context"8"encoding/json"910"github.com/gitpod-io/gitpod/common-go/analytics"11"golang.org/x/xerrors"12)1314const (15Outcome_Success = "success"16Outcome_UserErr = "user_error"17Outcome_SystemErr = "system_error"18)1920const (21// System22SystemErrorCode = "system_error"23UserErrorCode = "user_error"2425// Rebuild26RebuildErrorCode_ImageBuildFailed = "rebuild_image_build_failed"27RebuildErrorCode_DockerErr = "rebuild_docker_err"28RebuildErrorCode_DockerNotFound = "rebuild_docker_not_found"29RebuildErrorCode_DockerRunFailed = "rebuild_docker_run_failed"30RebuildErrorCode_MalformedGitpodYaml = "rebuild_malformed_gitpod_yaml"31RebuildErrorCode_MissingGitpodYaml = "rebuild_missing_gitpod_yaml"32RebuildErrorCode_NoCustomImage = "rebuild_no_custom_image"33RebuildErrorCode_AlreadyInDebug = "rebuild_already_in_debug"34RebuildErrorCode_InvaligLogLevel = "rebuild_invalid_log_level"3536// UserError37UserErrorCode_NeedUpgradePlan = "plan_upgrade_required"38UserErrorCode_InvalidArguments = "invalid_arg"39UserErrorCode_AlreadyAttached = "already_attached"40)4142type TrackCommandUsageParams struct {43Command []string `json:"command,omitempty"`44Args int64 `json:"args,omitempty"`45Flags []string `json:"flags,omitempty"`46Duration int64 `json:"duration,omitempty"`47ErrorCode string `json:"errorCode,omitempty"`48WorkspaceId string `json:"workspaceId,omitempty"`49InstanceId string `json:"instanceId,omitempty"`50Timestamp int64 `json:"timestamp,omitempty"`51ImageBuildDuration int64 `json:"imageBuildDuration,omitempty"`52Outcome string `json:"outcome,omitempty"`53}5455func (e *TrackCommandUsageParams) ExportToJson() (string, error) {56data, err := json.Marshal(e)57if err != nil {58return "", err59}60return string(data), nil61}6263type analyticsEvent struct {64Data *TrackCommandUsageParams65userId string66w analytics.Writer67}6869func NewAnalyticsEvent(userId string) *analyticsEvent {70return &analyticsEvent{71w: analytics.NewFromEnvironment(),72userId: userId,73}74}7576func (e *analyticsEvent) Send(ctx context.Context) error {77defer e.w.Close()7879data := make(map[string]interface{})80jsonData, err := json.Marshal(e.Data)81if err != nil {82return xerrors.Errorf("Could not marshal event data: %w", err)83}84err = json.Unmarshal(jsonData, &data)85if err != nil {86return xerrors.Errorf("Could not unmarshal event data: %w", err)87}8889e.w.Track(analytics.TrackMessage{90Identity: analytics.Identity{UserID: e.userId},91Event: "gp_command",92Properties: data,93})94return nil95}969798