-
Notifications
You must be signed in to change notification settings - Fork 124
/
http_handler.go
79 lines (70 loc) · 2.34 KB
/
http_handler.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
package graphql
import (
"context"
"errors"
"net/http"
"runtime/debug"
"strings"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/evergreen-ci/gimlet"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/ravilushqa/otelgqlgen"
"github.com/vektah/gqlparser/v2/gqlerror"
"go.opentelemetry.io/otel/attribute"
)
// Handler returns a gimlet http handler func used as the gql route handler
func Handler(apiURL string) func(w http.ResponseWriter, r *http.Request) {
srv := handler.NewDefaultServer(NewExecutableSchema(New(apiURL)))
// Send OTEL traces for each request.
// Only create spans for resolved fields.
srv.Use(otelgqlgen.Middleware(
otelgqlgen.WithCreateSpanFromFields(func(fieldCtx *graphql.FieldContext) bool {
return fieldCtx.IsMethod
}),
otelgqlgen.WithRequestVariablesAttributesBuilder(
otelgqlgen.RequestVariablesBuilderFunc(func(requestVariables map[string]interface{}) []attribute.KeyValue {
redactedRequestVariables := RedactFieldsInMap(requestVariables, redactedFields)
return otelgqlgen.RequestVariables(redactedRequestVariables)
}),
),
))
// Log graphql requests to splunk
srv.Use(SplunkTracing{})
// Disable queries for service degradation
srv.Use(DisableQuery{})
// Handler to log graphql panics to splunk
srv.SetRecoverFunc(func(ctx context.Context, err interface{}) error {
queryPath := graphql.GetFieldContext(ctx).Path()
grip.Critical(message.Fields{
"path": "/graphql/query",
"message": "unhandled panic",
"error": err,
"stack": string(debug.Stack()),
"request": gimlet.GetRequestID(ctx),
"query": queryPath,
})
return errors.New("internal server error")
})
srv.SetErrorPresenter(func(ctx context.Context, err error) *gqlerror.Error {
fieldCtx := graphql.GetFieldContext(ctx)
queryPath := ""
args := map[string]interface{}{}
if fieldCtx != nil {
queryPath = fieldCtx.Path().String()
args = fieldCtx.Args
}
args = RedactFieldsInMap(args, redactedFields)
if err != nil && !strings.HasSuffix(err.Error(), context.Canceled.Error()) {
grip.Error(message.WrapError(err, message.Fields{
"path": "/graphql/query",
"query": queryPath,
"args": args,
"request": gimlet.GetRequestID(ctx),
}))
}
return graphql.DefaultErrorPresenter(ctx, err)
})
return srv.ServeHTTP
}