Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions docs/architecture/appendix-a-trace-and-metric-catalog.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,20 @@ The start is the earliest of all those times, so a non-agent commit that predate

Only the first end of a PR counts. If a reopened PR ends again, the stored `ended` milestone blocks a second observation.

Every histogram is an exponential (native) histogram, with the SDK's default limit of 160 buckets. Over a range of 1 second to 30 days, 160 buckets give a scale of 2, which means each bucket spans a ratio of about 1.19 and a quantile estimate is off by about 9% at most. Goal thresholds of 600 s and 900 s sit inside a bucket, not on an edge, so a goal check has the same error. D15 records the choice, the fallback, and the backend check. Each attribute set exports one native histogram series, with the count and sum inside it, if Mimir ingests native histograms (unverified).
Completed CI jobs record queue and run durations for every conclusion:

The metric attributes are `vcs.repository.name`, and `github.author.type` with the values `user` or `bot`. The review counter adds `github.review.state`, and the cycle time histogram adds `vcs.change.state` and `github.pull_request.start_source`.
| Instrument | Unit | Start | End | Attributes |
| --- | --- | --- | --- | --- |
| `github.ci.job.queue_seconds` | `s` | Job `created_at` | Job `started_at` | `repository`, `workflow`, `job`, `conclusion`, `event_class` and `runner` |
| `github.ci.job.run_seconds` | `s` | Job `started_at` | Job `completed_at` | `repository`, `workflow`, `job`, `conclusion`, `event_class` and `runner` |

The workflow and job names come from `workflow_name` and `name`. The runner comes from the first `labels` entry or `none` for an empty or missing array. Each attribute removes line breaks and keeps at most 64 characters. Both histograms use explicit boundaries of 5, 15, 30, 60, 120, 300, 600, 900, 1200, 1800, 2700, 3600, 5400 and 7200 seconds. A missing or invalid timestamp skips that duration. A negative duration also skips that observation. The ledger claims the decimal job ID followed by a zero byte and `ci_job` before recording. Jobs without an ID skip recording. A duplicate claim skips all metrics and logs for that delivery; a claim error leaves it for retry.

The `event_class` attribute approximates the trigger because the payload has no triggering event. A `head_branch` with the prefix `gh-readonly-queue/` counts as `merge_group`. The repository's default branch counts as `push`; any other branch counts as `pull_request`. Scheduled and manual runs on the default branch count as `push`, and runs on a non-default branch from other triggers count as `pull_request`.

Every PR histogram is an exponential (native) histogram, with the SDK's default limit of 160 buckets. Over a range of 1 second to 30 days, 160 buckets give a scale of 2, which means each bucket spans a ratio of about 1.19 and a quantile estimate is off by about 9% at most. Goal thresholds of 600 s and 900 s sit inside a bucket, not on an edge, so a goal check has the same error. D15 records the choice, the fallback, and the backend check. Each attribute set exports one native histogram series, with the count and sum inside it, if Mimir ingests native histograms (unverified).

The PR metric attributes are `vcs.repository.name`, and `github.author.type` with the values `user` or `bot`. The review counter adds `github.review.state`, and the cycle time histogram adds `vcs.change.state` and `github.pull_request.start_source`.

### Direct metrics, not derived from traces

