Skip to content

Commit

Permalink
feat: better metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Braun <rainbowstack@gmail.com>
  • Loading branch information
bluebrown committed Jan 19, 2024
1 parent 6803eb9 commit 3f1a725
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,26 @@ Kobold exposes prometheus metrics on port 8080. The metrics are exposed in the
`$KOBOLD_ADDR_API/metrics` endpoint. The metrics are prefixed with `kobold_`.

```python
# HELP kobold_recv number of messages received
# TYPE kobold_recv counter
kobold_recv{channel="distribution",rejected="false"} 13
kobold_recv{channel="distribution",rejected="true"} 2
# HELP kobold_run run status (task groups)
# TYPE kobold_run gauge
kobold_run{repo="http://gitea/dev/test",status="running"} 2
kobold_run{repo="http://gitea/dev/test",status="success"} 5
kobold_run{repo="http://gitea/dev/test",status="failure"} 1
# HELP kobold_git_fetch number of git fetches
# TYPE kobold_git_fetch counter
kobold_git_fetch{repo="git@github.com:bluebrown/foobar"} 3
# HELP kobold_git_push number of git pushes
# TYPE kobold_git_push counter
kobold_git_push{repo="git@github.com:bluebrown/foobar"} 1
# HELP kobold_image_seen number of images seen
# TYPE kobold_image_seen counter
kobold_image_seen{ref="docker.io/bluebrown/busybox"} 3
kobold_image_seen{ref="docker.io/bluebrown/nginx"} 2
# HELP kobold_msg_recv number of messages received
# TYPE kobold_msg_recv counter
kobold_msg_recv{channel="dockerhub",rejected="false"} 5
# HELP kobold_run_active number of active runs
# TYPE kobold_run_active gauge
kobold_run_active 0
# HELP kobold_run_status run status (task groups)
# TYPE kobold_run_status counter
kobold_run_status{repo="git@github.com:bluebrown/foobar",status="success"} 2
kobold_run_status{repo="git@github.com:bluebrown/foobar",status="failure"} 1
```

## Web API
Expand Down
14 changes: 7 additions & 7 deletions krm/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
)

type Pipeline struct {
RepoURI string `json:"repoUri,omitempty"`
SrcBranch string `json:"sourceBranch,omitempty"`
DstBranch string `json:"destinationBranch,omitempty"`
CachePath string `json:"cachePath,omitempty"`
RepoURI kioutil.GitPackageURI `json:"repoUri,omitempty"`
SrcBranch string `json:"sourceBranch,omitempty"`
DstBranch string `json:"destinationBranch,omitempty"`
CachePath string `json:"cachePath,omitempty"`
PushCounter *prometheus.CounterVec
}

func (opts Pipeline) Run(ctx context.Context, imageRefs []string) (msg string, changes, warnings []string, err error) {
kf := NewImageRefUpdateFilter(nil, imageRefs...)

grw := kioutil.NewGitPackageReadWriter(ctx, opts.RepoURI, opts.DstBranch)
grw := kioutil.NewGitPackageReadWriter(ctx, opts.RepoURI.String(), opts.DstBranch)

grw.SetCachePath(opts.CachePath)

Expand All @@ -35,7 +35,7 @@ func (opts Pipeline) Run(ctx context.Context, imageRefs []string) (msg string, c
msg := "chore(kobold): update krm package"
var buf bytes.Buffer
if err := tpl.Execute(&buf, TemplateContext{
Repo: opts.RepoURI,
Repo: opts.RepoURI.String(),
Branch: opts.DstBranch,
Changes: kf.Changes,
Warnings: kf.Warnings,
Expand All @@ -57,7 +57,7 @@ func (opts Pipeline) Run(ctx context.Context, imageRefs []string) (msg string, c
}

if len(kf.Changes) > 0 && opts.PushCounter != nil {
opts.PushCounter.With(prometheus.Labels{"repo": opts.RepoURI}).Inc()
opts.PushCounter.With(prometheus.Labels{"repo": opts.RepoURI.Repo}).Inc()
}

return grw.CommitMessage(), kf.Changes, kf.Warnings, nil
Expand Down
4 changes: 2 additions & 2 deletions task/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func KoboldHandler(ctx context.Context, cache string, g store.TaskGroup, runner
msg string
)

// TODO: this should be emitted to the monitoring system
defer func(ts time.Time) {
slog.InfoContext(ctx,
"pipeline run completed",
"fingerprint", g.Fingerprint,
"repoUri", g.RepoUri.String(),
"changes", len(changes),
"warnings", len(warnings),
"pipelineErr", pipelineErr,
Expand All @@ -40,7 +40,7 @@ func KoboldHandler(ctx context.Context, cache string, g store.TaskGroup, runner
}

pipline := krm.Pipeline{
RepoURI: g.RepoUri.String(),
RepoURI: g.RepoUri,
SrcBranch: g.RepoUri.Ref,
DstBranch: g.DestBranch.String,
CachePath: cache,
Expand Down
4 changes: 4 additions & 0 deletions task/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ var (
Name: "kobold_git_push",
Help: "number of git pushes",
}, []string{"repo"})
metricImageSeen = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "kobold_image_seen",
Help: "number of images seen",
}, []string{"ref"})
)
7 changes: 7 additions & 0 deletions task/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"sync"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/volatiletech/null/v8"
Expand Down Expand Up @@ -234,6 +235,12 @@ func (p *Pool) Queue(ctx context.Context, channel string, msg []byte) (err error
}
}

for i := range refs {
if r, err := name.ParseReference(refs[i]); err == nil {
metricImageSeen.With(prometheus.Labels{"ref": r.Context().RepositoryStr()}).Inc()
}
}

_, err = p.queries.TasksAppend(ctx, store.TasksAppendParams{
Msgs: dbutil.SliceText(refs),
Name: channel,
Expand Down

0 comments on commit 3f1a725

Please sign in to comment.