-
Notifications
You must be signed in to change notification settings - Fork 5
/
metrics.go
139 lines (113 loc) · 3.74 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
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
package gbox
import (
"sync"
"time"
"github.com/jensneuse/graphql-go-tools/pkg/graphql"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap"
)
var metrics = new(Metrics)
func init() { // nolint:gochecknoinits
metrics.once.Do(func() {
const ns, sub = "caddy", "http_gbox"
operationLabels := []string{"operation_type", "operation_name"}
metrics.operationInFlight = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: ns,
Subsystem: sub,
Name: "operations_in_flight",
Help: "Number of graphql operations currently handled by this server.",
}, operationLabels)
metrics.operationCount = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: ns,
Subsystem: sub,
Name: "operation_total",
Help: "Counter of graphql operations served.",
}, operationLabels)
metrics.operationDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: ns,
Subsystem: sub,
Name: "operation_duration",
Help: "Histogram of GraphQL operations execution duration.",
Buckets: prometheus.DefBuckets,
}, operationLabels)
cachingLabels := []string{"operation_name", "status"}
metrics.cachingCount = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: ns,
Subsystem: sub,
Name: "caching_total",
Help: "Counter of graphql query operations caching statues.",
}, cachingLabels)
})
}
type Metrics struct {
once sync.Once
operationInFlight *prometheus.GaugeVec
operationCount *prometheus.CounterVec
operationDuration *prometheus.HistogramVec
cachingCount *prometheus.CounterVec
}
type cachingMetrics interface {
addMetricsCaching(*graphql.Request, CachingStatus)
}
func (h *Handler) addMetricsBeginRequest(request *graphql.Request) {
labels, err := h.metricsOperationLabels(request)
if err != nil {
h.logger.Warn("fail to get metrics operation labels", zap.Error(err))
return
}
h.metrics.operationCount.With(labels).Inc()
h.metrics.operationInFlight.With(labels).Inc()
}
func (h *Handler) addMetricsEndRequest(request *graphql.Request, d time.Duration) {
labels, err := h.metricsOperationLabels(request)
if err != nil {
h.logger.Warn("fail to get metrics operation labels", zap.Error(err))
return
}
h.metrics.operationInFlight.With(labels).Dec()
h.metrics.operationDuration.With(labels).Observe(d.Seconds())
}
func (h *Handler) addMetricsCaching(request *graphql.Request, status CachingStatus) {
labels, err := h.metricsCachingLabels(request, status)
if err != nil {
h.logger.Warn("fail to get metrics caching labels", zap.Error(err))
return
}
h.metrics.cachingCount.With(labels).Inc()
}
func (h *Handler) metricsCachingLabels(request *graphql.Request, status CachingStatus) (map[string]string, error) {
if !request.IsNormalized() {
if result, _ := request.Normalize(h.schema); !result.Successful {
return nil, result.Errors
}
}
labels := map[string]string{
"operation_name": request.OperationName,
"status": string(status),
}
return labels, nil
}
func (h *Handler) metricsOperationLabels(request *graphql.Request) (map[string]string, error) {
if !request.IsNormalized() {
if result, _ := request.Normalize(h.schema); !result.Successful {
return nil, result.Errors
}
}
labels := map[string]string{
"operation_name": request.OperationName,
}
operationType, _ := request.OperationType()
// nolint:exhaustive
switch operationType {
case graphql.OperationTypeQuery:
labels["operation_type"] = "query"
case graphql.OperationTypeMutation:
labels["operation_type"] = "mutation"
case graphql.OperationTypeSubscription:
labels["operation_type"] = "subscription"
default:
labels["operation_type"] = "unknown"
}
return labels, nil
}