From 883207eea10e5dc4c3fbc04d67e3a60f4ba39ca0 Mon Sep 17 00:00:00 2001 From: "tbhb.dev Agent" Date: Sun, 15 Nov 2026 16:41:20 -0500 Subject: [PATCH] feat: record queued events as metrics Generated-by: LLM effort=high;harness=codex:0.164.0;model=openai:gpt-6.1-sol;session=sha-256-64:ca12ec0da8efeef9;surface=exec --- .cspell-words.txt | 2 + BUILD.bazel | 2 + cmd/github-metrics/main.go | 2 +- internal/recorder/BUILD.bazel | 29 ++++++ internal/recorder/recorder.go | 94 ++++++++++++++++++++ internal/recorder/recorder_test.go | 138 +++++++++++++++++++++++++++++ internal/worker/BUILD.bazel | 11 ++- internal/worker/export_test.go | 7 -- internal/worker/worker.go | 45 +--------- internal/worker/worker_test.go | 69 +++++---------- 10 files changed, 295 insertions(+), 104 deletions(-) create mode 100644 internal/recorder/BUILD.bazel create mode 100644 internal/recorder/recorder.go create mode 100644 internal/recorder/recorder_test.go delete mode 100644 internal/worker/export_test.go diff --git a/.cspell-words.txt b/.cspell-words.txt index 72443cc..8c28479 100644 --- a/.cspell-words.txt +++ b/.cspell-words.txt @@ -217,3 +217,5 @@ otlpmetric otlpmetrichttp sdkmetric stdr +metricdata +nilerr diff --git a/BUILD.bazel b/BUILD.bazel index 81e3241..4791640 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -105,6 +105,8 @@ GO_TARGETS = [ "//internal/budget:budget_test", "//internal/ghclient", "//internal/ghclient:ghclient_test", + "//internal/recorder", + "//internal/recorder:recorder_test", "//internal/receiver", "//deploy:deploy_test", "//internal/receiver:receiver_test", diff --git a/cmd/github-metrics/main.go b/cmd/github-metrics/main.go index 3e2d384..f0d6bf0 100644 --- a/cmd/github-metrics/main.go +++ b/cmd/github-metrics/main.go @@ -129,7 +129,7 @@ func run(addr string, log *slog.Logger) error { type noopRecorder struct{} -func (noopRecorder) RecordPullRequest(_, _ string) error { return errNoRecorder } +func (noopRecorder) Record(_ context.Context, _ string, _ []byte) error { return errNoRecorder } // probe asks the local health endpoint and returns the exit code. func probe(addr string) int { diff --git a/internal/recorder/BUILD.bazel b/internal/recorder/BUILD.bazel new file mode 100644 index 0000000..74f0d8c --- /dev/null +++ b/internal/recorder/BUILD.bazel @@ -0,0 +1,29 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright Tony Burns + +load("@rules_go//go:def.bzl", "go_library", "go_test") +load("@tbhb_rules//repo:defs.bzl", "repo_files") + +repo_files() + +go_library( + name = "recorder", + srcs = ["recorder.go"], + importpath = "github.com/tbhb-dev/github-metrics/internal/recorder", + visibility = ["//:__subpackages__"], + deps = [ + "@io_opentelemetry_go_otel//attribute", + "@io_opentelemetry_go_otel_metric//:metric", + ], +) + +go_test( + name = "recorder_test", + srcs = ["recorder_test.go"], + visibility = ["//:__pkg__"], + deps = [ + ":recorder", + "@io_opentelemetry_go_otel_sdk_metric//:metric", + "@io_opentelemetry_go_otel_sdk_metric//metricdata", + ], +) diff --git a/internal/recorder/recorder.go b/internal/recorder/recorder.go new file mode 100644 index 0000000..b7bfd99 --- /dev/null +++ b/internal/recorder/recorder.go @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Tony Burns + +// Package recorder counts queued GitHub deliveries as metrics. +package recorder + +import ( + "context" + "encoding/json" + "fmt" + + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/metric" +) + +// Recorder counts events without recording webhook text in attributes. +type Recorder struct { + events, merges, conclusions, dropped metric.Int64Counter +} + +// New creates the counters used to record deliveries. +func New(meter metric.Meter) (*Recorder, error) { + var recorder Recorder + + for _, counter := range []struct { + target *metric.Int64Counter + name string + }{ + {&recorder.events, "github.events"}, + {&recorder.merges, "github.pr.merges"}, + {&recorder.conclusions, "github.check.conclusions"}, + {&recorder.dropped, "github.events.dropped"}, + } { + instrument, err := meter.Int64Counter(counter.name) + if err != nil { + return nil, fmt.Errorf("create %s counter: %w", counter.name, err) + } + + *counter.target = instrument + } + + return &recorder, nil +} + +// Record counts a delivery or a drop before allowing its removal from the queue. +func (r *Recorder) Record(ctx context.Context, event string, body []byte) error { + if event == "" { + r.dropped.Add(ctx, 1, metric.WithAttributes(attribute.String("reason", "empty_event"))) + + return nil + } + + var delivery struct { + Repository struct { + FullName string `json:"full_name"` //nolint:tagliatelle // GitHub uses this field name. + } `json:"repository"` + Action string `json:"action"` + CheckRun struct { + Name string `json:"name"` + Conclusion string `json:"conclusion"` + } `json:"check_run"` //nolint:tagliatelle // GitHub uses this field name. + PullRequest struct { + Merged bool `json:"merged"` + } `json:"pull_request"` //nolint:tagliatelle // GitHub uses this field name. + } + + err := json.Unmarshal(body, &delivery) + if err != nil { + r.dropped.Add(ctx, 1, metric.WithAttributes(attribute.String("reason", "bad_body"))) + + return nil //nolint:nilerr // Bad data is counted as a drop and may leave the queue. + } + + repository := delivery.Repository.FullName + if repository == "" { + repository = "unknown" + } + + repo := attribute.String("repository", repository) + r.events.Add(ctx, 1, metric.WithAttributes(repo, + attribute.String("event", event), attribute.String("action", delivery.Action))) + + if event == "pull_request" && delivery.Action == "closed" && delivery.PullRequest.Merged { + r.merges.Add(ctx, 1, metric.WithAttributes(repo)) + } + + if event == "check_run" && delivery.Action == "completed" { + r.conclusions.Add(ctx, 1, metric.WithAttributes(repo, + attribute.String("check_name", delivery.CheckRun.Name), + attribute.String("conclusion", delivery.CheckRun.Conclusion))) + } + + return nil +} diff --git a/internal/recorder/recorder_test.go b/internal/recorder/recorder_test.go new file mode 100644 index 0000000..eea3b73 --- /dev/null +++ b/internal/recorder/recorder_test.go @@ -0,0 +1,138 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Tony Burns + +package recorder_test + +import ( + "reflect" + "testing" + + "go.opentelemetry.io/otel/sdk/metric/metricdata" + + sdkmetric "go.opentelemetry.io/otel/sdk/metric" + + "github.com/tbhb-dev/github-metrics/internal/recorder" +) + +const ( + pullRequestEvent = "pull_request" + checkRunEvent = "check_run" + actionKey = "action" + eventKey = "event" + eventsName = "github.events" + repositoryKey = "repository" + unknownRepository = "unknown" +) + +func TestRecord(t *testing.T) { + t.Parallel() + + for _, test := range []struct { + want map[string]map[string]string + name, event, body string + }{ + { + map[string]map[string]string{ + eventsName: {repositoryKey: "tbhb-dev/github-metrics", eventKey: pullRequestEvent, actionKey: "closed"}, + "github.pr.merges": {repositoryKey: "tbhb-dev/github-metrics"}, + }, + "merge", pullRequestEvent, + `{"repository":{"full_name":"tbhb-dev/github-metrics"},"action":"closed",` + + `"pull_request":{"merged":true,"title":"omit title","body":"omit body",` + + `"head":{"ref":"omit branch"},"user":{"login":"omit login"}},"comment":{"body":"omit text"}}`, + }, + { + map[string]map[string]string{ + eventsName: {repositoryKey: unknownRepository, eventKey: pullRequestEvent, actionKey: "closed"}, + }, + "closed without merge", pullRequestEvent, `{"action":"closed","pull_request":{"merged":false}}`, + }, + { + map[string]map[string]string{ + eventsName: {repositoryKey: unknownRepository, eventKey: checkRunEvent, actionKey: "completed"}, + "github.check.conclusions": {repositoryKey: unknownRepository, "check_name": "build", "conclusion": "success"}, + }, + "completed check", checkRunEvent, + `{"action":"completed","check_run":{"name":"build","conclusion":"success"}}`, + }, + { + map[string]map[string]string{ + eventsName: {repositoryKey: unknownRepository, eventKey: checkRunEvent, actionKey: "created"}, + }, + "running check", checkRunEvent, `{"action":"created","check_run":{"name":"build"}}`, + }, + { + map[string]map[string]string{ + eventsName: {repositoryKey: unknownRepository, eventKey: "ping", actionKey: ""}, + }, + "other event with empty action", "ping", `{}`, + }, + { + map[string]map[string]string{"github.events.dropped": {"reason": "empty_event"}}, + "legacy entry", "", `{}`, + }, + { + map[string]map[string]string{"github.events.dropped": {"reason": "bad_body"}}, + "bad JSON", pullRequestEvent, `{"action":`, + }, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + reader := sdkmetric.NewManualReader() + provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) + + t.Cleanup(func() { + shutdownErr := provider.Shutdown(t.Context()) + if shutdownErr != nil { + t.Error(shutdownErr) + } + }) + + consumer, err := recorder.New(provider.Meter("test")) + if err != nil { + t.Fatal(err) + } + + err = consumer.Record(t.Context(), test.event, []byte(test.body)) + if err != nil { + t.Fatal(err) + } + + checkMetrics(t, reader, test.want) + }) + } +} + +func checkMetrics(t *testing.T, reader *sdkmetric.ManualReader, want map[string]map[string]string) { + t.Helper() + + var data metricdata.ResourceMetrics + + err := reader.Collect(t.Context(), &data) + if err != nil { + t.Fatal(err) + } + + got := make(map[string]map[string]string) + + for _, scope := range data.ScopeMetrics { + for _, measurement := range scope.Metrics { + sum, ok := measurement.Data.(metricdata.Sum[int64]) + if !ok || !sum.IsMonotonic || len(sum.DataPoints) != 1 || sum.DataPoints[0].Value != 1 { + t.Fatalf("%s: expected one counter observation of 1, got %v", measurement.Name, measurement.Data) + } + + attrs := make(map[string]string) + for _, attr := range sum.DataPoints[0].Attributes.ToSlice() { + attrs[string(attr.Key)] = attr.Value.AsString() + } + + got[measurement.Name] = attrs + } + } + + if !reflect.DeepEqual(got, want) { + t.Fatalf("metrics = %v, want %v", got, want) + } +} diff --git a/internal/worker/BUILD.bazel b/internal/worker/BUILD.bazel index bf8ce9f..da7c554 100644 --- a/internal/worker/BUILD.bazel +++ b/internal/worker/BUILD.bazel @@ -16,11 +16,10 @@ go_library( go_test( name = "worker_test", - srcs = [ - "export_test.go", - "worker_test.go", - ], - embed = [":worker"], + srcs = ["worker_test.go"], visibility = ["//:__pkg__"], - deps = ["//internal/store"], + deps = [ + ":worker", + "//internal/store", + ], ) diff --git a/internal/worker/export_test.go b/internal/worker/export_test.go deleted file mode 100644 index fcf6df7..0000000 --- a/internal/worker/export_test.go +++ /dev/null @@ -1,7 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright Tony Burns - -package worker - -// Parse reads a pull request body for table tests. -func Parse(body []byte) (string, string, error) { return parse(body) } diff --git a/internal/worker/worker.go b/internal/worker/worker.go index 52a4b8c..d7997ba 100644 --- a/internal/worker/worker.go +++ b/internal/worker/worker.go @@ -6,8 +6,6 @@ package worker import ( "context" - "encoding/json" - "errors" "fmt" "log/slog" "strings" @@ -18,11 +16,9 @@ import ( const batchSize = 100 -var errMissingFields = errors.New("pull request repository or action is empty") - -// Recorder records a pull request event, without a telemetry dependency. +// Recorder records a queued event, without a telemetry dependency. type Recorder interface { - RecordPullRequest(repository, action string) error + Record(ctx context.Context, event string, body []byte) error } // Queue is the part of the store that a worker consumes. @@ -31,7 +27,7 @@ type Queue interface { Remove(seq uint64) error } -// Worker records pull request events and removes consumed queue entries. +// Worker records events and removes consumed queue entries. type Worker struct { queue Queue recorder Recorder @@ -93,20 +89,7 @@ func (w *Worker) Pass(ctx context.Context) error { } func (w *Worker) record(ctx context.Context, entry store.Entry) bool { - if entry.Event != "pull_request" { - w.log.WarnContext(ctx, "unhandled event", "event", oneLine(entry.Event)) - - return false - } - - repository, action, err := parse(entry.Body) - if err != nil { - w.log.WarnContext(ctx, "unhandled event", "reason", "bad pull request body") - - return false - } - - err = w.recorder.RecordPullRequest(repository, action) + err := w.recorder.Record(ctx, entry.Event, entry.Body) if err != nil { w.log.WarnContext(ctx, "recording failed", "event", oneLine(entry.Event)) } @@ -114,24 +97,4 @@ func (w *Worker) record(ctx context.Context, entry store.Entry) bool { return err == nil } -func parse(body []byte) (string, string, error) { - var event struct { - Repository struct { - FullName string `json:"full_name"` //nolint:tagliatelle // GitHub uses this field name - } `json:"repository"` - Action string `json:"action"` - } - - err := json.Unmarshal(body, &event) - if err != nil { - return "", "", fmt.Errorf("parse pull request: %w", err) - } - - if event.Repository.FullName == "" || event.Action == "" { - return "", "", errMissingFields - } - - return event.Repository.FullName, event.Action, nil -} - func oneLine(s string) string { return strings.ReplaceAll(strings.ReplaceAll(s, "\n", ""), "\r", "") } diff --git a/internal/worker/worker_test.go b/internal/worker/worker_test.go index 2736044..ca25c0c 100644 --- a/internal/worker/worker_test.go +++ b/internal/worker/worker_test.go @@ -25,7 +25,6 @@ const ( removeFirst = "remove 1" pullRequestEvent = "pull_request" openedBody = `{"repository":{"full_name":"tbhb-dev/github-metrics"},"action":"opened"}` - badBodyReason = "bad pull request body" ) type queueStub struct { @@ -49,38 +48,10 @@ func (q *queueStub) Pending(limit int) ([]store.Entry, error) { func (q *queueStub) Remove(seq uint64) error { return q.remove(seq) } -type recorderFunc func(string, string) error +type recorderFunc func(context.Context, string, []byte) error -func (f recorderFunc) RecordPullRequest(repo, action string) error { return f(repo, action) } - -func TestParse(t *testing.T) { - t.Parallel() - - for _, test := range []struct { - name, body, repository, action string - bad bool - }{ - {"opened", openedBody, "tbhb-dev/github-metrics", "opened", false}, - { - "closed with unused fields", - `{"action":"closed","repository":{"full_name":"tbhb-dev/wiki","id":1},"pull_request":{"number":2}}`, - "tbhb-dev/wiki", "closed", false, - }, - {"bad JSON", `{"action":`, "", "", true}, - {"bad action type", `{"action":1}`, "", "", true}, - {"bad repository type", `{"repository":{"full_name":1}}`, "", "", true}, - {"empty repository", `{"repository":{"full_name":""},"action":"opened"}`, "", "", true}, - {"empty action", `{"repository":{"full_name":"tbhb-dev/github-metrics"},"action":""}`, "", "", true}, - } { - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - repository, action, err := worker.Parse([]byte(test.body)) - if (err != nil) != test.bad || repository != test.repository || action != test.action { - t.Fatalf("parse = %q, %q, %v", repository, action, err) - } - }) - } +func (f recorderFunc) Record(ctx context.Context, event string, body []byte) error { + return f(ctx, event, body) } func TestPass(t *testing.T) { @@ -88,25 +59,25 @@ func TestPass(t *testing.T) { for _, test := range []struct { name, event, body, log string + recordErr error want []string queued int }{ { - "record before remove", pullRequestEvent, openedBody, "", - []string{"record tbhb-dev/github-metrics opened", removeFirst}, + "record before remove", pullRequestEvent, openedBody, "", nil, + []string{"record " + pullRequestEvent + " " + openedBody, removeFirst}, 0, }, - {"bad body kept", pullRequestEvent, `{"action":`, badBodyReason, nil, 1}, - {"empty fields kept", pullRequestEvent, `{}`, badBodyReason, nil, 1}, { - "empty repository kept", pullRequestEvent, `{"repository":{"full_name":""},"action":"opened"}`, - badBodyReason, nil, 1, + "raw body passed through", "ping", `{"action":`, "", nil, + []string{`record ping {"action":`, removeFirst}, + 0, }, { - "empty action kept", pullRequestEvent, `{"repository":{"full_name":"tbhb-dev/github-metrics"},"action":""}`, - badBodyReason, nil, 1, + "recorder error kept", "ping\r\n", openedBody, "event=ping", store.ErrCorrupt, + []string{"record ping\r\n " + openedBody}, + 1, }, - {"other event kept", "ping\r\n", openedBody, "event=ping", nil, 1}, } { t.Run(test.name, func(t *testing.T) { t.Parallel() @@ -127,10 +98,10 @@ func TestPass(t *testing.T) { return nil }, } - recorder := recorderFunc(func(repository, action string) error { - calls = append(calls, "record "+repository+" "+action) + recorder := recorderFunc(func(_ context.Context, event string, body []byte) error { + calls = append(calls, "record "+event+" "+string(body)) - return nil + return test.recordErr }) err := worker.New(&queue, recorder, slog.New(slog.NewTextHandler(&logs, nil))).Pass(t.Context()) @@ -185,7 +156,7 @@ func TestPassDrainsBatches(t *testing.T) { return nil }, } - consumer := worker.New(&queue, recorderFunc(func(_, _ string) error { + consumer := worker.New(&queue, recorderFunc(func(_ context.Context, _ string, _ []byte) error { recorded++ return nil @@ -229,7 +200,7 @@ func testRecorderRetry(t *testing.T, passes int) { var records int recordErr := store.ErrCorrupt - consumer := worker.New(queue, recorderFunc(func(_, _ string) error { + consumer := worker.New(queue, recorderFunc(func(_ context.Context, _ string, _ []byte) error { records++ return recordErr @@ -320,7 +291,7 @@ func TestRunPollsAndCancelStops(t *testing.T) { } consumer := worker.New( &queue, - recorderFunc(func(_, _ string) error { + recorderFunc(func(_ context.Context, _ string, _ []byte) error { t.Error("unexpected record") return nil @@ -375,7 +346,7 @@ func TestCancelDuringBatchStops(t *testing.T) { } consumer := worker.New( &queue, - recorderFunc(func(_, _ string) error { + recorderFunc(func(_ context.Context, _ string, _ []byte) error { records++ cancel() @@ -399,7 +370,7 @@ func twoEntries() []store.Entry { } func countRecords(records *int) recorderFunc { - return func(_, _ string) error { + return func(_ context.Context, _ string, _ []byte) error { *records++ return nil