Skip to content

Commit c23146f

Browse files
authored
Add telemetry to pull command (#2394)
* Add telemetry to pull command * Fix lint
1 parent 243d297 commit c23146f

File tree

3 files changed

+67
-11
lines changed

3 files changed

+67
-11
lines changed

pkg/cli/pull.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/spf13/cobra"
1313

1414
"github.com/replicate/cog/pkg/api"
15+
"github.com/replicate/cog/pkg/coglog"
1516
"github.com/replicate/cog/pkg/docker"
1617
"github.com/replicate/cog/pkg/http"
1718
"github.com/replicate/cog/pkg/util/console"
@@ -117,9 +118,24 @@ func imageToDir(image string, projectDir string) string {
117118
func pull(cmd *cobra.Command, args []string) error {
118119
ctx := cmd.Context()
119120

121+
// Create the clients
122+
dockerClient, err := docker.NewClient(ctx)
123+
if err != nil {
124+
return err
125+
}
126+
127+
client, err := http.ProvideHTTPClient(ctx, dockerClient)
128+
if err != nil {
129+
return err
130+
}
131+
132+
logClient := coglog.NewClient(client)
133+
logCtx := logClient.StartPull()
134+
120135
// Find image name
121136
projectDir, err := os.Getwd()
122137
if err != nil {
138+
logClient.EndPull(ctx, err, logCtx)
123139
return err
124140
}
125141
image := args[0]
@@ -128,32 +144,27 @@ func pull(cmd *cobra.Command, args []string) error {
128144
projectDir = imageToDir(image, projectDir)
129145
err = os.MkdirAll(projectDir, 0o755)
130146
if err != nil {
131-
return err
132-
}
133-
134-
// Create the clients
135-
dockerClient, err := docker.NewClient(ctx)
136-
if err != nil {
147+
logClient.EndPull(ctx, err, logCtx)
137148
return err
138149
}
139150

140151
// Check if we are in a pipeline
141152
if !pushPipeline {
142-
return errors.New("Please use docker pull " + image + " to download this model.")
143-
}
144-
145-
client, err := http.ProvideHTTPClient(ctx, dockerClient)
146-
if err != nil {
153+
err = errors.New("Please use docker pull " + image + " to download this model.")
154+
logClient.EndPull(ctx, err, logCtx)
147155
return err
148156
}
157+
149158
webClient := web.NewClient(dockerClient, client)
150159
apiClient := api.NewClient(dockerClient, client, webClient)
151160

152161
// Pull the source
153162
err = apiClient.PullSource(ctx, image, extractTarFile(projectDir))
154163
if err != nil {
164+
logClient.EndPull(ctx, err, logCtx)
155165
return err
156166
}
157167

168+
logClient.EndPull(ctx, nil, logCtx)
158169
return nil
159170
}

pkg/coglog/client.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ type migrateLog struct {
4343
PythonTrainStatus string `json:"python_train_status"`
4444
}
4545

46+
type pullLog struct {
47+
DurationMs float32 `json:"length_ms"`
48+
BuildError *string `json:"error"`
49+
}
50+
4651
func NewClient(client *http.Client) *Client {
4752
return &Client{
4853
client: client,
@@ -159,6 +164,39 @@ func (c *Client) EndMigrate(ctx context.Context, err error, logContext *MigrateL
159164
return true
160165
}
161166

167+
func (c *Client) StartPull() PullLogContext {
168+
logContext := PullLogContext{
169+
started: time.Now(),
170+
}
171+
return logContext
172+
}
173+
174+
func (c *Client) EndPull(ctx context.Context, err error, logContext PullLogContext) bool {
175+
var errorStr *string = nil
176+
if err != nil {
177+
errStr := err.Error()
178+
errorStr = &errStr
179+
}
180+
pushLog := pullLog{
181+
DurationMs: float32(time.Now().Sub(logContext.started).Milliseconds()),
182+
BuildError: errorStr,
183+
}
184+
185+
jsonData, err := json.Marshal(pushLog)
186+
if err != nil {
187+
console.Warn("Failed to marshal JSON for build log: " + err.Error())
188+
return false
189+
}
190+
191+
err = c.postLog(ctx, jsonData, "pull")
192+
if err != nil {
193+
console.Warn(err.Error())
194+
return false
195+
}
196+
197+
return true
198+
}
199+
162200
func (c *Client) postLog(ctx context.Context, jsonData []byte, action string) error {
163201
disabled, err := DisableFromEnvironment()
164202
if err != nil {

pkg/coglog/pull_log_context.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package coglog
2+
3+
import "time"
4+
5+
type PullLogContext struct {
6+
started time.Time
7+
}

0 commit comments

Comments
 (0)