-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
opentracing.go
51 lines (42 loc) · 1.33 KB
/
opentracing.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
package opentracing
import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
)
func ResolverMiddleware() graphql.FieldMiddleware {
return func(ctx context.Context, next graphql.Resolver) (interface{}, error) {
rctx := graphql.GetResolverContext(ctx)
span, ctx := opentracing.StartSpanFromContext(ctx, rctx.Object+"_"+rctx.Field.Name,
opentracing.Tag{Key: "resolver.object", Value: rctx.Object},
opentracing.Tag{Key: "resolver.field", Value: rctx.Field.Name},
)
defer span.Finish()
ext.SpanKind.Set(span, "server")
ext.Component.Set(span, "gqlgen")
res, err := next(ctx)
if err != nil {
ext.Error.Set(span, true)
span.LogFields(
log.String("event", "error"),
log.String("message", err.Error()),
log.String("error.kind", fmt.Sprintf("%T", err)),
)
}
return res, err
}
}
func RequestMiddleware() graphql.RequestMiddleware {
return func(ctx context.Context, next func(ctx context.Context) []byte) []byte {
requestContext := graphql.GetRequestContext(ctx)
span, ctx := opentracing.StartSpanFromContext(ctx, requestContext.RawQuery)
defer span.Finish()
ext.SpanKind.Set(span, "server")
ext.Component.Set(span, "gqlgen")
res := next(ctx)
return res
}
}