/
metrics.go
77 lines (70 loc) · 3.23 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Provenance-includes-location: https://github.com/weaveworks/common/blob/main/server/metrics.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: Weaveworks Ltd.
package server
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/grafana/dskit/instrument"
"github.com/grafana/dskit/middleware"
)
type Metrics struct {
TCPConnections *prometheus.GaugeVec
TCPConnectionsLimit *prometheus.GaugeVec
RequestDuration *prometheus.HistogramVec
PerTenantRequestDuration *prometheus.HistogramVec
ReceivedMessageSize *prometheus.HistogramVec
SentMessageSize *prometheus.HistogramVec
InflightRequests *prometheus.GaugeVec
}
func NewServerMetrics(cfg Config) *Metrics {
reg := promauto.With(cfg.registererOrDefault())
return &Metrics{
TCPConnections: reg.NewGaugeVec(prometheus.GaugeOpts{
Namespace: cfg.MetricsNamespace,
Name: "tcp_connections",
Help: "Current number of accepted TCP connections.",
}, []string{"protocol"}),
TCPConnectionsLimit: reg.NewGaugeVec(prometheus.GaugeOpts{
Namespace: cfg.MetricsNamespace,
Name: "tcp_connections_limit",
Help: "The max number of TCP connections that can be accepted (0 means no limit).",
}, []string{"protocol"}),
RequestDuration: reg.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cfg.MetricsNamespace,
Name: "request_duration_seconds",
Help: "Time (in seconds) spent serving HTTP requests.",
Buckets: instrument.DefBuckets,
NativeHistogramBucketFactor: cfg.MetricsNativeHistogramFactor,
NativeHistogramMaxBucketNumber: 100,
NativeHistogramMinResetDuration: time.Hour,
}, []string{"method", "route", "status_code", "ws"}),
PerTenantRequestDuration: reg.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cfg.MetricsNamespace,
Name: "per_tenant_request_duration_seconds",
Help: "Time (in seconds) spent serving HTTP requests for a particular tenant.",
Buckets: instrument.DefBuckets,
NativeHistogramBucketFactor: cfg.MetricsNativeHistogramFactor,
NativeHistogramMaxBucketNumber: 100,
NativeHistogramMinResetDuration: time.Hour,
}, []string{"method", "route", "status_code", "ws", "tenant"}),
ReceivedMessageSize: reg.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cfg.MetricsNamespace,
Name: "request_message_bytes",
Help: "Size (in bytes) of messages received in the request.",
Buckets: middleware.BodySizeBuckets,
}, []string{"method", "route"}),
SentMessageSize: reg.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cfg.MetricsNamespace,
Name: "response_message_bytes",
Help: "Size (in bytes) of messages sent in response.",
Buckets: middleware.BodySizeBuckets,
}, []string{"method", "route"}),
InflightRequests: reg.NewGaugeVec(prometheus.GaugeOpts{
Namespace: cfg.MetricsNamespace,
Name: "inflight_requests",
Help: "Current number of inflight requests.",
}, []string{"method", "route"}),
}
}