Skip to content

Record metrics through an interface so the handlers don't link Prometheus - #886

Draft
simonchrz wants to merge 7 commits into
git-pkgs:mainfrom
simonchrz:refactor/metrics-recorder
Draft

simonchrz wants to merge 7 commits into
git-pkgs:mainfrom
simonchrz:refactor/metrics-recorder

Conversation

@simonchrz

Copy link
Copy Markdown
Contributor

The metrics part of step 5 of #501. Stacked on #868: until that is merged, the diff here includes it. Only the last commit is new.

internal/handler builds for wasip1 with #868, but it still links Prometheus, because the handlers call metrics.Record* and internal/metrics holds the Prometheus vectors.

  • internal/metrics keeps the Record* functions that the handlers, the git cache and the scanners call, FailureDetail and the cache kind constants. The functions pass each call to a registered Recorder, and to nothing while none is registered. The package imports only the standard library.
  • internal/metrics/prom gets the Prometheus vectors, the /metrics handler, the snapshot for /ui/analytics, the failure details and the gauges the server publishes. The files are moved, not rewritten. Importing the package registers it as the Recorder, the way internal/database/drivers registers the SQL drivers.
  • No call site in internal/handler, internal/gitcache or internal/scanner changes. internal/server and the tests that read a metric import prom; that is a rename of the prefix.

No behaviour change. I ran the binary of #868 and of this branch against the same npm and rpm requests and compared the request, cache, upstream and storage lines of /metrics: they are the same.

#868 this PR
Prometheus packages internal/handler and internal/mirror depend on 9 0

Prometheus builds for wasip1, so the wasm build doesn't notice it coming back. The wasm job gets a second step that fails when go list -deps of internal/handler or internal/mirror names a Prometheus package.

Two choices you may want differently:

  • The Recorder is registered globally. The counters are global today, and this way no call site changes. Passing a recorder into handler.Proxy would be a larger change.
  • The package name prom. prometheus would clash with the client library's package name.

internal/storage held the Storage interface next to the gocloud and GCS
backends, so every package that reads or writes artifacts compiled in the
AWS, Azure and Google SDKs. The backends now have their own package, and
internal/storage keeps the interface, the errors and the path layout.

internal/handler drops from 837 to 389 packages in its dependency graph and
no longer imports a cloud SDK. internal/storage builds for wasip1 and joins
the wasm build in CI.

No behaviour change. Step 5 of git-pkgs#501, for storage.
internal/database imported modernc.org/sqlite and lib/pq only to register
them, which kept the package and everything that uses it from building for
targets the drivers don't support. internal/database/drivers now registers
both, and internal/server and internal/cli import it.

No behaviour change. Step 5 of git-pkgs#501, for the database.
internal/gitcache locked with syscall.Flock, which WASI doesn't have. A
lockFile for wasip1 returns errors.ErrUnsupported; the git cache can't run
there anyway, since it starts git.

With the storage backends and the SQL drivers in their own packages,
internal/handler and internal/mirror now build for wasip1. CI builds them,
so a native import in a handler fails the wasm job.

Part of git-pkgs#501.
…ndlers' imports

internal/metrics keeps the Record functions the handlers, the git cache and the scanners call, and passes them to a registered Recorder. The Prometheus metrics move to internal/metrics/prom, which registers itself when imported. The files are moved, not rewritten.

internal/server and the tests that read a metric import prom. The wasm job also checks that the handlers don't depend on Prometheus, since the build alone doesn't notice it.
@coderabbitai

coderabbitai Bot commented Dec 3, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant