|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "go.opentelemetry.io/otel/attribute" |
| 9 | + "go.opentelemetry.io/otel/metric" |
| 10 | + |
| 11 | + "github.com/hyp3rd/hypercache" |
| 12 | + "github.com/hyp3rd/hypercache/pkg/backend" |
| 13 | + "github.com/hyp3rd/hypercache/pkg/cache" |
| 14 | + "github.com/hyp3rd/hypercache/pkg/stats" |
| 15 | +) |
| 16 | + |
| 17 | +// OTelMetricsMiddleware emits OpenTelemetry metrics for service methods. |
| 18 | +type OTelMetricsMiddleware struct { |
| 19 | + next hypercache.Service |
| 20 | + meter metric.Meter |
| 21 | + |
| 22 | + // instruments |
| 23 | + calls metric.Int64Counter |
| 24 | + durations metric.Float64Histogram |
| 25 | +} |
| 26 | + |
| 27 | +// NewOTelMetricsMiddleware constructs a metrics middleware using the provided meter. |
| 28 | +func NewOTelMetricsMiddleware(next hypercache.Service, meter metric.Meter) (hypercache.Service, error) { |
| 29 | + calls, err := meter.Int64Counter("hypercache.calls") |
| 30 | + if err != nil { |
| 31 | + return nil, fmt.Errorf("create counter: %w", err) |
| 32 | + } |
| 33 | + |
| 34 | + durations, err := meter.Float64Histogram("hypercache.duration.ms") |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("create histogram: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + return &OTelMetricsMiddleware{next: next, meter: meter, calls: calls, durations: durations}, nil |
| 40 | +} |
| 41 | + |
| 42 | +// Get implements Service.Get with metrics. |
| 43 | +func (mw *OTelMetricsMiddleware) Get(ctx context.Context, key string) (any, bool) { |
| 44 | + start := time.Now() |
| 45 | + v, ok := mw.next.Get(ctx, key) |
| 46 | + mw.rec(ctx, "Get", start, attribute.Int("key.len", len(key)), attribute.Bool("hit", ok)) |
| 47 | + |
| 48 | + return v, ok |
| 49 | +} |
| 50 | + |
| 51 | +// Set implements Service.Set with metrics. |
| 52 | +func (mw *OTelMetricsMiddleware) Set(ctx context.Context, key string, value any, expiration time.Duration) error { |
| 53 | + start := time.Now() |
| 54 | + err := mw.next.Set(ctx, key, value, expiration) |
| 55 | + mw.rec(ctx, "Set", start, attribute.Int("key.len", len(key))) |
| 56 | + |
| 57 | + return err |
| 58 | +} |
| 59 | + |
| 60 | +// GetOrSet implements Service.GetOrSet with metrics. |
| 61 | +func (mw *OTelMetricsMiddleware) GetOrSet(ctx context.Context, key string, value any, expiration time.Duration) (any, error) { |
| 62 | + start := time.Now() |
| 63 | + v, err := mw.next.GetOrSet(ctx, key, value, expiration) |
| 64 | + mw.rec(ctx, "GetOrSet", start, attribute.Int("key.len", len(key))) |
| 65 | + |
| 66 | + return v, err |
| 67 | +} |
| 68 | + |
| 69 | +// GetWithInfo implements Service.GetWithInfo with metrics. |
| 70 | +func (mw *OTelMetricsMiddleware) GetWithInfo(ctx context.Context, key string) (*cache.Item, bool) { |
| 71 | + start := time.Now() |
| 72 | + it, ok := mw.next.GetWithInfo(ctx, key) |
| 73 | + mw.rec(ctx, "GetWithInfo", start, attribute.Int("key.len", len(key)), attribute.Bool("hit", ok)) |
| 74 | + |
| 75 | + return it, ok |
| 76 | +} |
| 77 | + |
| 78 | +// GetMultiple implements Service.GetMultiple with metrics. |
| 79 | +func (mw *OTelMetricsMiddleware) GetMultiple(ctx context.Context, keys ...string) (map[string]any, map[string]error) { |
| 80 | + start := time.Now() |
| 81 | + res, failed := mw.next.GetMultiple(ctx, keys...) |
| 82 | + mw.rec(ctx, "GetMultiple", start, attribute.Int("keys.count", len(keys)), attribute.Int("result.count", len(res)), attribute.Int("failed.count", len(failed))) |
| 83 | + |
| 84 | + return res, failed |
| 85 | +} |
| 86 | + |
| 87 | +// List implements Service.List with metrics. |
| 88 | +func (mw *OTelMetricsMiddleware) List(ctx context.Context, filters ...backend.IFilter) ([]*cache.Item, error) { |
| 89 | + start := time.Now() |
| 90 | + items, err := mw.next.List(ctx, filters...) |
| 91 | + |
| 92 | + n := 0 |
| 93 | + if items != nil { |
| 94 | + n = len(items) |
| 95 | + } |
| 96 | + |
| 97 | + mw.rec(ctx, "List", start, attribute.Int("items.count", n)) |
| 98 | + |
| 99 | + return items, err |
| 100 | +} |
| 101 | + |
| 102 | +// Remove implements Service.Remove with metrics. |
| 103 | +func (mw *OTelMetricsMiddleware) Remove(ctx context.Context, keys ...string) error { |
| 104 | + start := time.Now() |
| 105 | + err := mw.next.Remove(ctx, keys...) |
| 106 | + mw.rec(ctx, "Remove", start, attribute.Int("keys.count", len(keys))) |
| 107 | + |
| 108 | + return err |
| 109 | +} |
| 110 | + |
| 111 | +// Clear implements Service.Clear with metrics. |
| 112 | +func (mw *OTelMetricsMiddleware) Clear(ctx context.Context) error { |
| 113 | + start := time.Now() |
| 114 | + err := mw.next.Clear(ctx) |
| 115 | + mw.rec(ctx, "Clear", start) |
| 116 | + |
| 117 | + return err |
| 118 | +} |
| 119 | + |
| 120 | +// Capacity returns cache capacity. |
| 121 | +func (mw *OTelMetricsMiddleware) Capacity() int { return mw.next.Capacity() } |
| 122 | + |
| 123 | +// Allocation returns allocated size. |
| 124 | +func (mw *OTelMetricsMiddleware) Allocation() int64 { return mw.next.Allocation() } |
| 125 | + |
| 126 | +// Count returns items count. |
| 127 | +func (mw *OTelMetricsMiddleware) Count(ctx context.Context) int { return mw.next.Count(ctx) } |
| 128 | + |
| 129 | +// TriggerEviction triggers eviction. |
| 130 | +func (mw *OTelMetricsMiddleware) TriggerEviction() { mw.next.TriggerEviction() } |
| 131 | + |
| 132 | +// Stop stops the underlying service. |
| 133 | +func (mw *OTelMetricsMiddleware) Stop() { mw.next.Stop() } |
| 134 | + |
| 135 | +// GetStats returns stats. |
| 136 | +func (mw *OTelMetricsMiddleware) GetStats() stats.Stats { return mw.next.GetStats() } |
| 137 | + |
| 138 | +// rec records call count and duration with attributes. |
| 139 | +// Moved to the end to satisfy funcorder linters. |
| 140 | +func (mw *OTelMetricsMiddleware) rec(ctx context.Context, method string, start time.Time, attrs ...attribute.KeyValue) { |
| 141 | + base := []attribute.KeyValue{attribute.String("method", method)} |
| 142 | + if len(attrs) > 0 { |
| 143 | + base = append(base, attrs...) |
| 144 | + } |
| 145 | + |
| 146 | + mw.calls.Add(ctx, 1, metric.WithAttributes(base...)) |
| 147 | + mw.durations.Record(ctx, float64(time.Since(start).Milliseconds()), metric.WithAttributes(base...)) |
| 148 | +} |
| 149 | + |
| 150 | +// keep helpers at end of file |
0 commit comments