-
Notifications
You must be signed in to change notification settings - Fork 4
/
metrics.go
61 lines (52 loc) · 1.77 KB
/
metrics.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
package httpmetrics
import (
"net/http"
"sync"
"github.com/prometheus/client_golang/prometheus"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/util"
)
var (
reqCnt *prometheus.CounterVec
reqDur *prometheus.HistogramVec
reqSize prometheus.Summary
rspSize prometheus.Summary
metricsMu sync.Mutex
)
type Metrics struct {
Key string
MetricsPath string
}
func NewMetrics(key string, logger util.Logger) *Metrics {
m := &Metrics{Key: key, MetricsPath: defaultMetricPath}
if reqCnt == nil {
metricsMu.Lock()
if reqCnt == nil {
registerHTTPMetrics(logger)
}
metricsMu.Unlock()
}
return m
}
func InjectHTTP(statusCode int, r *http.Request, span *telemetry.Span) {
span.Attributes(
&telemetry.Attribute{Key: "http.host", Value: r.Host},
&telemetry.Attribute{Key: "http.method", Value: r.Method},
&telemetry.Attribute{Key: "http.url", Value: r.URL.String()},
&telemetry.Attribute{Key: "http.scheme", Value: r.URL.Scheme},
)
if s := r.Header.Get("User-Agent"); s != "" {
span.Attribute("http.user_agent", s)
}
if s := r.Header.Get("Content-Length"); s != "" {
span.Attribute("http.request_content_length", s)
}
span.SetHTTPStatus(statusCode)
}
func registerHTTPMetrics(logger util.Logger) {
subsystem := "http"
reqCnt = telemetry.MetricsCounter(subsystem, "requests_total", "The HTTP request counts processed.", logger, "key", "code", "method")
reqDur = telemetry.MetricsHistogram(subsystem, "request_duration_seconds", "The HTTP request duration in seconds.", logger, "key", "code")
reqSize = telemetry.MetricsSummary(subsystem, "request_size_bytes", "The HTTP request sizes in bytes.", logger)
rspSize = telemetry.MetricsSummary(subsystem, "response_size_bytes", "The HTTP response sizes in bytes.", logger)
}