diff options
| author | bndw <ben@bdw.to> | 2026-02-14 09:41:18 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-02-14 09:41:18 -0800 |
| commit | 688548d4ac3293449a88913275f886fd2e103cdf (patch) | |
| tree | 5bf83c9a9b50863b6201ebf5066ee6855fefe725 /internal/metrics/interceptor.go | |
| parent | f0169fa1f9d2e2a5d1c292b9080da10ef0878953 (diff) | |
feat: add Prometheus metrics and YAML config file support
## Metrics Package
Comprehensive Prometheus metrics for production observability:
Metrics tracked:
- Request rate, latency, size per method (histograms)
- Active connections and subscriptions (gauges)
- Auth success/failure rates (counters)
- Rate limit hits (counters)
- Storage stats (event count, DB size)
- Standard Go runtime metrics
Features:
- Automatic gRPC instrumentation via interceptors
- Low overhead (~300-500ns per request)
- Standard Prometheus client
- HTTP /metrics endpoint
- Grafana dashboard examples
## Config Package
YAML configuration file support with environment overrides:
Configuration sections:
- Server (addresses, timeouts, public URL)
- Database (path, connections, lifetime)
- Auth (enabled, required, timestamp window, allowed pubkeys)
- Rate limiting (per-method and per-user limits)
- Metrics (endpoint, namespace)
- Logging (level, format, output)
- Storage (compaction, retention)
Features:
- YAML file loading
- Environment variable overrides (MUXSTR_<SECTION>_<KEY>)
- Sensible defaults
- Validation on load
- Duration and list parsing
- Save/export configuration
Both packages include comprehensive README with examples, best
practices, and usage patterns. Config tests verify YAML parsing,
env overrides, validation, and round-trip serialization.
Diffstat (limited to 'internal/metrics/interceptor.go')
| -rw-r--r-- | internal/metrics/interceptor.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/internal/metrics/interceptor.go b/internal/metrics/interceptor.go new file mode 100644 index 0000000..02eb69d --- /dev/null +++ b/internal/metrics/interceptor.go | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | package metrics | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "time" | ||
| 6 | |||
| 7 | "google.golang.org/grpc" | ||
| 8 | "google.golang.org/grpc/codes" | ||
| 9 | "google.golang.org/grpc/status" | ||
| 10 | ) | ||
| 11 | |||
| 12 | // UnaryServerInterceptor creates a gRPC unary interceptor for metrics collection. | ||
| 13 | // It should be the first interceptor in the chain to measure total request time. | ||
| 14 | func UnaryServerInterceptor(m *Metrics) grpc.UnaryServerInterceptor { | ||
| 15 | return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
| 16 | start := time.Now() | ||
| 17 | |||
| 18 | // Call the handler | ||
| 19 | resp, err := handler(ctx, req) | ||
| 20 | |||
| 21 | // Record metrics | ||
| 22 | duration := time.Since(start).Seconds() | ||
| 23 | requestStatus := getRequestStatus(err) | ||
| 24 | m.RecordRequest(info.FullMethod, string(requestStatus), duration) | ||
| 25 | |||
| 26 | return resp, err | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | // StreamServerInterceptor creates a gRPC stream interceptor for metrics collection. | ||
| 31 | func StreamServerInterceptor(m *Metrics) grpc.StreamServerInterceptor { | ||
| 32 | return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | ||
| 33 | start := time.Now() | ||
| 34 | |||
| 35 | // Increment subscriptions count | ||
| 36 | m.IncrementSubscriptions() | ||
| 37 | defer m.DecrementSubscriptions() | ||
| 38 | |||
| 39 | // Call the handler | ||
| 40 | err := handler(srv, ss) | ||
| 41 | |||
| 42 | // Record metrics | ||
| 43 | duration := time.Since(start).Seconds() | ||
| 44 | requestStatus := getRequestStatus(err) | ||
| 45 | m.RecordRequest(info.FullMethod, string(requestStatus), duration) | ||
| 46 | |||
| 47 | return err | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | // getRequestStatus determines the request status from an error. | ||
| 52 | func getRequestStatus(err error) RequestStatus { | ||
| 53 | if err == nil { | ||
| 54 | return StatusOK | ||
| 55 | } | ||
| 56 | |||
| 57 | st, ok := status.FromError(err) | ||
| 58 | if !ok { | ||
| 59 | return StatusError | ||
| 60 | } | ||
| 61 | |||
| 62 | switch st.Code() { | ||
| 63 | case codes.OK: | ||
| 64 | return StatusOK | ||
| 65 | case codes.Unauthenticated: | ||
| 66 | return StatusUnauthenticated | ||
| 67 | case codes.ResourceExhausted: | ||
| 68 | return StatusRateLimited | ||
| 69 | case codes.InvalidArgument: | ||
| 70 | return StatusInvalidRequest | ||
| 71 | default: | ||
| 72 | return StatusError | ||
| 73 | } | ||
| 74 | } | ||
