⚠ Experimental ⚠

This feature may get big changes in future releases. Check the changelog for update notes.

Tracing

GraphQL::Tracing provides a .trace hook to observe events from the GraphQL runtime.

A tracer must implement .trace, for example:

class MyCustomTracer
  def self.trace(key, data)
    # do stuff with key & data
    yield
  end
end

.trace is called with:

To run a tracer for every query, add it to the schema with tracer:

# Run `MyCustomTracer` for all queries
class MySchema < GraphQL::Schema
  tracer(MyCustomTracer)
end

Or, to run a tracer for one query only, add it to context: as tracers: [...], for example:

# Run `MyCustomTracer` for this query
MySchema.execute(..., context: { tracers: [MyCustomTracer]})

For a full list of events, see the GraphQL::Tracing API docs.

ActiveSupport::Notifications

You can emit events to ActiveSupport::Notifications with an experimental tracer, ActiveSupportNotificationsTracing.

To enable it, install the tracer:

# Send execution events to ActiveSupport::Notifications
class MySchema < GraphQL::Schema
  tracer(GraphQL::Tracing::ActiveSupportNotificationsTracing)
end

Monitoring

Several monitoring platforms are supported out-of-the box by GraphQL-Ruby (see platforms below).

Leaf fields are not monitored (to avoid high cardinality in the metrics service).

Implementations are based on Tracing::PlatformTracing.

Appsignal

To add AppSignal instrumentation:

class MySchema < GraphQL::Schema
  use(GraphQL::Tracing::AppsignalTracing)
end

New Relic

To add New Relic instrumentation:

class MySchema < GraphQL::Schema
  use(GraphQL::Tracing::NewRelicTracing)
  # Optional, use the operation name to set the new relic transaction name:
  # use(GraphQL::Tracing::NewRelicTracing, set_transaction_name: true)
end

Scout

To add Scout APM instrumentation:

class MySchema < GraphQL::Schema
  use(GraphQL::Tracing::ScoutTracing)
end

Skylight

To add Skylight instrumentation:

class MySchema < GraphQL::Schema
  use(GraphQL::Tracing::SkylightTracing)
end

Datadog

To add Datadog instrumentation:

class MySchema < GraphQL::Schema
  use(GraphQL::Tracing::DataDogTracing)
end

Prometheus

To add Prometheus instrumentation:

require 'prometheus_exporter/client'

class MySchema < GraphQL::Schema
  use(GraphQL::Tracing::PrometheusTracing)
end

The PrometheusExporter server must be run with a custom type collector that extends GraphQL::Tracing::PrometheusTracing::GraphQLCollector:

# lib/graphql_collector.rb

require 'graphql/tracing'

class GraphQLCollector < GraphQL::Tracing::PrometheusTracing::GraphQLCollector
end
bundle exec prometheus_exporter -a lib/graphql_collector.rb