Skip to content

Commit 88e83b6

Browse files
author
Andrea Falzetti
committed
event tracker
1 parent 5b366ce commit 88e83b6

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

components/gitpod-cli/cmd/rebuild.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@ var buildCmd = &cobra.Command{
4848
return
4949
}
5050

51-
event := &utils.TrackCommandUsageParams{
51+
event := utils.TrackEvent(ctx, client, &utils.TrackCommandUsageParams{
5252
Command: cmd.Name(),
53-
}
53+
})
5454

5555
ctx = context.Background()
56+
5657
gitpodConfig, err := util.ParseGitpodConfig(wsInfo.CheckoutLocation)
5758
if err != nil {
5859
fmt.Println("The .gitpod.yml file cannot be parsed: please check the file and try again")
5960
fmt.Println("")
6061
fmt.Println("For help check out the reference page:")
6162
fmt.Println("https://www.gitpod.io/docs/references/gitpod-yml#gitpodyml")
62-
utils.TrackUsage()
63+
event.Set("ErrorCode", utils.RebuildErrorCode_MalformedGitpodYaml).Send(ctx)
6364
return
6465
}
6566

@@ -70,6 +71,7 @@ var buildCmd = &cobra.Command{
7071
fmt.Println("")
7172
fmt.Println("Alternatively, check out the following docs for getting started configuring your project")
7273
fmt.Println("https://www.gitpod.io/docs/configure#configure-gitpod")
74+
event.Set("ErrorCode", utils.RebuildErrorCode_MissingGitpodYaml).Send(ctx)
7375
return
7476
}
7577

@@ -84,6 +86,7 @@ var buildCmd = &cobra.Command{
8486

8587
if _, err := os.Stat(dockerfilePath); os.IsNotExist(err) {
8688
fmt.Println("Your .gitpod.yml points to a Dockerfile that doesn't exist: " + dockerfilePath)
89+
event.Set("ErrorCode", utils.RebuildErrorCode_DockerfileNotFound).Send(ctx)
8790
return
8891
}
8992
dockerfile, err := os.ReadFile(dockerfilePath)
@@ -98,15 +101,18 @@ var buildCmd = &cobra.Command{
98101
fmt.Println("https://www.gitpod.io/docs/configure/workspaces/workspace-image#use-a-custom-dockerfile")
99102
fmt.Println("")
100103
fmt.Println("Once you configure your Dockerfile, re-run this command to validate your changes")
104+
event.Set("ErrorCode", utils.RebuildErrorCode_DockerfileEmpty).Send(ctx)
101105
return
102106
}
103107
baseimage = "\n" + string(dockerfile) + "\n"
104108
default:
105109
fmt.Println("Check your .gitpod.yml and make sure the image property is configured correctly")
110+
event.Set("ErrorCode", utils.RebuildErrorCode_MalformedGitpodYaml).Send(ctx)
106111
return
107112
}
108113

