Socket
Socket
Sign inDemoInstall

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

Package Overview
Dependencies
14
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

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


Version published

Readme

Source

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.

FAQs

Last updated on 27 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc