-
Notifications
You must be signed in to change notification settings - Fork 17
/
tracer.go
80 lines (66 loc) · 2.21 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
// Copyright 2023 The Infratographer Authors
//
// 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 oteltracing
import (
"context"
"fmt"
"strings"
"github.com/99designs/gqlgen/graphql"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
type (
// Tracer provides an otel tracer for gqlgen servers
Tracer struct {
// FieldSpans allow you to enable a span for each field in the response
FieldSpans bool
}
)
var _ interface {
graphql.HandlerExtension
graphql.FieldInterceptor
} = Tracer{}
var tracer = otel.Tracer("go.infratographer.com/x/gqlgenx/oteltracing")
// ExtensionName returns the name of this extension
func (Tracer) ExtensionName() string {
return "OpenTelemetryTracing"
}
// Validate is required to meet HandlerExtension interface
func (Tracer) Validate(graphql.ExecutableSchema) error {
return nil
}
// InterceptField adds the middleware that lets us add traces to each field of a request
func (t Tracer) InterceptField(ctx context.Context, next graphql.Resolver) (interface{}, error) {
fc := graphql.GetFieldContext(ctx)
// check if this is a method or a resolver, if it's not and we aren't emitting
// field spans skip tracing
if !fc.IsMethod && !fc.IsResolver && !t.FieldSpans {
return next(ctx)
}
attrs := []attribute.KeyValue{
attribute.String("fieldName", fc.Field.Name),
attribute.String("returnType", fc.Field.Definition.Type.String()),
}
if fc.IsMethod {
for k, v := range fc.Args {
if strings.HasSuffix(k, "ID") || k == "id" {
attrs = append(attrs, attribute.String("args."+k, fmt.Sprintf("%s", v)))
}
}
}
ctx, span := tracer.Start(ctx, fc.Path().String(), trace.WithAttributes(attrs...))
defer span.End()
return next(ctx)
}