109114
if baseimage == "" {
115+
// TODO: should this fallback to workspace full?
110116
fmt.Println("Your project is not using any custom Docker image.")
111117
fmt.Println("Check out the following docs, to know how to get started")
112118
fmt.Println("")
@@ -126,6 +132,7 @@ var buildCmd = &cobra.Command{
126132
dockerPath, err := exec.LookPath("docker")
127133
if err != nil {
128134
fmt.Println("Docker is not installed in your workspace")
135+
event.Set("ErrorCode", utils.RebuildErrorCode_DockerNotFound).Send(ctx)
129136
return
130137
}
131138

@@ -148,13 +155,13 @@ var buildCmd = &cobra.Command{
148155
// TODO: duration
149156
err = dockerCmd.Run()
150157
if _, ok := err.(*exec.ExitError); ok {
151-
// event.Duration = time.Since(startTime).Seconds()
152-
utils.TrackUsage(ctx, client, event)
153158
fmt.Println("Image Build Failed")
159+
event.Set("ErrorCode", utils.RebuildErrorCode_DockerBuildFailed).Send(ctx)
154160
log.Fatal(err)
155161
return
156162
} else if err != nil {
157163
fmt.Println("Docker error")
164+
event.Set("ErrorCode", utils.RebuildErrorCode_DockerErr).Send(ctx)
158165
log.Fatal(err)
159166
return
160167
}

components/gitpod-cli/pkg/utils/trackEvent.go

+74-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package utils
66

77
import (
88
"context"
9+
"fmt"
10+
"time"
911

1012
gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
1113
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
@@ -14,41 +16,100 @@ import (
1416
log "github.com/sirupsen/logrus"
1517
)
1618

19+
const (
20+
// Rebuild
21+
RebuildErrorCode_MalformedGitpodYaml = "rebuild_malformed_gitpod_yaml"
22+
RebuildErrorCode_MissingGitpodYaml = "rebuild_missing_gitpod_yaml"
23+
RebuildErrorCode_DockerfileNotFound = "rebuild_dockerfile_not_found"
24+
RebuildErrorCode_DockerfileEmpty = "rebuild_dockerfile_empty"
25+
RebuildErrorCode_DockerNotFound = "rebuild_docker_not_found"
26+
RebuildErrorCode_DockerBuildFailed = "rebuild_docker_build_failed"
27+
RebuildErrorCode_DockerErr = "rebuild_docker_err"
28+
)
29+
1730
type TrackCommandUsageParams struct {
1831
Command string `json:"command,omitempty"`
19-
DurationMs int64 `json:"duration,omitempty"`
32+
DurationMs int64 `json:"durationMs,omitempty"`
33+
Success bool `json:"success,omitempty"`
34+
ErrorCode string `json:"errorCode,omitempty"`
2035
WorkspaceId string `json:"workspaceId,omitempty"`
2136
WorkspaceInstanceId string `json:"workspaceInstanceId,omitempty"`
2237
Timestamp int64 `json:"timestamp,omitempty"`
2338
}
2439

25-
func TrackUsage(ctx context.Context, supervisorClient *supervisor.SupervisorClient, cmdParams *TrackCommandUsageParams) {
40+
type EventTracker struct {
41+
data *TrackCommandUsageParams
42+
startTime time.Time
43+
serverClient *serverapi.APIoverJSONRPC
44+
supervisorClient *supervisor.SupervisorClient
45+
}
46+
47+
func TrackEvent(ctx context.Context, supervisorClient *supervisor.SupervisorClient, cmdParams *TrackCommandUsageParams) *EventTracker {
48+
tracker := &EventTracker{
49+
startTime: time.Now(),
50+
supervisorClient: supervisorClient,
51+
}
52+
2653
wsInfo, err := supervisorClient.Info.WorkspaceInfo(ctx, &api.WorkspaceInfoRequest{})
2754
if err != nil {
2855
LogError(ctx, err, "Could not fetch the workspace info", supervisorClient)
29-
return
56+
return nil
3057
}
3158

32-
client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{"function:trackEvent"})
59+
serverClient, err := gitpod.ConnectToServer(ctx, wsInfo, []string{"function:trackEvent"})
3360
if err != nil {
3461
log.WithError(err).Fatal("error connecting to server")
62+
return nil
3563
}
36-
defer client.Close()
64+
defer serverClient.Close()
3765

38-
params := &TrackCommandUsageParams{
66+
tracker.serverClient = serverClient
67+
tracker.data = &TrackCommandUsageParams{
3968
Command: cmdParams.Command,
40-
DurationMs: cmdParams.DurationMs,
41-
WorkspaceId: "",
42-
WorkspaceInstanceId: "",
43-
Timestamp: 0,
69+
DurationMs: 0,
70+
WorkspaceId: wsInfo.WorkspaceId,
71+
WorkspaceInstanceId: wsInfo.InstanceId,
72+
ErrorCode: "",
73+
Timestamp: time.Now().UnixMilli(),
74+
}
75+
76+
return tracker
77+
}
78+
79+
func (t *EventTracker) Set(key string, value interface{}) *EventTracker {
80+
switch key {
81+
case "Command":
82+
t.data.Command = value.(string)
83+
case "Success":
84+
t.data.Success = value.(bool)
85+
case "ErrorCode":
86+
t.data.ErrorCode = value.(string)
87+
t.data.Success = false
88+
case "DurationMs":
89+
t.data.DurationMs = value.(int64)
90+
case "WorkspaceId":
91+
t.data.WorkspaceId = value.(string)
92+
case "WorkspaceInstanceId":
93+
t.data.WorkspaceInstanceId = value.(string)
94+
default:
95+
log.Warnf("Field not allowed: '%s'", key)
4496
}
97+
return t
98+
}
99+
100+
func (t *EventTracker) Send(ctx context.Context) {
101+
t.Set("DurationMs", time.Since(t.startTime).Milliseconds())
102+
45103
event := &serverapi.RemoteTrackMessage{
46104
Event: "gp_command",
47-
Properties: *params,
105+
Properties: t.data,
48106
}
49107

50-
err = client.TrackEvent(ctx, event)
108+
err := t.serverClient.TrackEvent(ctx, event)
51109
if err != nil {
52-
LogError(ctx, err, "Could not track gp command event", supervisorClient)
110+
LogError(ctx, err, "Could not track gp command event", t.supervisorClient)
111+
return
53112
}
113+
114+
fmt.Println("EVENT SENT TO SERVER")
54115
}

0 commit comments

Comments
 (0)