Skip to content

Commit

Permalink
Add otel tracer for gqlgenx (#73)
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
nicolerenee authored May 19, 2023
1 parent 52949ce commit 0c579d4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
26 changes: 26 additions & 0 deletions gqlgenx/oteltracing/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 provides a gqlgen middleware that adds otel tracing.
//
// You can use it like:
//
// srv := handler.New(es)
// srv.Use(oteltracing.Tracer{})
//
// If you would like spans for every field in the response you can enable FieldSpans:
//
// srv := handler.New(es)
// srv.Use(oteltracing.Tracer{FieldSpans: true})
package oteltracing
66 changes: 66 additions & 0 deletions gqlgenx/oteltracing/tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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)
}

0 comments on commit 0c579d4

Please sign in to comment.