/
graphql_prehandler.go
462 lines (363 loc) · 14.5 KB
/
graphql_prehandler.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package core
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"sync"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/wundergraph/graphql-go-tools/v2/pkg/graphqlerrors"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"github.com/wundergraph/cosmo/router/pkg/art"
"github.com/wundergraph/cosmo/router/pkg/logging"
"github.com/wundergraph/cosmo/router/pkg/otel"
rtrace "github.com/wundergraph/cosmo/router/pkg/trace"
"github.com/go-chi/chi/v5/middleware"
"go.uber.org/zap"
"github.com/wundergraph/cosmo/router/internal/pool"
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/resolve"
)
type PreHandlerOptions struct {
Logger *zap.Logger
Executor *Executor
Metrics RouterMetrics
OperationProcessor *OperationProcessor
Planner *OperationPlanner
AccessController *AccessController
OperationBlocker *OperationBlocker
DevelopmentMode bool
RouterPublicKey *ecdsa.PublicKey
EnableRequestTracing bool
TracerProvider *sdktrace.TracerProvider
FlushTelemetryAfterResponse bool
TraceExportVariables bool
}
type PreHandler struct {
log *zap.Logger
executor *Executor
metrics RouterMetrics
operationProcessor *OperationProcessor
planner *OperationPlanner
accessController *AccessController
operationBlocker *OperationBlocker
developmentMode bool
routerPublicKey *ecdsa.PublicKey
enableRequestTracing bool
tracerProvider *sdktrace.TracerProvider
flushTelemetryAfterResponse bool
tracer trace.Tracer
traceExportVariables bool
}
func NewPreHandler(opts *PreHandlerOptions) *PreHandler {
return &PreHandler{
log: opts.Logger,
executor: opts.Executor,
metrics: opts.Metrics,
operationProcessor: opts.OperationProcessor,
planner: opts.Planner,
accessController: opts.AccessController,
operationBlocker: opts.OperationBlocker,
routerPublicKey: opts.RouterPublicKey,
developmentMode: opts.DevelopmentMode,
enableRequestTracing: opts.EnableRequestTracing,
flushTelemetryAfterResponse: opts.FlushTelemetryAfterResponse,
tracerProvider: opts.TracerProvider,
traceExportVariables: opts.TraceExportVariables,
tracer: opts.TracerProvider.Tracer(
"wundergraph/cosmo/router/pre_handler",
trace.WithInstrumentationVersion("0.0.1"),
),
}
}
// Error and Status Code handling
//
// When a server receives a well-formed GraphQL-over-HTTP request, it must return a
// well‐formed GraphQL response. The server's response describes the result of validating
// and executing the requested operation if successful, and describes any errors encountered
// during the request. This means working errors should be returned as part of the response body.
// That also implies parsing or validation errors. They should be returned as part of the response body.
// Only in cases where the request is malformed or invalid GraphQL should the server return an HTTP 4xx or 5xx error code.
// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#response
func (h *PreHandler) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestLogger := h.log.With(logging.WithRequestID(middleware.GetReqID(r.Context())))
var (
// In GraphQL the statusCode does not always express the error state of the request
// we use this flag to determine if we have an error for the request metrics
finalErr error
writtenBytes int
statusCode = http.StatusOK
traceOptions = resolve.TraceOptions{}
)
routerSpan := trace.SpanFromContext(r.Context())
clientInfo := NewClientInfoFromRequest(r)
baseAttributeValues := []attribute.KeyValue{
otel.WgClientName.String(clientInfo.Name),
otel.WgClientVersion.String(clientInfo.Version),
otel.WgOperationProtocol.String(OperationProtocolHTTP.String()),
}
if h.enableRequestTracing {
if clientInfo.WGRequestToken != "" && h.routerPublicKey != nil {
_, err := jwt.Parse(clientInfo.WGRequestToken, func(token *jwt.Token) (interface{}, error) {
return h.routerPublicKey, nil
}, jwt.WithValidMethods([]string{jwt.SigningMethodES256.Name}))
if err != nil {
err := errors.New("invalid request token. Router version 0.42.1 or above is required to use request tracing in production")
finalErr = err
requestLogger.Error(fmt.Sprintf("failed to parse request token: %s", err.Error()))
writeRequestErrors(r, w, http.StatusForbidden, graphqlerrors.RequestErrorsFromError(err), requestLogger)
return
}
// Enable ART after successful request token validation
traceOptions = ParseRequestTraceOptions(r)
} else if h.developmentMode {
// In development, without request signing, we enable ART
traceOptions = ParseRequestTraceOptions(r)
} else {
// In production, without request signing, we disable ART because it's not safe to use
traceOptions.DisableAll()
}
}
if traceOptions.Enable {
r = r.WithContext(resolve.SetTraceStart(r.Context(), traceOptions.EnablePredictableDebugTimings))
}
traceTimings := art.NewTraceTimings(r.Context())
metrics := h.metrics.StartOperation(clientInfo, requestLogger, r.ContentLength)
routerSpan.SetAttributes(baseAttributeValues...)
metrics.AddAttributes(baseAttributeValues...)
if h.flushTelemetryAfterResponse {
defer h.flushMetrics(r.Context(), requestLogger)
}
defer func() {
metrics.Finish(finalErr, statusCode, writtenBytes)
}()
// XXX: This buffer needs to be returned to the pool only
// AFTER we're done with body (retrieved from parser.ReadBody())
buf := pool.GetBytesBuffer()
defer pool.PutBytesBuffer(buf)
body, err := h.operationProcessor.ReadBody(buf, r.Body)
if err != nil {
finalErr = err
// This error is expected e.g. when the client defines (Content-Length) and aborts the request before
// It means that EOF was encountered in the middle of reading the body. This is not a server error.
if errors.Is(err, io.ErrUnexpectedEOF) {
requestLogger.Debug("unexpected EOF while reading request body", zap.Error(err))
} else {
requestLogger.Error("failed to read request body", zap.Error(err))
}
writeRequestErrors(r, w, http.StatusBadRequest, graphqlerrors.RequestErrorsFromError(err), requestLogger)
return
}
/**
* Parse the operation
*/
if !traceOptions.ExcludeParseStats {
traceTimings.StartParse()
}
_, engineParseSpan := h.tracer.Start(r.Context(), "Operation - Parse",
trace.WithSpanKind(trace.SpanKindInternal),
)
operationKit, err := h.operationProcessor.NewKit(body)
defer operationKit.Free()
if err != nil {
finalErr = err
rtrace.AttachErrToSpan(engineParseSpan, err)
// Mark the root span of the router as failed, so we can easily identify failed requests
rtrace.AttachErrToSpan(routerSpan, err)
engineParseSpan.End()
writeOperationError(r, w, requestLogger, err)
return
}
err = operationKit.Parse(r.Context(), clientInfo, requestLogger)
if err != nil {
finalErr = err
rtrace.AttachErrToSpan(engineParseSpan, err)
// Mark the root span of the router as failed, so we can easily identify failed requests
rtrace.AttachErrToSpan(routerSpan, err)
engineParseSpan.End()
writeOperationError(r, w, requestLogger, err)
return
}
engineParseSpan.End()
if !traceOptions.ExcludeParseStats {
traceTimings.EndParse()
}
if blockedErr := h.operationBlocker.OperationIsBlocked(operationKit.parsedOperation); blockedErr != nil {
// Mark the root span of the router as failed, so we can easily identify failed requests
rtrace.AttachErrToSpan(routerSpan, blockedErr)
writeRequestErrors(r, w, http.StatusOK, graphqlerrors.RequestErrorsFromError(blockedErr), requestLogger)
return
}
// Set the router span name after we have the operation name
routerSpan.SetName(GetSpanName(operationKit.parsedOperation.Name, operationKit.parsedOperation.Type))
baseAttributeValues = []attribute.KeyValue{
otel.WgOperationName.String(operationKit.parsedOperation.Name),
otel.WgOperationType.String(operationKit.parsedOperation.Type),
}
if operationKit.parsedOperation.PersistedID != "" {
baseAttributeValues = append(baseAttributeValues, otel.WgOperationPersistedID.String(operationKit.parsedOperation.PersistedID))
}
routerSpan.SetAttributes(baseAttributeValues...)
metrics.AddAttributes(baseAttributeValues...)
/**
* Normalize the operation
*/
if !traceOptions.ExcludeNormalizeStats {
traceTimings.StartNormalize()
}
_, engineNormalizeSpan := h.tracer.Start(r.Context(), "Operation - Normalize",
trace.WithSpanKind(trace.SpanKindInternal),
)
err = operationKit.Normalize()
if err != nil {
finalErr = err
rtrace.AttachErrToSpan(engineNormalizeSpan, err)
// Mark the root span of the router as failed, so we can easily identify failed requests
rtrace.AttachErrToSpan(routerSpan, err)
engineNormalizeSpan.End()
writeOperationError(r, w, requestLogger, err)
return
}
engineNormalizeSpan.End()
if !traceOptions.ExcludeNormalizeStats {
traceTimings.EndNormalize()
}
if h.traceExportVariables {
// At this stage the variables are normalized
routerSpan.SetAttributes(otel.WgOperationVariables.String(string(operationKit.parsedOperation.Variables)))
}
baseAttributeValues = []attribute.KeyValue{
otel.WgOperationHash.String(strconv.FormatUint(operationKit.parsedOperation.ID, 10)),
}
// Set the normalized operation as soon as we have it
routerSpan.SetAttributes(otel.WgOperationContent.String(operationKit.parsedOperation.NormalizedRepresentation))
routerSpan.SetAttributes(baseAttributeValues...)
metrics.AddAttributes(baseAttributeValues...)
/**
* Validate the operation
*/
if !traceOptions.ExcludeValidateStats {
traceTimings.StartValidate()
}
_, engineValidateSpan := h.tracer.Start(r.Context(), "Operation - Validate",
trace.WithSpanKind(trace.SpanKindInternal),
)
err = operationKit.Validate()
if err != nil {
finalErr = err
rtrace.AttachErrToSpan(engineValidateSpan, err)
// Mark the root span of the router as failed, so we can easily identify failed requests
rtrace.AttachErrToSpan(routerSpan, err)
engineValidateSpan.End()
writeOperationError(r, w, requestLogger, err)
return
}
engineValidateSpan.End()
if !traceOptions.ExcludeValidateStats {
traceTimings.EndValidate()
}
/**
* Plan the operation
*/
// If the request has a query parameter wg_trace=true we skip the cache
// and always plan the operation
// this allows us to "write" to the plan
if !traceOptions.ExcludePlannerStats {
traceTimings.StartPlanning()
}
_, enginePlanSpan := h.tracer.Start(r.Context(), "Operation - Plan",
trace.WithSpanKind(trace.SpanKindInternal),
trace.WithAttributes(otel.WgEngineRequestTracingEnabled.Bool(traceOptions.Enable)),
)
opContext, err := h.planner.Plan(operationKit.parsedOperation, clientInfo, OperationProtocolHTTP, traceOptions)
if err != nil {
finalErr = err
rtrace.AttachErrToSpan(enginePlanSpan, err)
// Mark the root span of the router as failed, so we can easily identify failed requests
rtrace.AttachErrToSpan(routerSpan, err)
enginePlanSpan.End()
requestLogger.Error("failed to plan operation", zap.Error(err))
writeOperationError(r, w, requestLogger, err)
return
}
enginePlanSpan.SetAttributes(otel.WgEnginePlanCacheHit.Bool(opContext.planCacheHit))
enginePlanSpan.End()
if !traceOptions.ExcludePlannerStats {
traceTimings.EndPlanning()
}
// If we have authenticators, we try to authenticate the request
if len(h.accessController.authenticators) > 0 {
_, authenticateSpan := h.tracer.Start(r.Context(), "Authenticate",
trace.WithSpanKind(trace.SpanKindServer),
)
validatedReq, err := h.accessController.Access(w, r)
if err != nil {
finalErr = err
requestLogger.Error("failed to authenticate request", zap.Error(err))
// Mark the root span of the router as failed, so we can easily identify failed requests
rtrace.AttachErrToSpan(routerSpan, err)
rtrace.AttachErrToSpan(authenticateSpan, err)
authenticateSpan.End()
writeRequestErrors(r, w, http.StatusUnauthorized, graphqlerrors.RequestErrorsFromError(err), requestLogger)
return
}
authenticateSpan.End()
r = validatedReq
}
art.SetRequestTracingStats(r.Context(), traceOptions, traceTimings)
requestContext := buildRequestContext(w, r, opContext, requestLogger)
metrics.AddOperationContext(opContext)
ctxWithRequest := withRequestContext(r.Context(), requestContext)
ctxWithOperation := withOperationContext(ctxWithRequest, opContext)
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
newReq := r.WithContext(ctxWithOperation)
// Call the final handler that resolves the operation
// and enrich the context to make it available in the request context as well for metrics etc.
next.ServeHTTP(ww, newReq)
statusCode = ww.Status()
writtenBytes = ww.BytesWritten()
// Evaluate the request after the request has been handled by the engine handler
finalErr = requestContext.error
// Mark the root span of the router as failed, so we can easily identify failed requests
routerSpan = trace.SpanFromContext(newReq.Context())
if finalErr != nil {
rtrace.AttachErrToSpan(routerSpan, finalErr)
}
})
}
// flushMetrics flushes all metrics to the respective exporters
// only used for serverless router build
func (h *PreHandler) flushMetrics(ctx context.Context, requestLogger *zap.Logger) {
requestLogger.Debug("Flushing metrics ...")
now := time.Now()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := h.metrics.GqlMetricsExporter().ForceFlush(ctx); err != nil {
requestLogger.Error("Failed to flush schema usage metrics", zap.Error(err))
}
}()
wg.Add(1)
go func() {
defer wg.Done()
if err := h.metrics.MetricStore().Flush(ctx); err != nil {
requestLogger.Error("Failed to flush OTEL metrics", zap.Error(err))
}
}()
wg.Add(1)
go func() {
defer wg.Done()
if err := h.tracerProvider.ForceFlush(ctx); err != nil {
requestLogger.Error("Failed to flush OTEL tracer", zap.Error(err))
}
}()
wg.Wait()
requestLogger.Debug("Metrics flushed", zap.Duration("duration", time.Since(now)))
}