-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
426 lines (358 loc) · 11.4 KB
/
plugin.go
File metadata and controls
426 lines (358 loc) · 11.4 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package prometheus
import (
"context"
"net/http"
"strconv"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
rrcontext "github.com/roadrunner-server/context"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
jprop "go.opentelemetry.io/contrib/propagators/jaeger"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/trace"
)
const (
pluginName string = "http_metrics"
namespace string = "rr_http"
// should be in sync with the http/handler.go constants
noWorkers string = "No-Workers"
trueStr string = "true"
)
type Plugin struct {
// Core components
writersPool sync.Pool
prop propagation.TextMapPropagator
stopCh chan struct{}
// Configuration
cfg Configurer
config *Config
endpointMatcher *EndpointMatcher
// Existing metrics
queueSize prometheus.Gauge
noFreeWorkers *prometheus.CounterVec
requestCounter *prometheus.CounterVec
requestDuration *prometheus.HistogramVec
uptime *prometheus.CounterVec
// NEW: Phase 1 metrics - Performance breakdown
queueTime *prometheus.HistogramVec
processingTime *prometheus.HistogramVec
// NEW: Phase 1 metrics - Request/Response sizes
requestSize *prometheus.HistogramVec
responseSize *prometheus.HistogramVec
// NEW: Phase 1 metrics - Endpoint-level tracking
requestsByEndpoint *prometheus.CounterVec
durationByEndpoint *prometheus.HistogramVec
// NEW: Phase 1 metrics - Error classification
errorsByType *prometheus.CounterVec
// NEW: Phase 1 metrics - Worker pool health
activeWorkers prometheus.Gauge
idleWorkers prometheus.Gauge
workerUtilization prometheus.Gauge
}
func (p *Plugin) Init(cfg Configurer) error {
// Initialize default configuration
p.config = DefaultConfig()
// Store configurer for potential future use
p.cfg = cfg
// Try to load configuration from http_metrics section
if cfg != nil && cfg.Has(configKey) {
if err := cfg.UnmarshalKey(configKey, p.config); err != nil {
return err
}
}
// Initialize writers pool
p.writersPool = sync.Pool{
New: func() any {
wr := new(writer)
wr.code = -1
return wr
},
}
p.stopCh = make(chan struct{}, 1)
// Initialize endpoint matcher
var err error
p.endpointMatcher, err = NewEndpointMatcher(p.config.EndpointPatterns)
if err != nil {
return err
}
// Initialize existing metrics
p.queueSize = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "requests_queue",
Help: "Total number of queued requests.",
})
p.noFreeWorkers = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "no_free_workers_total",
Help: "Total number of NoFreeWorkers occurrences.",
}, nil)
p.requestCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "request_total",
Help: "Total number of handled http requests after server restart.",
}, []string{"status"})
p.requestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Name: "request_duration_seconds",
Help: "HTTP request duration.",
Buckets: p.config.DurationBuckets,
},
[]string{"status"},
)
p.uptime = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "uptime_seconds",
Help: "Uptime in seconds",
}, nil)
// Initialize NEW metrics - Performance breakdown
if p.config.CollectQueueTime {
p.queueTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Name: "queue_time_seconds",
Help: "Time request spent waiting in queue before being picked up by a worker.",
Buckets: p.config.DurationBuckets,
},
[]string{"method", "endpoint"},
)
p.processingTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Name: "processing_time_seconds",
Help: "Time spent processing the request by PHP worker.",
Buckets: p.config.DurationBuckets,
},
[]string{"method", "endpoint"},
)
}
// Initialize NEW metrics - Request/Response sizes
if p.config.CollectSizes {
p.requestSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Name: "request_size_bytes",
Help: "HTTP request body size in bytes.",
Buckets: p.config.SizeBuckets,
},
[]string{"method", "endpoint"},
)
p.responseSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Name: "response_size_bytes",
Help: "HTTP response body size in bytes.",
Buckets: p.config.SizeBuckets,
},
[]string{"method", "endpoint", "status"},
)
}
// Initialize NEW metrics - Endpoint-level tracking
p.requestsByEndpoint = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "requests_by_endpoint_total",
Help: "Total number of HTTP requests by endpoint pattern.",
},
[]string{"method", "endpoint", "status"},
)
p.durationByEndpoint = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Name: "duration_by_endpoint_seconds",
Help: "HTTP request duration by endpoint pattern.",
Buckets: p.config.DurationBuckets,
},
[]string{"method", "endpoint"},
)
// Initialize NEW metrics - Error classification
p.errorsByType = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "errors_total",
Help: "Total number of HTTP errors classified by type.",
},
[]string{"type", "endpoint", "status"},
)
// Initialize NEW metrics - Worker pool health
if p.config.CollectWorkerInfo {
p.activeWorkers = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "active_workers",
Help: "Number of workers currently processing requests.",
})
p.idleWorkers = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "idle_workers",
Help: "Number of idle workers available to process requests.",
})
p.workerUtilization = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "worker_utilization_percent",
Help: "Worker pool utilization percentage (0-100).",
})
}
p.prop = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, jprop.Jaeger{})
return nil
}
func (p *Plugin) Serve() chan error {
errCh := make(chan error, 1)
// Existing uptime ticker
go func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-p.stopCh:
return
case <-ticker.C:
p.uptime.With(nil).Inc()
}
}
}()
return errCh
}
func (p *Plugin) Stop(context.Context) error {
close(p.stopCh)
return nil
}
func (p *Plugin) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Handle OpenTelemetry tracing (existing logic)
if val, ok := r.Context().Value(rrcontext.OtelTracerNameKey).(string); ok {
tp := trace.SpanFromContext(r.Context()).TracerProvider()
ctx, span := tp.Tracer(val, trace.WithSchemaURL(semconv.SchemaURL),
trace.WithInstrumentationVersion(otelhttp.Version())).
Start(r.Context(), pluginName, trace.WithSpanKind(trace.SpanKindServer))
defer span.End()
// inject
p.prop.Inject(ctx, propagation.HeaderCarrier(r.Header))
r = r.WithContext(ctx)
}
// Record arrival time
arrivalTime := time.Now()
// Get writer from pool and initialize timing
rrWriter := p.getWriter(w)
defer p.putWriter(rrWriter)
rrWriter.arrivalTime = arrivalTime
rrWriter.queueStart = time.Now()
rrWriter.requestSize = r.ContentLength
// Track queue size
p.queueSize.Inc()
// Processing starts when worker picks up request
rrWriter.processStart = time.Now()
// Execute request
next.ServeHTTP(rrWriter, r)
processEnd := time.Now()
// Extract request metadata
endpoint := "other"
if p.config.EndpointPatterns.Enabled {
endpoint = p.endpointMatcher.Match(r.URL.Path)
}
method := r.Method
status := strconv.Itoa(rrWriter.code)
// Calculate timings
queueTime := rrWriter.processStart.Sub(rrWriter.queueStart)
processingTime := processEnd.Sub(rrWriter.processStart)
totalTime := processEnd.Sub(rrWriter.arrivalTime)
// Create label sets for metrics
endpointLabels := prometheus.Labels{
"method": method,
"endpoint": endpoint,
}
fullLabels := prometheus.Labels{
"method": method,
"endpoint": endpoint,
"status": status,
}
// Record existing metrics
p.requestCounter.With(prometheus.Labels{"status": status}).Inc()
p.requestDuration.With(prometheus.Labels{"status": status}).Observe(totalTime.Seconds())
// Record NEW metrics - Performance breakdown
if p.config.CollectQueueTime {
p.queueTime.With(endpointLabels).Observe(queueTime.Seconds())
p.processingTime.With(endpointLabels).Observe(processingTime.Seconds())
}
// Record NEW metrics - Request/Response sizes
if p.config.CollectSizes {
if rrWriter.requestSize > 0 {
p.requestSize.With(endpointLabels).Observe(float64(rrWriter.requestSize))
}
if rrWriter.bytesWritten > 0 {
p.responseSize.With(fullLabels).Observe(float64(rrWriter.bytesWritten))
}
}
// Record NEW metrics - Endpoint-level tracking
p.requestsByEndpoint.With(fullLabels).Inc()
p.durationByEndpoint.With(endpointLabels).Observe(totalTime.Seconds())
// Record NEW metrics - Error classification
if isErrorStatus(rrWriter.code) {
errorType := string(classifyError(rrWriter.code, w.Header()))
p.errorsByType.With(prometheus.Labels{
"type": errorType,
"endpoint": endpoint,
"status": status,
}).Inc()
}
// Handle no workers case (existing logic)
if w.Header().Get(noWorkers) == trueStr {
p.noFreeWorkers.With(nil).Inc()
}
p.queueSize.Dec()
})
}
func (p *Plugin) Name() string {
return pluginName
}
func (p *Plugin) MetricsCollector() []prometheus.Collector {
collectors := []prometheus.Collector{
// Existing metrics
p.requestCounter,
p.requestDuration,
p.queueSize,
p.noFreeWorkers,
p.uptime,
// NEW: Endpoint-level metrics (always enabled)
p.requestsByEndpoint,
p.durationByEndpoint,
p.errorsByType,
}
// Add conditional metrics
if p.config.CollectQueueTime {
collectors = append(collectors, p.queueTime, p.processingTime)
}
if p.config.CollectSizes {
collectors = append(collectors, p.requestSize, p.responseSize)
}
if p.config.CollectWorkerInfo {
collectors = append(collectors, p.activeWorkers, p.idleWorkers, p.workerUtilization)
}
return collectors
}
func (p *Plugin) getWriter(w http.ResponseWriter) *writer {
wr := p.writersPool.Get().(*writer)
wr.w = w
return wr
}
func (p *Plugin) putWriter(w *writer) {
w.reset()
p.writersPool.Put(w)
}
// updateWorkerMetrics queries worker pool state and updates metrics
// NOTE: This requires integration with HTTP plugin's worker pool
// Implementation depends on RoadRunner's internal API for accessing worker pool state
func (p *Plugin) updateWorkerMetrics() {
// TODO: Implement worker pool state access
// This requires extending the HTTP plugin to expose worker pool statistics
// or using RoadRunner's internal interfaces to query worker state
//
// Example pseudo-code:
// stats := p.getWorkerPoolStats()
// if stats != nil {
// p.activeWorkers.Set(float64(stats.Active))
// p.idleWorkers.Set(float64(stats.Idle))
// p.workerUtilization.Set(stats.Utilization)
// }
}