1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
package metrics
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Metrics holds all Prometheus metrics for the relay.
type Metrics struct {
// Request metrics
requestsTotal *prometheus.CounterVec
requestDuration *prometheus.HistogramVec
requestSizeBytes *prometheus.HistogramVec
responseSizeBytes *prometheus.HistogramVec
// Connection metrics
activeConnections prometheus.Gauge
activeSubscriptions prometheus.Gauge
connectionsTotal prometheus.Counter
// Auth metrics
authAttemptsTotal *prometheus.CounterVec
rateLimitHitsTotal *prometheus.CounterVec
// Storage metrics
eventsTotal prometheus.Gauge
dbSizeBytes prometheus.Gauge
eventDeletionsTotal prometheus.Counter
// Config
config *Config
}
// Config configures the metrics.
type Config struct {
// Namespace is the Prometheus namespace (e.g., "muxstr")
Namespace string
// Subsystem is the Prometheus subsystem (e.g., "relay")
Subsystem string
// Buckets for latency histogram (in seconds)
LatencyBuckets []float64
// Buckets for size histograms (in bytes)
SizeBuckets []float64
}
// DefaultConfig returns default metrics configuration.
func DefaultConfig() *Config {
return &Config{
Namespace: "muxstr",
Subsystem: "relay",
LatencyBuckets: []float64{
0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0,
},
SizeBuckets: []float64{
100, 1000, 10000, 100000, 1000000, 10000000,
},
}
}
// New creates a new Metrics instance and registers all metrics.
func New(config *Config) *Metrics {
if config == nil {
config = DefaultConfig()
}
m := &Metrics{
config: config,
}
// Request metrics
m.requestsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "requests_total",
Help: "Total number of requests by method and status",
},
[]string{"method", "status"},
)
m.requestDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "request_duration_seconds",
Help: "Request latency distribution in seconds",
Buckets: config.LatencyBuckets,
},
[]string{"method"},
)
m.requestSizeBytes = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "request_size_bytes",
Help: "Request size distribution in bytes",
Buckets: config.SizeBuckets,
},
[]string{"method"},
)
m.responseSizeBytes = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "response_size_bytes",
Help: "Response size distribution in bytes",
Buckets: config.SizeBuckets,
},
[]string{"method"},
)
// Connection metrics
m.activeConnections = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "active_connections",
Help: "Current number of active gRPC connections",
},
)
m.activeSubscriptions = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "active_subscriptions",
Help: "Current number of active subscriptions",
},
)
m.connectionsTotal = promauto.NewCounter(
prometheus.CounterOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "connections_total",
Help: "Total number of connections since startup",
},
)
// Auth metrics
m.authAttemptsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "auth_attempts_total",
Help: "Total authentication attempts by result",
},
[]string{"result"},
)
m.rateLimitHitsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "rate_limit_hits_total",
Help: "Total rate limit rejections",
},
[]string{"authenticated"},
)
// Storage metrics
m.eventsTotal = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "events_total",
Help: "Total events stored in database",
},
)
m.dbSizeBytes = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "db_size_bytes",
Help: "Database file size in bytes",
},
)
m.eventDeletionsTotal = promauto.NewCounter(
prometheus.CounterOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "event_deletions_total",
Help: "Total events deleted (NIP-09)",
},
)
return m
}
// RecordRequest records a completed request with its status and duration.
func (m *Metrics) RecordRequest(method, status string, durationSeconds float64) {
m.requestsTotal.WithLabelValues(method, status).Inc()
m.requestDuration.WithLabelValues(method).Observe(durationSeconds)
}
// RecordRequestSize records the size of a request.
func (m *Metrics) RecordRequestSize(method string, sizeBytes int) {
m.requestSizeBytes.WithLabelValues(method).Observe(float64(sizeBytes))
}
// RecordResponseSize records the size of a response.
func (m *Metrics) RecordResponseSize(method string, sizeBytes int) {
m.responseSizeBytes.WithLabelValues(method).Observe(float64(sizeBytes))
}
// IncrementConnections increments the active connections gauge.
func (m *Metrics) IncrementConnections() {
m.activeConnections.Inc()
m.connectionsTotal.Inc()
}
// DecrementConnections decrements the active connections gauge.
func (m *Metrics) DecrementConnections() {
m.activeConnections.Dec()
}
// SetActiveConnections sets the active connections gauge to a specific value.
func (m *Metrics) SetActiveConnections(count int) {
m.activeConnections.Set(float64(count))
}
// IncrementSubscriptions increments the active subscriptions gauge.
func (m *Metrics) IncrementSubscriptions() {
m.activeSubscriptions.Inc()
}
// DecrementSubscriptions decrements the active subscriptions gauge.
func (m *Metrics) DecrementSubscriptions() {
m.activeSubscriptions.Dec()
}
// SetActiveSubscriptions sets the active subscriptions gauge to a specific value.
func (m *Metrics) SetActiveSubscriptions(count int) {
m.activeSubscriptions.Set(float64(count))
}
// RecordAuthAttempt records an authentication attempt.
func (m *Metrics) RecordAuthAttempt(success bool) {
result := "failure"
if success {
result = "success"
}
m.authAttemptsTotal.WithLabelValues(result).Inc()
}
// RecordRateLimitHit records a rate limit rejection.
func (m *Metrics) RecordRateLimitHit(authenticated bool) {
auth := "false"
if authenticated {
auth = "true"
}
m.rateLimitHitsTotal.WithLabelValues(auth).Inc()
}
// UpdateStorageStats updates storage-related metrics.
func (m *Metrics) UpdateStorageStats(eventCount int64, dbSizeBytes int64) {
m.eventsTotal.Set(float64(eventCount))
m.dbSizeBytes.Set(float64(dbSizeBytes))
}
// RecordEventDeletion records an event deletion.
func (m *Metrics) RecordEventDeletion() {
m.eventDeletionsTotal.Inc()
}
// RequestStatus represents the status of a request for metrics.
type RequestStatus string
const (
StatusOK RequestStatus = "ok"
StatusError RequestStatus = "error"
StatusUnauthenticated RequestStatus = "unauthenticated"
StatusRateLimited RequestStatus = "rate_limited"
StatusInvalidRequest RequestStatus = "invalid_request"
)
func (m *Metrics) Serve(addr, path string) error {
mux := http.NewServeMux()
mux.Handle(path, promhttp.Handler())
return http.ListenAndServe(addr, mux)
}
|