Skip to content

Commit af28911

Browse files
committed
Send metrics on runner startup
1 parent fd03704 commit af28911

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

internal/server/runner.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ func NewRunner(ipcUrl, uploadUrl string) *Runner {
120120
}
121121

122122
func NewProcedureRunner(ipcUrl, uploadUrl, name, srcDir string) *Runner {
123-
// Use srcDir as name
124123
r := newRunner(name, ipcUrl, uploadUrl)
125124
r.cmd.Dir = srcDir
126125
return r
@@ -333,6 +332,12 @@ func (r *Runner) config() {
333332
predictorName = c
334333
// Default to 1 if not set in cog.yaml, regardless whether async predict or not
335334
r.maxConcurrency = max(1, y.Concurrency.Max)
335+
336+
// Send metrics for normal single instance runner
337+
// Do not send for multi-tenant procedure runners to reduce noise
338+
if r.name == DefaultRunner {
339+
go util.SendRunnerMetric(*y)
340+
}
336341
}
337342
conf := PredictConfig{
338343
ModuleName: moduleName,

internal/util/metrics.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package util
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
"os"
8+
)
9+
10+
type MetricsPayload struct {
11+
Source string `json:"source,omitempty"`
12+
Type string `json:"type,omitempty"`
13+
Data map[string]any `json:"data,omitempty"`
14+
}
15+
16+
const MetricsEndpointEnv = "COG_METRICS_ENDPOINT"
17+
18+
func SendRunnerMetric(yaml CogYaml) {
19+
log := logger.Sugar()
20+
endpoint := os.Getenv(MetricsEndpointEnv)
21+
if endpoint == "" {
22+
return
23+
}
24+
data := map[string]any{
25+
"gpu": yaml.Build.GPU,
26+
"fast": yaml.Build.Fast,
27+
"cog_runtime": yaml.Build.CogRuntime,
28+
"version": Version(),
29+
}
30+
payload := MetricsPayload{
31+
Source: "cog-runtime",
32+
Type: "runner",
33+
Data: data,
34+
}
35+
body, err := json.Marshal(payload)
36+
if err != nil {
37+
log.Errorw("failed to marshal payload", "error", err)
38+
return
39+
}
40+
resp, err := http.DefaultClient.Post(endpoint, "application/json", bytes.NewBuffer(body))
41+
if err != nil || resp.StatusCode != http.StatusOK {
42+
log.Errorw("failed to send runner metrics", "error", err)
43+
}
44+
}

internal/util/util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,28 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/replicate/go/logging"
14+
1315
"github.com/replicate/go/must"
1416
"github.com/replicate/go/uuid"
1517

1618
"gopkg.in/yaml.v3"
1719
)
1820

21+
var logger = logging.New("cog-util")
22+
23+
type Build struct {
24+
GPU bool `yaml:"gpu"`
25+
Fast bool `yaml:"fast"`
26+
CogRuntime bool `yaml:"cog_runtime"`
27+
}
28+
1929
type Concurrency struct {
2030
Max int `yaml:"max"`
2131
}
2232

2333
type CogYaml struct {
34+
Build Build `yaml:"build"`
2435
Concurrency Concurrency `yaml:"concurrency"`
2536
Predict string `yaml:"predict"`
2637
}

0 commit comments

Comments
 (0)