forked from ravilushqa/otelgqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgqlgen.go
161 lines (134 loc) · 4.23 KB
/
gqlgen.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
// Copyright Ravil Galaktionov
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package otelgqlgen
import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler/extension"
otelcontrib "go.opentelemetry.io/contrib"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
oteltrace "go.opentelemetry.io/otel/trace"
)
const (
tracerName = "go.opentelemetry.io/contrib/instrumentation/github.com/99designs/gqlgen/otelgqlgen"
extensionName = "OpenTelemetry"
complexityLimit = "ComplexityLimit"
)
type Tracer struct {
complexityExtensionName string
tracer oteltrace.Tracer
}
var _ interface {
graphql.HandlerExtension
graphql.ResponseInterceptor
graphql.FieldInterceptor
} = Tracer{}
func (a Tracer) ExtensionName() string {
return extensionName
}
func (a Tracer) Validate(_ graphql.ExecutableSchema) error {
return nil
}
func (a Tracer) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
ctx, span := a.tracer.Start(ctx, operationName(ctx), oteltrace.WithSpanKind(oteltrace.SpanKindServer))
defer span.End()
if !span.IsRecording() {
return next(ctx)
}
oc := graphql.GetOperationContext(ctx)
span.SetAttributes(
RequestQuery(oc.RawQuery),
)
complexityExtension := a.complexityExtensionName
if complexityExtension == "" {
complexityExtension = complexityLimit
}
complexityStats, ok := oc.Stats.GetExtension(complexityExtension).(*extension.ComplexityStats)
if !ok {
// complexity extension is not used
complexityStats = &extension.ComplexityStats{}
}
if complexityStats.ComplexityLimit > 0 {
span.SetAttributes(
RequestComplexityLimit(int64(complexityStats.ComplexityLimit)),
RequestOperationComplexity(int64(complexityStats.Complexity)),
)
}
span.SetAttributes(RequestVariables(oc.Variables)...)
resp := next(ctx)
if len(resp.Errors) > 0 {
span.SetStatus(codes.Error, resp.Errors.Error())
span.RecordError(fmt.Errorf(resp.Errors.Error()))
span.SetAttributes(ResolverErrors(resp.Errors)...)
}
return resp
}
func (a Tracer) InterceptField(ctx context.Context, next graphql.Resolver) (interface{}, error) {
fc := graphql.GetFieldContext(ctx)
ctx, span := a.tracer.Start(ctx,
fc.Field.ObjectDefinition.Name+"/"+fc.Field.Name,
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
)
defer span.End()
if !span.IsRecording() {
return next(ctx)
}
span.SetAttributes(
ResolverPath(fc.Path().String()),
ResolverObject(fc.Field.ObjectDefinition.Name),
ResolverField(fc.Field.Name),
ResolverAlias(fc.Field.Alias),
)
span.SetAttributes(ResolverArgs(fc.Field.Arguments)...)
resp, err := next(ctx)
errList := graphql.GetFieldErrors(ctx, fc)
if len(errList) != 0 {
span.SetStatus(codes.Error, errList.Error())
span.RecordError(fmt.Errorf(errList.Error()))
span.SetAttributes(ResolverErrors(errList)...)
}
return resp, err
}
// Middleware sets up a handler to start tracing the incoming
// requests. The service parameter should describe the name of the
// (virtual) server handling the request. extension parameter may be empty string.
func Middleware(opts ...Option) Tracer {
cfg := config{}
for _, opt := range opts {
opt.apply(&cfg)
}
if cfg.TracerProvider == nil {
cfg.TracerProvider = otel.GetTracerProvider()
}
tracer := cfg.TracerProvider.Tracer(
tracerName,
oteltrace.WithInstrumentationVersion(otelcontrib.SemVersion()),
)
return Tracer{
tracer: tracer,
}
}
func operationName(ctx context.Context) string {
requestContext := graphql.GetOperationContext(ctx)
requestName := "nameless-operation"
if requestContext.Doc != nil && len(requestContext.Doc.Operations) != 0 {
op := requestContext.Doc.Operations[0]
if op.Name != "" {
requestName = op.Name
}
}
return requestName
}