Expand Down
161 changes: 138 additions & 23 deletions internal/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"log/slog"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -76,7 +77,7 @@ type ClosedPR struct {
type Recorder struct {
events, merges, conclusions, dropped metric.Int64Counter
ciJobFailures metric.Int64Counter
cycleTime metric.Float64Histogram
cycleTime, ciJobQueue, ciJobRun metric.Float64Histogram
mainRed metric.Int64Gauge
ledger Ledger
log *slog.Logger
Expand All @@ -92,17 +93,20 @@ type checkRun struct {
}

type workflowJob struct {
WorkflowName string `json:"workflow_name"` //nolint:tagliatelle // GitHub uses this field name.
Name string `json:"name"`
Conclusion string `json:"conclusion"`
HeadBranch string `json:"head_branch"` //nolint:tagliatelle // GitHub uses this field name.
HeadSHA string `json:"head_sha"` //nolint:tagliatelle // GitHub uses this field name.
StartedAt string `json:"started_at"` //nolint:tagliatelle // GitHub uses this field name.
CompletedAt string `json:"completed_at"` //nolint:tagliatelle // GitHub uses this field name.
CreatedAt string `json:"created_at"` //nolint:tagliatelle // GitHub uses this field name.
Labels []string `json:"labels"`
WorkflowName string `json:"workflow_name"` //nolint:tagliatelle // GitHub uses this field name.
Name string `json:"name"`
Conclusion string `json:"conclusion"`
HeadBranch string `json:"head_branch"` //nolint:tagliatelle // GitHub uses this field name.
HeadSHA string `json:"head_sha"` //nolint:tagliatelle // GitHub uses this field name.
StartedAt string `json:"started_at"` //nolint:tagliatelle // GitHub uses this field name.
CompletedAt string `json:"completed_at"` //nolint:tagliatelle // GitHub uses this field name.
Steps []struct {
Name string `json:"name"`
Conclusion string `json:"conclusion"`
} `json:"steps"`
ID int64 `json:"id"`
RunID int64 `json:"run_id"` //nolint:tagliatelle // GitHub uses this field name.
}

Expand All @@ -118,7 +122,7 @@ type pullRequest struct {
// New creates the instruments used to record deliveries. The ledger claims
// each milestone before it counts.
//
//nolint:mnd // The histogram has fixed cycle time buckets in seconds.
//nolint:mnd // The histograms have fixed duration buckets in seconds.
func New(meter metric.Meter, ledger Ledger, log *slog.Logger) (*Recorder, error) {
if ledger == nil {
return nil, ErrNoLedger
Expand Down Expand Up @@ -162,6 +166,22 @@ func New(meter metric.Meter, ledger Ledger, log *slog.Logger) (*Recorder, error)
return nil, fmt.Errorf("create github.pr.cycle_time histogram: %w", err)
}

for _, histogram := range []struct {
target *metric.Float64Histogram
name string
}{
{&recorder.ciJobQueue, "github.ci.job.queue_seconds"},
{&recorder.ciJobRun, "github.ci.job.run_seconds"},
} {
instrument, histErr := meter.Float64Histogram(histogram.name, metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(5, 15, 30, 60, 120, 300, 600, 900, 1200, 1800, 2700, 3600, 5400, 7200))
if histErr != nil {
return nil, fmt.Errorf("create %s histogram: %w", histogram.name, histErr)
}

*histogram.target = instrument
}

recorder.mainRed, err = meter.Int64Gauge("github.main.red")
if err != nil {
return nil, fmt.Errorf("create github.main.red gauge: %w", err)
Expand Down Expand Up @@ -215,19 +235,20 @@ func (r *Recorder) Record(ctx context.Context, event string, body []byte) error
}
}

repo := attribute.String("repository", repository)
r.merges.Add(ctx, 0, metric.WithAttributes(repo))
if event == "workflow_job" && delivery.Action == actionCompleted {
return r.recordJob(ctx, repository, delivery.Repository.DefaultBranch, delivery.WorkflowJob)
}

eventAttrs := metric.WithAttributes(repo,
attribute.String("event", event), attribute.String("action", delivery.Action))
r.events.Add(ctx, 0, eventAttrs)
r.events.Add(ctx, 1, eventAttrs)
r.recordEvent(ctx, repository, event, delivery.Action)

switch {
case event == "check_run":
r.recordCheckRun(ctx, repo, delivery.Action, delivery.Repository.DefaultBranch, delivery.CheckRun)
case event == "workflow_job" && delivery.Action == actionCompleted:
r.recordFailedJob(ctx, repository, delivery.WorkflowJob)
if event == "check_run" {
r.recordCheckRun(
ctx,
attribute.String("repository", repository),
delivery.Action,
delivery.Repository.DefaultBranch,
delivery.CheckRun,
)
}

return nil
Expand Down Expand Up @@ -272,6 +293,16 @@ func (r *Recorder) RecordClosed(ctx context.Context, closed ClosedPR) (Outcome,
return out, err
}

func (r *Recorder) recordEvent(ctx context.Context, repository, event, action string) {
repo := attribute.String("repository", repository)
r.merges.Add(ctx, 0, metric.WithAttributes(repo))

eventAttrs := metric.WithAttributes(repo,
attribute.String("event", event), attribute.String("action", action))
r.events.Add(ctx, 0, eventAttrs)
r.events.Add(ctx, 1, eventAttrs)
}

// recordPullRequest handles a closed or reopened delivery. For a PR created
// at or before the repair floor, a reopen claims ended to guard against
// counting a first close the old recorder may already have counted.
Expand Down Expand Up @@ -334,14 +365,98 @@ func (r *Recorder) recordCheckRun(
r.mainRed.Record(ctx, red, metric.WithAttributes(repo))
}

func (r *Recorder) recordFailedJob(ctx context.Context, repository string, body json.RawMessage) {
func (r *Recorder) recordJob(
ctx context.Context,
repository, defaultBranch string,
body json.RawMessage,
) error {
var job workflowJob

err := json.Unmarshal(body, &job)
if err != nil || job.Conclusion != failure {
return
if err != nil || job.ID == 0 {
return nil //nolint:nilerr // Invalid job data may leave the queue.
}

claimed, err := r.ledger.ClaimMilestone(strconv.FormatInt(job.ID, 10) + "\x00ci_job")
if err != nil {
return fmt.Errorf("claim ci job milestone: %w", err)
}

if !claimed {
return nil
}

r.recordJobTiming(ctx, repository, defaultBranch, job)

if job.Conclusion == failure {
r.recordFailedJob(ctx, repository, job)
}

r.recordEvent(ctx, repository, "workflow_job", actionCompleted)

return nil
}

func (r *Recorder) recordJobTiming(ctx context.Context, repository, defaultBranch string, job workflowJob) {
eventClass, runner := jobEventClass(job.HeadBranch, defaultBranch), "none"
if len(job.Labels) > 0 {
runner = job.Labels[0]
}

values := []struct{ key, value string }{
{"repository", repository},
{"workflow", job.WorkflowName},
{"job", job.Name},
{"conclusion", job.Conclusion},
{"event_class", eventClass},
{"runner", runner},
}
attrs := make([]attribute.KeyValue, 0, len(values))
fields := []any{}

for _, field := range values {
attrs = append(attrs, attribute.String(field.key, cleanText(field.value, metricNameLimit)))
fields = append(fields, field.key, cleanText(field.value, logTextLimit))
}

fields = append(fields,
"head_branch", cleanText(job.HeadBranch, logTextLimit),
"head_sha", cleanText(job.HeadSHA, logTextLimit), "run_id", job.RunID, "job_id", job.ID)

for _, duration := range []struct {
histogram metric.Float64Histogram
name, start, end string
}{
{r.ciJobQueue, "queue_seconds", job.CreatedAt, job.StartedAt},
{r.ciJobRun, "run_seconds", job.StartedAt, job.CompletedAt},
} {
start, end := parseTime(duration.start), parseTime(duration.end)
if start.IsZero() || end.IsZero() || end.Before(start) {
continue
}

seconds := end.Sub(start).Seconds()
duration.histogram.Record(ctx, seconds, metric.WithAttributes(attrs...))
fields = append(fields, duration.name, seconds)
}

r.log.InfoContext(ctx, "ci job finished", fields...)
}

// jobEventClass approximates the trigger: scheduled and manual runs on the default
// branch count as push, and other triggers on non-default branches count as pull_request.
func jobEventClass(branch, defaultBranch string) string {
switch {
case strings.HasPrefix(branch, "gh-readonly-queue/"):
return "merge_group"
case branch == defaultBranch:
return "push"
default:
return "pull_request"
}
}

func (r *Recorder) recordFailedJob(ctx context.Context, repository string, job workflowJob) {
var failedSteps []string

for _, step := range job.Steps {
Expand Down
14 changes: 7 additions & 7 deletions internal/recorder/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func recordCycleFixture(t *testing.T, test cycleTimeTest) (*sdkmetric.ManualRead
}

body, err := json.Marshal(map[string]any{
"repository": map[string]string{"full_name": repositoryName},
"action": test.action,
"pull_request": map[string]any{
repositoryKey: map[string]string{fullNameKey: repositoryName},
"action": test.action,
pullRequestEvent: map[string]any{
"node_id": nodeID, "merged": test.merged, "created_at": test.createdAt,
"merged_at": test.end, "closed_at": test.end, "body": test.body,
},
Expand Down Expand Up @@ -245,6 +245,8 @@ const (
eventKey = "event"
eventsName = "github.events"
repositoryKey = "repository"
fullNameKey = "full_name"
mainBranch = "main"
unknownRepository = "unknown"
buildName = "build"
conclusionKey = "conclusion"
Expand Down Expand Up @@ -484,8 +486,6 @@ type mainRedStep struct {
func TestRecordMainRed(t *testing.T) {
t.Parallel()

const mainBranch = "main"

for _, test := range []struct {
name string
steps []mainRedStep
Expand Down Expand Up @@ -525,8 +525,8 @@ func TestRecordMainRed(t *testing.T) {

for _, step := range test.steps {
body, err := json.Marshal(map[string]any{
"repository": map[string]string{
"full_name": repositoryName,
repositoryKey: map[string]string{
fullNameKey: repositoryName,
"default_branch": step.defaultBranch,
},
"action": step.action,
Expand Down
Loading