Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Start for Free

OpenTelemetry in Apollo Federation

Configure your federated graph to emit logs, traces, and metrics

OpenTelemetry is a collection of open-source tools for generating and processing telemetry data (such as logs, traces, and metrics) from different systems in a generic and consistent way.

You can configure your gateway, your individual , or even a monolothic instance to emit telemetry related to processing .

Additionally, the @apollo/gateway library provides built-in OpenTelemetry instrumentation to emit gateway-specific spans for traces.

If you are using , it comes pre-built with support for OpenTelemetry.


does not currently consume OpenTelemetry-formatted data. To push trace data to Studio, see Federated trace data.

You should configure OpenTelemetry if you want to push trace data to an OpenTelemetry-compatible system, such as Zipkin or Jaeger.


1. Install required libraries

To use OpenTelemetry in your application, you need to install a baseline set of @opentelemetry Node.js libraries. This set differs slightly depending on whether you're setting up your federated gateway or a /monolith.

Most importantly, subgraphs and monoliths must install @opentelemetry/instrumentation-graphql, and gateways must not install it.

As shown above, most @opentelemetry libraries have reached 1.0. The instrumentation packages listed above are compatible at the time of this writing.

Update @apollo/gateway

If you're using OpenTelemetry in your federated gateway, also update the @apollo/gateway library to version 0.31.1 or later to add support for gateway-specific spans.

2. Configure instrumentation

Next, update your application to configure your OpenTelemetry instrumentation as early as possible in your app's execution. This must occur before you even import @apollo/server, express, or http. Otherwise, your trace data will be incomplete.

We recommend putting this configuration in its own file, which you import at the very top of index.js. A sample file is provided below (note the lines that should either be deleted or uncommented).

// Import required symbols
const { Resource } = require('@opentelemetry/resources');
const { SimpleSpanProcessor, ConsoleSpanExporter } = require ("@opentelemetry/sdk-trace-base");
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { HttpInstrumentation } = require ('@opentelemetry/instrumentation-http');
const { ExpressInstrumentation } = require ('@opentelemetry/instrumentation-express');
// const { GraphQLInstrumentation } = require ('@opentelemetry/instrumentation-graphql');
// Register server-related instrumentation
instrumentations: [
new HttpInstrumentation(),
new ExpressInstrumentation(),
//new GraphQLInstrumentation()
// Initialize provider and identify this particular service
// (in this case, we're implementing a federated gateway)
const provider = new NodeTracerProvider({
resource: Resource.default().merge(new Resource({
// Replace with any string to identify this service in your system
"": "gateway",
// Configure a test exporter to print all traces to the console
const consoleExporter = new ConsoleSpanExporter();
new SimpleSpanProcessor(consoleExporter)
// Register the provider to begin tracing

For now, this code does not push trace data to an external system. Instead, it prints that data to the console for debugging purposes.

After you make these changes to your app, start it up locally. It should begin printing trace data similar to the following:

Nice! Next, we can modify this code to begin pushing trace data to an external service, such as Zipkin or Jaeger.

3. Push trace data to a tracing system

Next, let's modify the code in the previous step to instead push traces to a locally running instance of Zipkin.


To run Zipkin locally, see the quickstart. If you want to use a different tracing system, consult the documentation for that system.

First, we need to replace our ConsoleSpanExporter (which prints traces to the terminal) with a ZipkinExporter, which specifically pushes trace data to a running Zipkin instance.

Install the following additional library:

npm install @opentelemetry/exporter-zipkin@1.0

Then, import the ZipkinExporter in your OpenTelemetry file:

const { ZipkinExporter } = require("@opentelemetry/exporter-zipkin");

Now we can replace our ConsoleSpanExporter with a ZipkinExporter. Replace lines 31-34 of the code in the previous step with the following:

// Configure an exporter that pushes all traces to Zipkin
// (This assumes Zipkin is running on localhost at the
// default port of 9411)
const zipkinExporter = new ZipkinExporter({
// url: set_this_if_not_running_zipkin_locally
new SimpleSpanProcessor(zipkinExporter)

Now, open Zipkin in your browser at http://localhost:9411. You should now be able to recent trace data in the UI!

You can show the details of any operation and see a breakdown of its processing timeline by span.

4. Update for production readiness

Our example telemetry configuration assumes that Zipkin is running locally, and that we want to process every span individually as it's emitted.

To prepare for production, we'll want to optimize performance by sending our traces to an OpenTelemetry Collector using the OTLPTraceExporter and replace our SimpleSpanProcessor with a BatchSpanProcessor. The Collector should be deployed as a local sidecar agent to buffer traces before they're sent along to their final destination. See the getting started docs for an overview.

npm install @opentelemetry/exporter-trace-otlp-http@0.27

Then, import the OTLPTraceExporter and BatchSpanProcessor in your dedicated OpenTelemetry file:

const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-http");
const { BatchSpanProcessor } = require("@opentelemetry/sdk-trace-base");

Now we can replace our ZipkinExporter with an OTLPTraceExporter. We can also replace our SimpleSpanProcessor with a BatchSpanProcessor. Replace lines 4-9 of the code in the previous step with the following:

// Configure an exporter that pushes all traces to a Collector
// (This assumes the Collector is running on the default url
// of http://localhost:4318/v1/traces)
const collectorTraceExporter = new OTLPTraceExporter();
new BatchSpanProcessor(collectorTraceExporter, {
maxQueueSize: 1000,
scheduledDelayMillis: 1000,

You can learn more about using the OTLPTraceExporter in the instrumentation docs.

GraphQL-specific spans

The @opentelemetry/instrumentation-graphql library enables subgraphs and monoliths to emit the following spans as part of OpenTelemetry traces:

graphql.parseThe amount of time the server spent parsing an operation string.
graphql.validateThe amount of time the server spent validating an operation string.
graphql.executeThe total amount of time the server spent executing an operation.
graphql.resolveThe amount of time the server spent resolving a particular field.

Note that not every GraphQL span appears in every operation trace. This is because can skip parsing or validating an operation string if that string is available in the operation cache.


Federated gateways must not install the @opentelemetry/instrumentation-graphql library, so these spans are not included in its traces.

Gateway-specific spans

The @apollo/gateway library emits the following spans as part of OpenTelemetry traces:

gateway.requestThe total amount of time the gateway spent serving a request.
gateway.validateThe amount of time the gateway spent validating a GraphQL operation string.
gateway.planThe amount of time the gateway spent generating a query plan for a validated operation.
gateway.executeThe amount of time the gateway spent executing operations on subgraphs.
gateway.fetchThe amount of time the gateway spent fetching data from a particular subgraph.
gateway.postprocessingThe amount of time the gateway spent composing a complete response from individual subgraph responses.
Federated Trace Data
Subgraph Specification
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy