-
Notifications
You must be signed in to change notification settings - Fork 34
/
prometheus.go
162 lines (129 loc) · 4.37 KB
/
prometheus.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
package prometheus
import (
"context"
"time"
"github.com/99designs/gqlgen/graphql"
prometheusclient "github.com/prometheus/client_golang/prometheus"
)
const (
existStatusFailure = "failure"
exitStatusSuccess = "success"
)
var (
requestStartedCounter prometheusclient.Counter
requestCompletedCounter prometheusclient.Counter
resolverStartedCounter *prometheusclient.CounterVec
resolverCompletedCounter *prometheusclient.CounterVec
timeToResolveField *prometheusclient.HistogramVec
timeToHandleRequest *prometheusclient.HistogramVec
)
type (
Tracer struct{}
)
var _ interface {
graphql.HandlerExtension
graphql.OperationInterceptor
graphql.ResponseInterceptor
graphql.FieldInterceptor
} = Tracer{}
func Register() {
RegisterOn(prometheusclient.DefaultRegisterer)
}
func RegisterOn(registerer prometheusclient.Registerer) {
requestStartedCounter = prometheusclient.NewCounter(
prometheusclient.CounterOpts{
Name: "graphql_request_started_total",
Help: "Total number of requests started on the graphql server.",
},
)
requestCompletedCounter = prometheusclient.NewCounter(
prometheusclient.CounterOpts{
Name: "graphql_request_completed_total",
Help: "Total number of requests completed on the graphql server.",
},
)
resolverStartedCounter = prometheusclient.NewCounterVec(
prometheusclient.CounterOpts{
Name: "graphql_resolver_started_total",
Help: "Total number of resolver started on the graphql server.",
},
[]string{"object", "field"},
)
resolverCompletedCounter = prometheusclient.NewCounterVec(
prometheusclient.CounterOpts{
Name: "graphql_resolver_completed_total",
Help: "Total number of resolver completed on the graphql server.",
},
[]string{"object", "field"},
)
timeToResolveField = prometheusclient.NewHistogramVec(prometheusclient.HistogramOpts{
Name: "graphql_resolver_duration_ms",
Help: "The time taken to resolve a field by graphql server.",
Buckets: prometheusclient.ExponentialBuckets(1, 2, 11),
}, []string{"exitStatus", "object", "field"})
timeToHandleRequest = prometheusclient.NewHistogramVec(prometheusclient.HistogramOpts{
Name: "graphql_request_duration_ms",
Help: "The time taken to handle a request by graphql server.",
Buckets: prometheusclient.ExponentialBuckets(1, 2, 11),
}, []string{"exitStatus"})
registerer.MustRegister(
requestStartedCounter,
requestCompletedCounter,
resolverStartedCounter,
resolverCompletedCounter,
timeToResolveField,
timeToHandleRequest,
)
}
func UnRegister() {
UnRegisterFrom(prometheusclient.DefaultRegisterer)
}
func UnRegisterFrom(registerer prometheusclient.Registerer) {
registerer.Unregister(requestStartedCounter)
registerer.Unregister(requestCompletedCounter)
registerer.Unregister(resolverStartedCounter)
registerer.Unregister(resolverCompletedCounter)
registerer.Unregister(timeToResolveField)
registerer.Unregister(timeToHandleRequest)
}
func (a Tracer) ExtensionName() string {
return "Prometheus"
}
func (a Tracer) Validate(schema graphql.ExecutableSchema) error {
return nil
}
func (a Tracer) InterceptOperation(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
requestStartedCounter.Inc()
return next(ctx)
}
func (a Tracer) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
errList := graphql.GetErrors(ctx)
var exitStatus string
if len(errList) > 0 {
exitStatus = existStatusFailure
} else {
exitStatus = exitStatusSuccess
}
oc := graphql.GetOperationContext(ctx)
observerStart := oc.Stats.OperationStart
timeToHandleRequest.With(prometheusclient.Labels{"exitStatus": exitStatus}).
Observe(float64(time.Since(observerStart).Nanoseconds() / int64(time.Millisecond)))
requestCompletedCounter.Inc()
return next(ctx)
}
func (a Tracer) InterceptField(ctx context.Context, next graphql.Resolver) (interface{}, error) {
fc := graphql.GetFieldContext(ctx)
resolverStartedCounter.WithLabelValues(fc.Object, fc.Field.Name).Inc()
observerStart := time.Now()
res, err := next(ctx)
var exitStatus string
if err != nil {
exitStatus = existStatusFailure
} else {
exitStatus = exitStatusSuccess
}
timeToResolveField.WithLabelValues(exitStatus, fc.Object, fc.Field.Name).
Observe(float64(time.Since(observerStart).Nanoseconds() / int64(time.Millisecond)))
resolverCompletedCounter.WithLabelValues(fc.Object, fc.Field.Name).Inc()
return res, err
}