/otelgqlgen

OpenTelemetry instrumentation for 99designs/gqlgen

Primary LanguageGoApache License 2.0Apache-2.0

OpenTelemetry-Go gqlgen Instrumentation

ci Go Report Card codecov Go Reference

It is an OpenTelemetry instrumentation for Golang 99designs/gqlgen, a port from open-telemetry/opentelemetry-go-contrib#761.

It can only instrument traces for the present.

Installation

To install the otelgqlgen package, use the following command:

go get github.com/ravilushqa/otelgqlgen

Usage

Below is a basic example of how to use otelgqlgen:

package main

import (
    "github.com/99designs/gqlgen/graphql/handler"
    "github.com/ravilushqa/otelgqlgen"
    "github.com/myorg/mygqlgenapi/graph"
)

func main() {
    srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))

    // Add the otelgqlgen middleware to add OpenTelemetry tracing
    srv.Use(otelgqlgen.Middleware())

    // Your usual server setup code...
}

Configuration Options

otelgqlgen provides several options to customize the tracing behavior:

  • WithTracerProvider(provider): Specifies a custom tracer provider. By default, the global OpenTelemetry tracer provider is used.
  • WithComplexityExtensionName(name): Specifies a name for the complexity extension. By default, a name is automatically generated.
  • WithRequestVariablesAttributesBuilder(builder): Specifies a custom function to build the attributes for the request variables.
  • WithoutVariables(): Disables the variables attributes.
  • WithCreateSpanFromFields(predicate): Specifies a custom function to control whether a span should be created based on the GraphQL context fields.

Example

See ./example.

Compatibility

This project is tested on the following systems.

OS Go Version Architecture
Ubuntu 1.20 amd64
Ubuntu 1.19 amd64
Ubuntu 1.20 386
Ubuntu 1.19 386
MacOS 1.20 amd64
MacOS 1.19 amd64
Windows 1.20 amd64
Windows 1.19 amd64
Windows 1.20 386
Windows 1.19 386

While this project should work for other systems, no compatibility guarantees are made for those systems currently.

The project follows the Release Policy to support major Go releases.

References

License

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.