Skip to content

feat(storage/otelcache): expose cache stats via OpenTelemetry#93

Merged
achille-roussel merged 1 commit into
mainfrom
metrics-reader
May 18, 2026
Merged

feat(storage/otelcache): expose cache stats via OpenTelemetry#93
achille-roussel merged 1 commit into
mainfrom
metrics-reader

Conversation

@achille-roussel

Copy link
Copy Markdown
Contributor

Summary

  • New storage/otelcache package: Register(provider, observable, attrs...) wires Stat() storage.CacheStat into OTel observable instruments — gauges for size/limit/entries, counters for hits/misses/evictions. Cache implementations stay free of OTel imports.
  • Observable is a one-method interface (Stat() storage.CacheStat); file.Cache satisfies it directly with no shims.
  • Fix file.Cache.Stat() to populate Entries from the underlying LRU (it was always zero before).

Why this shape

Implementing go.opentelemetry.io/otel/sdk/metric.Reader would put the library in the SDK's exporter pipeline — the wrong layer, since the application owns the MeterProvider, Views, and exporter setup. Observable instruments registered against the application's MeterProvider are the idiomatic pull model; placing them in a separate package that consumes Stat() keeps the cache types decoupled from OTel.

Usage

fileCache := file.NewCache(tmpdir, size)
reg, _ := otelcache.Register(provider, fileCache,
    attribute.String("storage.cache.kind", "file"))
defer reg.Unregister()

Test plan

  • go test ./storage/otelcache/...
  • go test ./storage/file/...
  • go test ./storage/...
  • go vet ./...
  • Confirm storage/file has no OTel imports (go list -f '{{ join .Imports "\n" }}' ./storage/file | grep otel → empty)

🤖 Generated with Claude Code

Add a new storage/otelcache package that bridges storage.CacheStat to
OTel observable instruments. Register accepts any value satisfying
Observable (Stat() storage.CacheStat) so cache implementations stay
free of OTel imports; file.Cache satisfies it directly.

Also fix file.Cache.Stat() to populate Entries from the underlying LRU
(the field was always zero before).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jpugliesi jpugliesi self-requested a review May 18, 2026 23:06
@achille-roussel achille-roussel merged commit f32c42c into main May 18, 2026
3 checks passed
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.

2 participants