|
| 1 | +/* |
| 2 | + * Flow Playground |
| 3 | + * |
| 4 | + * Copyright 2019 Dapper Labs, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package telemetry |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/99designs/gqlgen/graphql" |
| 26 | + "github.com/prometheus/client_golang/prometheus" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + existStatusFailure = "failure" |
| 31 | + exitStatusSuccess = "success" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + registered bool |
| 36 | + requestStartedCounter prometheus.Counter |
| 37 | + requestCompletedCounter prometheus.Counter |
| 38 | + resolverStartedCounter *prometheus.CounterVec |
| 39 | + resolverCompletedCounter *prometheus.CounterVec |
| 40 | + timeToResolveField *prometheus.HistogramVec |
| 41 | + timeToHandleRequest *prometheus.HistogramVec |
| 42 | + ServerErrorCounter prometheus.Counter |
| 43 | + UserErrorCounter prometheus.Counter |
| 44 | +) |
| 45 | + |
| 46 | +type ( |
| 47 | + RequestsMetrics struct{} |
| 48 | +) |
| 49 | + |
| 50 | +var _ interface { |
| 51 | + graphql.HandlerExtension |
| 52 | + graphql.OperationInterceptor |
| 53 | + graphql.ResponseInterceptor |
| 54 | + graphql.FieldInterceptor |
| 55 | +} = RequestsMetrics{} |
| 56 | + |
| 57 | +func NewMetrics() graphql.HandlerExtension { |
| 58 | + RegisterMetrics() |
| 59 | + return RequestsMetrics{} |
| 60 | +} |
| 61 | + |
| 62 | +func RegisterMetrics() { |
| 63 | + if !registered { |
| 64 | + RegisterOn(prometheus.DefaultRegisterer) |
| 65 | + registered = true |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func RegisterOn(registerer prometheus.Registerer) { |
| 70 | + requestStartedCounter = prometheus.NewCounter( |
| 71 | + prometheus.CounterOpts{ |
| 72 | + Name: "graphql_request_started_total", |
| 73 | + Help: "Total number of requests started on the graphql server.", |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + requestCompletedCounter = prometheus.NewCounter( |
| 78 | + prometheus.CounterOpts{ |
| 79 | + Name: "graphql_request_completed_total", |
| 80 | + Help: "Total number of requests completed on the graphql server.", |
| 81 | + }, |
| 82 | + ) |
| 83 | + |
| 84 | + resolverStartedCounter = prometheus.NewCounterVec( |
| 85 | + prometheus.CounterOpts{ |
| 86 | + Name: "graphql_resolver_started_total", |
| 87 | + Help: "Total number of resolver started on the graphql server.", |
| 88 | + }, |
| 89 | + []string{"object", "field"}, |
| 90 | + ) |
| 91 | + |
| 92 | + resolverCompletedCounter = prometheus.NewCounterVec( |
| 93 | + prometheus.CounterOpts{ |
| 94 | + Name: "graphql_resolver_completed_total", |
| 95 | + Help: "Total number of resolver completed on the graphql server.", |
| 96 | + }, |
| 97 | + []string{"object", "field"}, |
| 98 | + ) |
| 99 | + |
| 100 | + timeToResolveField = prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 101 | + Name: "graphql_resolver_duration_ms", |
| 102 | + Help: "The time taken to resolve a field by graphql server.", |
| 103 | + Buckets: prometheus.ExponentialBuckets(1, 2, 11), |
| 104 | + }, []string{"exitStatus", "object", "field"}) |
| 105 | + |
| 106 | + timeToHandleRequest = prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 107 | + Name: "graphql_request_duration_ms", |
| 108 | + Help: "The time taken to handle a request by graphql server.", |
| 109 | + Buckets: prometheus.ExponentialBuckets(1, 2, 11), |
| 110 | + }, []string{"exitStatus"}) |
| 111 | + |
| 112 | + ServerErrorCounter = prometheus.NewCounter( |
| 113 | + prometheus.CounterOpts{ |
| 114 | + Name: "server_error_total", |
| 115 | + Help: "Total number of internal server errors.", |
| 116 | + }, |
| 117 | + ) |
| 118 | + |
| 119 | + UserErrorCounter = prometheus.NewCounter( |
| 120 | + prometheus.CounterOpts{ |
| 121 | + Name: "user_error_total", |
| 122 | + Help: "Total number of bad requests from users.", |
| 123 | + }, |
| 124 | + ) |
| 125 | + |
| 126 | + registerer.MustRegister( |
| 127 | + requestStartedCounter, |
| 128 | + requestCompletedCounter, |
| 129 | + resolverStartedCounter, |
| 130 | + resolverCompletedCounter, |
| 131 | + timeToResolveField, |
| 132 | + timeToHandleRequest, |
| 133 | + ServerErrorCounter, |
| 134 | + UserErrorCounter, |
| 135 | + ) |
| 136 | +} |
| 137 | + |
| 138 | +func UnRegisterMetrics() { |
| 139 | + if registered { |
| 140 | + UnRegisterFrom(prometheus.DefaultRegisterer) |
| 141 | + registered = false |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func UnRegisterFrom(registerer prometheus.Registerer) { |
| 146 | + registerer.Unregister(requestStartedCounter) |
| 147 | + registerer.Unregister(requestCompletedCounter) |
| 148 | + registerer.Unregister(resolverStartedCounter) |
| 149 | + registerer.Unregister(resolverCompletedCounter) |
| 150 | + registerer.Unregister(timeToResolveField) |
| 151 | + registerer.Unregister(timeToHandleRequest) |
| 152 | + registerer.Unregister(ServerErrorCounter) |
| 153 | + registerer.Unregister(UserErrorCounter) |
| 154 | +} |
| 155 | + |
| 156 | +func (a RequestsMetrics) ExtensionName() string { |
| 157 | + return "Prometheus" |
| 158 | +} |
| 159 | + |
| 160 | +func (a RequestsMetrics) Validate(schema graphql.ExecutableSchema) error { |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func (a RequestsMetrics) InterceptOperation(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler { |
| 165 | + requestStartedCounter.Inc() |
| 166 | + return next(ctx) |
| 167 | +} |
| 168 | + |
| 169 | +func (a RequestsMetrics) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response { |
| 170 | + errList := graphql.GetErrors(ctx) |
| 171 | + |
| 172 | + var exitStatus string |
| 173 | + if len(errList) > 0 { |
| 174 | + exitStatus = existStatusFailure |
| 175 | + } else { |
| 176 | + exitStatus = exitStatusSuccess |
| 177 | + } |
| 178 | + |
| 179 | + oc := graphql.GetOperationContext(ctx) |
| 180 | + observerStart := oc.Stats.OperationStart |
| 181 | + |
| 182 | + timeToHandleRequest.With(prometheus.Labels{"exitStatus": exitStatus}). |
| 183 | + Observe(float64(time.Since(observerStart).Nanoseconds() / int64(time.Millisecond))) |
| 184 | + |
| 185 | + requestCompletedCounter.Inc() |
| 186 | + |
| 187 | + return next(ctx) |
| 188 | +} |
| 189 | + |
| 190 | +func (a RequestsMetrics) InterceptField(ctx context.Context, next graphql.Resolver) (interface{}, error) { |
| 191 | + fc := graphql.GetFieldContext(ctx) |
| 192 | + |
| 193 | + resolverStartedCounter.WithLabelValues(fc.Object, fc.Field.Name).Inc() |
| 194 | + |
| 195 | + observerStart := time.Now() |
| 196 | + |
| 197 | + res, err := next(ctx) |
| 198 | + |
| 199 | + var exitStatus string |
| 200 | + if err != nil { |
| 201 | + exitStatus = existStatusFailure |
| 202 | + } else { |
| 203 | + exitStatus = exitStatusSuccess |
| 204 | + } |
| 205 | + |
| 206 | + timeToResolveField.WithLabelValues(exitStatus, fc.Object, fc.Field.Name). |
| 207 | + Observe(float64(time.Since(observerStart).Nanoseconds() / int64(time.Millisecond))) |
| 208 | + |
| 209 | + resolverCompletedCounter.WithLabelValues(fc.Object, fc.Field.Name).Inc() |
| 210 | + |
| 211 | + return res, err |
| 212 | +} |
0 commit comments