-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
167 lines (137 loc) · 4.06 KB
/
plugin.go
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
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 {
writersPool sync.Pool
prop propagation.TextMapPropagator
stopCh chan struct{}
queueSize prometheus.Gauge
noFreeWorkers *prometheus.CounterVec
requestCounter *prometheus.CounterVec
requestDuration *prometheus.HistogramVec
uptime *prometheus.CounterVec
}
func (p *Plugin) Init() error {
p.writersPool = sync.Pool{
New: func() any {
wr := new(writer)
wr.code = -1
return wr
},
}
p.stopCh = make(chan struct{}, 1)
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.",
},
[]string{"status"},
)
p.uptime = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "uptime_seconds",
Help: "Uptime in seconds",
}, nil)
p.prop = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, jprop.Jaeger{})
return nil
}
func (p *Plugin) Serve() chan error {
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 make(chan error, 1)
}
func (p *Plugin) Stop(context.Context) error {
p.stopCh <- struct{}{}
return nil
}
func (p *Plugin) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
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)
}
start := time.Now()
// overwrite original rw, because we need to delete sensitive rr_newrelic headers
rrWriter := p.getWriter(w)
defer p.putWriter(rrWriter)
p.queueSize.Inc()
next.ServeHTTP(rrWriter, r)
if w.Header().Get(noWorkers) == trueStr {
p.noFreeWorkers.With(nil).Inc()
}
p.requestCounter.With(prometheus.Labels{
"status": strconv.Itoa(rrWriter.code),
}).Inc()
p.requestDuration.With(prometheus.Labels{
"status": strconv.Itoa(rrWriter.code),
}).Observe(time.Since(start).Seconds())
p.queueSize.Dec()
})
}
func (p *Plugin) Name() string {
return pluginName
}
func (p *Plugin) MetricsCollector() []prometheus.Collector {
return []prometheus.Collector{p.requestCounter, p.requestDuration, p.queueSize, p.noFreeWorkers, p.uptime}
}
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.code = -1
w.w = nil
p.writersPool.Put(w)
}