otelgraphql

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2024 License: BSD-2-Clause Imports: 11 Imported by: 3

README

PkgGoDev

OpenTelemetry Go instrumentation for the graphql-go GraphQL Server

This instrumentation records GraphQL operations using OpenTelemetry API.

Installation

go get github.com/uptrace/opentelemetry-go-extra/otelgraphql

Usage

graphql-go already provides a tracer interface along with its respective opentracing implementation This interface consists of the following methods, all of which get context.Context as a parameter:

TraceValidation: traces the schema validation step which precedes the actual operation.

TraceQuery: traces the actual operation, query or mutation, as a whole

TraceField: traces a field-specific operation; its span should typically be a sub-span of the TraceQuery one;

otelgraphql provides an implementation of this interface, which is practically a port of the opentracing one that comes pre-packaged with graphql-go.

Some other points:

  1. graphql-go exposes a single HTTP Handler for all graphql operations. That makes it a natural fit for otelhttp and other router packages instrumentation, if propagating frontend baggage (e.g. headers) is required.
  2. graphql-go resolver methods do get context.Context as a parameter, which allows for field-specific (sub-)span creation, if TraceQuery does not suffice.
import (
  "context"
  "log"
  "net/http"

  "github.com/graph-gophers/graphql-go"
  "github.com/graph-gophers/graphql-go/relay"

  "github.com/uptrace/opentelemetry-go-extra/otelgraphql"
)

tracer := otelgraphql.NewTracer(otelgraphql.WithTracerProvider(traceProvider))

opts := []graphql.SchemaOpt{
  graphql.Tracer(tracer),
}
schema = graphql.MustParseSchema(schemaString, &RootResolver{}, opts...)

http.Handle("/graphql", &relay.Handler{Schema: schema})

log.Fatal(http.ListenAndServe(":8080", nil))

See example for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version is the current release version.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option specifies instrumentation configuration options.

func WithTracerProvider

func WithTracerProvider(provider oteltrace.TracerProvider) Option

WithTracerProvider specifies a tracer provider to use for creating a tracer. If none is specified, the global provider is used.

type Tracer

type Tracer struct {
	// contains filtered or unexported fields
}

func NewTracer

func NewTracer(opts ...Option) *Tracer

func (Tracer) TraceField

func (t Tracer) TraceField(
	ctx context.Context,
	label,
	typeName,
	fieldName string,
	trivial bool,
	args map[string]interface{},
) (context.Context, tracer.FieldFinishFunc)

func (Tracer) TraceQuery

func (t Tracer) TraceQuery(
	ctx context.Context,
	queryString string, operationName string,
	variables map[string]interface{},
	_ map[string]*introspection.Type,
) (context.Context, tracer.QueryFinishFunc)

func (Tracer) TraceValidation

func (t Tracer) TraceValidation(ctx context.Context) tracer.ValidationFinishFunc

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL