Skip to content

Commit 5b366ce

Browse files
author
Andrea Falzetti
committed
add trackEvent
1 parent e535fff commit 5b366ce

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

components/gitpod-cli/cmd/rebuild.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,18 @@ var buildCmd = &cobra.Command{
4848
return
4949
}
5050

51+
event := &utils.TrackCommandUsageParams{
52+
Command: cmd.Name(),
53+
}
54+
5155
ctx = context.Background()
5256
gitpodConfig, err := util.ParseGitpodConfig(wsInfo.CheckoutLocation)
5357
if err != nil {
5458
fmt.Println("The .gitpod.yml file cannot be parsed: please check the file and try again")
5559
fmt.Println("")
5660
fmt.Println("For help check out the reference page:")
5761
fmt.Println("https://www.gitpod.io/docs/references/gitpod-yml#gitpodyml")
62+
utils.TrackUsage()
5863
return
5964
}
6065

@@ -118,7 +123,13 @@ var buildCmd = &cobra.Command{
118123

119124
tag := "temp-build-" + time.Now().Format("20060102150405")
120125

121-
dockerCmd := exec.Command("docker", "build", "-t", tag, "--progress=tty", ".")
126+
dockerPath, err := exec.LookPath("docker")
127+
if err != nil {
128+
fmt.Println("Docker is not installed in your workspace")
129+
return
130+
}
131+
132+
dockerCmd := exec.Command(dockerPath, "build", "-t", tag, "--progress=tty", ".")
122133
dockerCmd.Dir = tmpDir
123134

124135
dockerCmd.Stdout = os.Stdout
@@ -131,9 +142,14 @@ var buildCmd = &cobra.Command{
131142
}
132143
}()
133144

145+
// TODO: add docker run
146+
// TODO: add message to suggest how to exit
147+
134148
// TODO: duration
135149
err = dockerCmd.Run()
136150
if _, ok := err.(*exec.ExitError); ok {
151+
// event.Duration = time.Since(startTime).Seconds()
152+
utils.TrackUsage(ctx, client, event)
137153
fmt.Println("Image Build Failed")
138154
log.Fatal(err)
139155
return

components/gitpod-cli/cmd/root.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"strings"
12+
"time"
1213

1314
"github.com/spf13/cobra"
1415
)
@@ -20,8 +21,12 @@ var rootCmd = &cobra.Command{
2021

2122
var noColor bool
2223

24+
var startTime time.Time
25+
2326
// Execute runs the root command
2427
func Execute() {
28+
startTime = time.Now()
29+
2530
entrypoint := strings.TrimPrefix(filepath.Base(os.Args[0]), "gp-")
2631
for _, c := range rootCmd.Commands() {
2732
if c.Name() == entrypoint {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
10+
gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
11+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
12+
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"
13+
"github.com/gitpod-io/gitpod/supervisor/api"
14+
log "github.com/sirupsen/logrus"
15+
)
16+
17+
type TrackCommandUsageParams struct {
18+
Command string `json:"command,omitempty"`
19+
DurationMs int64 `json:"duration,omitempty"`
20+
WorkspaceId string `json:"workspaceId,omitempty"`
21+
WorkspaceInstanceId string `json:"workspaceInstanceId,omitempty"`
22+
Timestamp int64 `json:"timestamp,omitempty"`
23+
}
24+
25+
func TrackUsage(ctx context.Context, supervisorClient *supervisor.SupervisorClient, cmdParams *TrackCommandUsageParams) {
26+
wsInfo, err := supervisorClient.Info.WorkspaceInfo(ctx, &api.WorkspaceInfoRequest{})
27+
if err != nil {
28+
LogError(ctx, err, "Could not fetch the workspace info", supervisorClient)
29+
return
30+
}
31+
32+
client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{"function:trackEvent"})
33+
if err != nil {
34+
log.WithError(err).Fatal("error connecting to server")
35+
}
36+
defer client.Close()
37+
38+
params := &TrackCommandUsageParams{
39+
Command: cmdParams.Command,
40+
DurationMs: cmdParams.DurationMs,
41+
WorkspaceId: "",
42+
WorkspaceInstanceId: "",
43+
Timestamp: 0,
44+
}
45+
event := &serverapi.RemoteTrackMessage{
46+
Event: "gp_command",
47+
Properties: *params,
48+
}
49+
50+
err = client.TrackEvent(ctx, event)
51+
if err != nil {
52+
LogError(ctx, err, "Could not track gp command event", supervisorClient)
53+
}
54+
}

0 commit comments

Comments
 (0)