serve: add /metrics endpoint for instance and cache size info#418
Merged
Conversation
Adds `GET /metrics` to the serve query service so callers and monitoring can see an instance's identity, liveness, and cache size usage without scraping logs. - MetricsService assembles a JSON snapshot: version, uptime, readiness, git engine, trackDeps, cache footprint (directory / entries / bytes / human), and JVM heap use. Wired into BazelDiffServer as an always-on endpoint, intentionally NOT gated on readiness so an un-ready or lame-ducked instance is still scrapeable (readiness is reported as a field). - MeasurableHashCacheStorage capability interface (mirrors the pluggable storage design); LocalDiskHashCacheStorage reports entry count + total bytes by summing its `*.json` entries. A backend that cannot cheaply report its size (e.g. a future S3 store) omits it and the size fields are null. - No new CLI flags; the endpoint is always available. Tests: MetricsService snapshot assembly (injected clock, null cache for a non-measurable backend), LocalDisk stats, the /metrics HTTP endpoint (200 / 405 / 404 / served-when-not-ready), and an end-to-end ServeCommand test that curls /metrics over real HTTP and reads live on-disk cache stats. Docs added to readme_template.md and the regenerated README. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Resolves conflicts from #417 (serve cache pruning) landing on master: - HashCacheStorage.kt: LocalDiskHashCacheStorage now implements BOTH MeasurableHashCacheStorage (stats(), this PR) and PrunableHashCacheStorage (prune(), #417), keeping both method bodies and the shared touch-on-get LRU. - ServeCommand.buildAndStartServer wires the MetricsService alongside #417's CachePruner startup; imports for both features merged. - cli/BUILD and LocalDiskHashCacheStorageTest keep both features' test targets and cases. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
GET /metricsto theservequery service so callers and monitoring can see a running instance's identity, liveness, and cache size usage without scraping logs.Example:
curl 'http://localhost:8080/metrics'{ "version": "31.4.0", "uptimeSeconds": 3600, "ready": true, "gitEngine": "jgit", "trackDeps": false, "cache": {"directory": "/var/cache/bazel-diff", "entries": 128, "sizeBytes": 4823913, "sizeHuman": "4.6 MB"}, "jvm": {"usedBytes": 123456789, "maxBytes": 2147483648} }How
MetricsServiceassembles the snapshot from the instance's config, its readiness flag, live JVM state, and — when the backing store supports it — the on-disk cache footprint. Read on demand per request (metrics are polled at a low rate, so walking the cache dir each call is fine).MeasurableHashCacheStoragecapability interface (mirrors the existing pluggable-storage design).LocalDiskHashCacheStoragereports entry count + total bytes by summing its*.jsonentries. A backend that can't cheaply report size (a future S3 store) simply doesn't implement it, and thecachesize fields arenull.BazelDiffServeras an always-on endpoint. Deliberately not gated on readiness — a scrape of an un-ready or lame-ducked instance still returns data, with the current state in thereadyfield.Notes for reviewers
master. There is a small, intentional overlap inHashCacheStorage.kt: this PR adds aMeasurableHashCacheStoragecapability while serve: prune cached hashes to bound cache growth #417 adds aPrunableHashCacheStorageone. They're separate interfaces/methods and will reconcile trivially when both land.sizeHumanuses base-1024 units to match the--cacheMaxSizescale from serve: prune cached hashes to bound cache growth #417.Testing
MetricsServicesnapshot assembly (injected clock for deterministic uptime;nullcache for a non-measurable backend), andLocalDiskHashCacheStorage.stats()./metricsreturns200+ JSON,405on non-GET,404when no provider is wired, and is served even when not ready.ServeCommandTestcase that stands up the real server viabuildAndStartServerand curls/metricsover real HTTP, reading live on-disk cache stats parsed from JSON.ktfmt/buildifierclean; docs inreadme_template.md+ regeneratedREADME.md.