Using OpenTelemetry to monitor GraphQL APIs

What is OpenTelemetry?

OpenTelemetry is an open-source vendor-agnostic set of tools, APIs, and SDKs used to instrument applications to create and manage telemetry data(logs, metrics, and traces). It aims to make telemetry data(logs, metrics, and traces) a built-in feature of cloud-native software applications. It is a project under CNCF with huge community backing.

Using OpenTelemetry GraphQL library

OpenTelemetry provides a library to monitor GraphQL APIs. The library will monitor your GraphQL queries without any code changes. OpenTelemetry libraries are used to generate and capture telemetry data.

Once the data is captured, it needs to be sent to a backend tool of your choice for storage and visualization. We will send the monitoring data captured by OpenTelemetry from the GraphQL application to JaegerUI.

Running a sample GraphQL application with OpenTelemetry

Below are the steps to run the sample GraphQL application with OpenTelemetry.

  1. Clone sample GraphQL app repository and go to the root folder

We will be using a sample GraphQL app at this GitHub repo.

git clone https://github.com/ganny26/graphql-opentelemetry-sample.git
cd graphql-opentelemetry-sample
  1. Install the required dependencies

You can check out the depencies required from package.json file. Install all the required dependencies for the sample application using npm

OpenTelemetry needs the following packages to instrument the GraphQL app.

"@opentelemetry/api": "^1.0.3",
"@opentelemetry/auto-instrumentations-node": "^0.25.0",
"@opentelemetry/exporter-otlp-grpc": "^0.26.0",
"@opentelemetry/instrumentation-graphql": "0.27.4",
"@opentelemetry/resources": "^0.24.0",
"@opentelemetry/sdk-node": "0.27.0",
"@opentelemetry/sdk-trace-base": "^1.0.1",
"@opentelemetry/sdk-trace-node": "^1.0.1",
"@opentelemetry/semantic-conventions": "^0.24.0",
  1. Configure instrumentation using tracer.js file

In order to instrument our GraphQL APIs, we will create a single tracer.js file and use it to instrument the service.

To capture GraphQL instrumentation, add the OpenTelemetry instrumentation GraphQL package. You can also configure some parameters based on your use-case.

const sdk = new opentelemetry.NodeSDK({
  traceExporter,
  instrumentations: [
    getNodeAutoInstrumentations(),
    new GraphQLInstrumentation({
      allowValues: true,
    }),
  ],
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: serviceName,
  }),
})

We need to initialize OpenTelemetry before your application gets loaded. If your application begins requiring packages before OpenTelemetry is set up, it can create issues. You can initialize OpenTelemetry by using the code as shown below:

const init = require('./tracer')
init('graphql-service')

A sample tracer.js file is provided in the GitHub repo here. Note the lines that should either be deleted or uncommented based on the application need.

  1. Setting up SigNoz as the OpenTelemetry backend

To set up OpenTelemetry to export telemetry data, you need to specify OTLP (OpenTelemetry Protocol) endpoint of a backend tool like SigNoz. It consists of the IP of the machine where SigNoz is installed and the port number at which SigNoz listens.

OTLP endpoint for SigNoz - <IP of the machine>:4317

If you have installed SigNoz on your local machine, then your endpoint is 127.0.0.1:4317.

  1. Run the GraphQL service
OTEL_EXPORTER_OTLP_ENDPOINT=127.0.0.1:4317 \
OTEL_RESOURCE_ATTRIBUTES=service.name=graphql-service \
node -r ./tracer.js index.js

Open GraphQL interface at http://localhost:4000/graphql

Selvaganesh © 2023.