-
Notifications
You must be signed in to change notification settings - Fork 4
/
graphtracer.go
52 lines (43 loc) · 1.19 KB
/
graphtracer.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
package gqltracer
import (
"context"
"github.com/99designs/gqlgen/graphql"
"go.opentelemetry.io/otel"
)
type GraphTracer struct {
}
// ExtensionName name of extension
func (t *GraphTracer) ExtensionName() string {
return "GraphTracer"
}
// InterceptField intercepts
func (t *GraphTracer) InterceptField(ctx context.Context, next graphql.Resolver) (any, error) {
field := graphql.GetFieldContext(ctx)
if field.IsResolver != true {
// We are not really interested in tracing simple values
// as it only produces noise
return next(ctx)
}
fieldName := field.Field.Name
if fieldName == "" {
fieldName = "root"
}
ctx, span := otel.Tracer("graph-field").Start(ctx, fieldName)
defer span.End()
return next(ctx)
}
// InterceptOperation intercepts
func (t *GraphTracer) InterceptOperation(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
op := graphql.GetOperationContext(ctx)
opName := op.OperationName
if opName == "" {
opName = "unknown"
}
ctx, span := otel.Tracer("graph-operation").Start(ctx, opName)
defer span.End()
return next(ctx)
}
// Validate is unnecessary in our case
func (t *GraphTracer) Validate(_ graphql.ExecutableSchema) error {
return nil
}