forked from zhevron/gqlgen-opentelemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.go
150 lines (137 loc) · 4.41 KB
/
tracer.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
package gqlgen_opentelemetry
import (
"context"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/vektah/gqlparser/v2/ast"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/trace"
)
const (
extensionName = "github.com/zhevron/gqlgen-opentelemetry"
extensionVersion = "1.0.4"
graphqlComplexity = attribute.Key("graphql.operation.complexity")
graphqlFieldAlias = attribute.Key("graphql.field.alias")
graphqlFieldName = attribute.Key("graphql.field.name")
graphqlFieldPath = attribute.Key("graphql.field.path")
graphqlFieldType = attribute.Key("graphql.field.type")
graphqlVariablesPrefix = "graphql.variables."
)
var baseAttributes = []attribute.KeyValue{
semconv.OTelLibraryName(extensionName),
semconv.OTelLibraryVersion(extensionVersion),
}
type Tracer struct {
IncludeFieldSpans bool
IncludeVariables bool
TracerProvider trace.TracerProvider
}
func (Tracer) ExtensionName() string {
return extensionName
}
func (t Tracer) Validate(schema graphql.ExecutableSchema) error {
return nil
}
func (t Tracer) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
if !graphql.HasOperationContext(ctx) {
return next(ctx)
}
oc := graphql.GetOperationContext(ctx)
if oc.Operation == nil {
return next(ctx)
}
operationType := getOperationTypeAttribute(oc)
spanName := makeSpanName(oc.Operation.Name, operationType.Value.AsString())
ctx, span := t.getTracer(ctx).Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindServer), trace.WithAttributes(baseAttributes...))
defer span.End()
span.SetAttributes(
semconv.GraphqlOperationName(oc.Operation.Name),
operationType,
semconv.GraphqlDocument(oc.RawQuery),
)
if stats := extension.GetComplexityStats(ctx); stats != nil {
span.SetAttributes(graphqlComplexity.Int(stats.Complexity))
}
if t.IncludeVariables {
for name, value := range oc.Variables {
span.SetAttributes(attribute.KeyValue{
Key: attribute.Key(graphqlVariablesPrefix + name),
Value: makeAttributeValue(value),
})
}
}
res := next(ctx)
if res != nil && len(res.Errors) > 0 {
span.SetStatus(codes.Error, res.Errors.Error())
for _, err := range res.Errors {
span.RecordError(err)
}
}
return res
}
func (t Tracer) InterceptField(ctx context.Context, next graphql.Resolver) (interface{}, error) {
fc := graphql.GetFieldContext(ctx)
if !t.IncludeFieldSpans || !fc.IsMethod || !fc.IsResolver {
return next(ctx)
}
spanName := fc.Field.ObjectDefinition.Name + "." + fc.Field.Name
ctx, span := t.getTracer(ctx).Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindServer), trace.WithAttributes(baseAttributes...))
defer span.End()
span.SetAttributes(
graphqlFieldName.String(fc.Field.Name),
graphqlFieldPath.String(fc.Path().String()),
graphqlFieldType.String(fc.Field.ObjectDefinition.Name),
)
if fc.Field.Alias != fc.Field.Name {
span.SetAttributes(graphqlFieldAlias.String(fc.Field.Alias))
}
res, err := next(ctx)
if errList := graphql.GetFieldErrors(ctx, fc); len(errList) > 0 {
span.SetStatus(codes.Error, errList.Error())
for _, err := range errList {
span.RecordError(err)
}
}
return res, err
}
func (t Tracer) getTracer(ctx context.Context) trace.Tracer {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
return span.TracerProvider().Tracer(extensionName, trace.WithInstrumentationVersion(extensionVersion))
} else {
tp := t.TracerProvider
if tp == nil {
tp = otel.GetTracerProvider()
}
return tp.Tracer(extensionName, trace.WithInstrumentationVersion(extensionVersion))
}
}
func makeSpanName(operationName, operationType string) string {
spanName := operationType
if spanName == "" {
return "GraphQL Operation"
}
if operationName != "" {
spanName += " " + operationName
}
return spanName
}
func getOperationTypeAttribute(oc *graphql.OperationContext) attribute.KeyValue {
switch oc.Operation.Operation {
case ast.Mutation:
return semconv.GraphqlOperationTypeMutation
case ast.Subscription:
return semconv.GraphqlOperationTypeSubscription
case ast.Query:
return semconv.GraphqlOperationTypeQuery
default:
return semconv.GraphqlOperationTypeQuery
}
}
var _ interface {
graphql.HandlerExtension
graphql.ResponseInterceptor
graphql.FieldInterceptor
} = Tracer{}