feat(observability): add OpenTelemetry tracing and metrics middleware…#42
feat(observability): add OpenTelemetry tracing and metrics middleware…#42
Conversation
… with docs and example middleware: introduce OTelTracingMiddleware that wraps all Service methods with spans and attributes (e.g., method names, key.len, hit, counts, expiration.ms) and records errors middleware: add OTelMetricsMiddleware with counter (hypercache.calls) and duration histogram (hypercache.duration.ms), tagging method, key.len, hit, item/key counts examples: add otel.go using noop MeterProvider/TracerProvider and ApplyMiddleware; defers svc.Stop() for clean shutdown docs: README Observability section with wiring snippet; examples README links the new example deps: add go.opentelemetry.io/otel, otel/metric, and otel/trace to go.mod/go.sum No breaking changes; behavior unchanged unless middleware is applied.
|
Running Code Quality on PRs by uploading data to Trunk will soon be removed. You can still run checks on your PRs using trunk-action - see the migration guide for more information. |
There was a problem hiding this comment.
Pull Request Overview
This PR adds OpenTelemetry observability support to HyperCache by introducing middleware for tracing and metrics collection. The implementation provides comprehensive instrumentation without breaking existing functionality.
Key changes:
- Added OTelTracingMiddleware to wrap service methods with OpenTelemetry spans and attributes
- Added OTelMetricsMiddleware to emit call counters and duration histograms
- Included a complete example demonstrating usage with noop providers
Reviewed Changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/middleware/otel_tracing.go | Implements tracing middleware with spans for all service methods and configurable attributes |
| pkg/middleware/otel_metrics.go | Implements metrics middleware with call counters and duration histograms |
| go.mod | Adds OpenTelemetry dependencies (otel, metric, trace) |
| __examples/observability/otel.go | Provides usage example with noop providers and middleware composition |
| __examples/README.md | Links to the new observability example |
| README.md | Documents observability features with wiring snippet |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| } | ||
|
|
||
| // Get implements Service.Get with tracing. | ||
| func (mw OTelTracingMiddleware) Get(ctx context.Context, key string) (any, bool) { |
There was a problem hiding this comment.
Receiver should be a pointer (*OTelTracingMiddleware) for consistency with the other methods and to avoid potential performance issues with large structs.
| func (mw OTelTracingMiddleware) Get(ctx context.Context, key string) (any, bool) { | |
| func (mw *OTelTracingMiddleware) Get(ctx context.Context, key string) (any, bool) { |
| } | ||
|
|
||
| // Set implements Service.Set with tracing. | ||
| func (mw OTelTracingMiddleware) Set(ctx context.Context, key string, value any, expiration time.Duration) error { |
There was a problem hiding this comment.
Receiver should be a pointer (*OTelTracingMiddleware) for consistency with the other methods and to avoid potential performance issues with large structs.
| func (mw OTelTracingMiddleware) Set(ctx context.Context, key string, value any, expiration time.Duration) error { | |
| func (mw *OTelTracingMiddleware) Set(ctx context.Context, key string, value any, expiration time.Duration) error { |
… with docs and example
middleware: introduce OTelTracingMiddleware that wraps all Service methods with spans and attributes (e.g., method names, key.len, hit, counts, expiration.ms) and records errors
middleware: add OTelMetricsMiddleware with counter (hypercache.calls) and duration histogram (hypercache.duration.ms), tagging method, key.len, hit, item/key counts
examples: add otel.go using noop MeterProvider/TracerProvider and ApplyMiddleware; defers svc.Stop() for clean shutdown
docs: README Observability section with wiring snippet; examples README links the new example
deps: add go.opentelemetry.io/otel, otel/metric, and otel/trace to go.mod/go.sum
No breaking changes; behavior unchanged unless middleware is applied.