|
| 1 | +// Copyright Splunk Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package splunkgraphql provides OpenTelemetry instrumentation for the |
| 16 | +// github.com/graph-gophers/graphql-go module. |
| 17 | +package splunkgraphql |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/graph-gophers/graphql-go/errors" |
| 24 | + "github.com/graph-gophers/graphql-go/introspection" |
| 25 | + "github.com/graph-gophers/graphql-go/trace" |
| 26 | + "github.com/signalfx/splunk-otel-go/instrumentation/internal" |
| 27 | + "go.opentelemetry.io/otel/attribute" |
| 28 | + "go.opentelemetry.io/otel/codes" |
| 29 | + oteltrace "go.opentelemetry.io/otel/trace" |
| 30 | +) |
| 31 | + |
| 32 | +const instrumentationName = "github.com/signalfx/splunk-otel-go/instrumentation/github.com/graph-gophers/graphql-go/splunkgraphql" |
| 33 | + |
| 34 | +var ( |
| 35 | + graphqlFieldKey = attribute.Key("graphql.field") |
| 36 | + graphqlQueryKey = attribute.Key("graphql.query") |
| 37 | + graphqlTypeKey = attribute.Key("graphql.type") |
| 38 | +) |
| 39 | + |
| 40 | +// otelTracer implements the graphql-go/trace.Tracer interface using |
| 41 | +// OpenTelemetry. |
| 42 | +type otelTracer struct { |
| 43 | + cfg internal.Config |
| 44 | +} |
| 45 | + |
| 46 | +var ( |
| 47 | + _ trace.Tracer = (*otelTracer)(nil) |
| 48 | + _ trace.ValidationTracerContext = (*otelTracer)(nil) |
| 49 | +) |
| 50 | + |
| 51 | +// NewTracer returns a new trace.Tracer backed by OpenTelemetry. |
| 52 | +func NewTracer(opts ...Option) trace.Tracer { |
| 53 | + cfg := internal.NewConfig(instrumentationName, localToInternal(opts)...) |
| 54 | + return &otelTracer{cfg: *cfg} |
| 55 | +} |
| 56 | + |
| 57 | +func traceQueryFinishFunc(span oteltrace.Span) trace.TraceQueryFinishFunc { |
| 58 | + return func(errs []*errors.QueryError) { |
| 59 | + for _, err := range errs { |
| 60 | + span.RecordError(err) |
| 61 | + } |
| 62 | + switch n := len(errs); n { |
| 63 | + case 0: |
| 64 | + // Nothing to do. |
| 65 | + case 1: |
| 66 | + span.SetStatus(codes.Error, errs[0].Error()) |
| 67 | + default: |
| 68 | + msg := fmt.Sprintf("%s (and %d more errors)", errs[0], n-1) |
| 69 | + span.SetStatus(codes.Error, msg) |
| 70 | + } |
| 71 | + span.End() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TraceQuery traces a GraphQL query. |
| 76 | +func (t *otelTracer) TraceQuery(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, varTypes map[string]*introspection.Type) (context.Context, trace.TraceQueryFinishFunc) { |
| 77 | + spanCtx, span := t.cfg.ResolveTracer(ctx).Start( |
| 78 | + ctx, |
| 79 | + "GraphQL request", |
| 80 | + oteltrace.WithSpanKind(oteltrace.SpanKindServer), |
| 81 | + oteltrace.WithAttributes(graphqlQueryKey.String(queryString)), |
| 82 | + ) |
| 83 | + |
| 84 | + return spanCtx, traceQueryFinishFunc(span) |
| 85 | +} |
| 86 | + |
| 87 | +// TraceField traces a GraphQL field access. |
| 88 | +func (t *otelTracer) TraceField(ctx context.Context, label, typeName, fieldName string, trivial bool, args map[string]interface{}) (context.Context, trace.TraceFieldFinishFunc) { |
| 89 | + if trivial { |
| 90 | + return ctx, func(*errors.QueryError) {} |
| 91 | + } |
| 92 | + |
| 93 | + spanCtx, span := t.cfg.ResolveTracer(ctx).Start( |
| 94 | + ctx, |
| 95 | + "GraphQL field", |
| 96 | + oteltrace.WithAttributes( |
| 97 | + graphqlFieldKey.String(fieldName), |
| 98 | + graphqlTypeKey.String(typeName), |
| 99 | + ), |
| 100 | + ) |
| 101 | + |
| 102 | + return spanCtx, func(err *errors.QueryError) { |
| 103 | + if err != nil { |
| 104 | + span.RecordError(err) |
| 105 | + span.SetStatus(codes.Error, err.Error()) |
| 106 | + } |
| 107 | + span.End() |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// TraceValidation traces the schema validation step preceding an operation. |
| 112 | +func (t *otelTracer) TraceValidation(ctx context.Context) trace.TraceValidationFinishFunc { |
| 113 | + _, span := t.cfg.ResolveTracer(ctx).Start(ctx, "GraphQL validation") |
| 114 | + return traceQueryFinishFunc(span) |
| 115 | +} |
0 commit comments