This guide provides step-by-step instructions for setting up OpenTelemetry (OTel) instrumentation in your Express.js applications to enable comprehensive observability. OpenTelemetry is an open-source framework that helps you collect telemetry data—such as traces, metrics, and logs—from your app to monitor its performance and health.

Once your telemetry data is captured, you can configure an exporter to send it directly to Atatus for advanced monitoring and insights.

The process of setting up OpenTelemetry in Express.js involves three key steps:

Instrumenting Your Express.js Application: Add the necessary OpenTelemetry components to your Express.js app for trace and metric collection.
Configuring the Exporter: Set up the exporter to send your telemetry data to Atatus.
Validating the Setup: Verify that the configuration is working and that your data is being correctly sent to Atatus.

Sending Telemetry Data to Atatus in Two Ways

Send Telemetry Traces to Atatus

Option 1: No Code Automatic Instrumentation (recommended)
Option 2: Code Level Automatic Instrumentation

Send Traces Using the OTel Collector Binary

Option 1: No Code Automatic Instrumentation (recommended)
Option 2: Code Level Automatic Instrumentation

Send Telemetry Traces to Atatus

Option 1: No Code Automatic Instrumentation

  1. Install OpenTelemetry packages

    npm install --save @opentelemetry/api
    npm install --save @opentelemetry/auto-instrumentations-node
    
  2. Run your application

    export OTEL_TRACES_EXPORTER="otlp"
    export OTEL_EXPORTER_OTLP_ENDPOINT="https://otel-rx.atatus.com:443"
    export OTEL_NODE_RESOURCE_DETECTORS="env,host,os"
    export OTEL_SERVICE_NAME="<APP_NAME>"
    export OTEL_EXPORTER_OTLP_HEADERS="api-key=ATATUS_INGESTION_KEY"
    export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"
    <your_run_command>
    

    You can get the ATATUS_INGESTION_KEY from Settings -> Account Settings -> API Keys in Atatus dashboard.

Option 2: Code Level Automatic Instrumentation

  1. Install OpenTelemetry packages

    npm install --save @opentelemetry/api@^1.6.0
    npm install --save @opentelemetry/sdk-node@^0.45.0
    npm install --save @opentelemetry/auto-instrumentations-node@^0.39.4
    npm install --save @opentelemetry/exporter-trace-otlp-http@^0.45.0
    
  2. Create tracing.js file

    // tracing.js
    'use strict'
    const process = require('process');
    const opentelemetry = require('@opentelemetry/sdk-node');
    const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
    const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
    const { Resource } = require('@opentelemetry/resources');
    const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
    
    // do not set headers in exporterOptions, the OTel spec recommends setting headers through ENV variables
    // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specifying-headers-via-environment-variables
    
    // highlight-start
    const exporterOptions = {
      url: 'https://otel-rx.atatus.com:443/v1/traces'
    }
    // highlight-end
    
    const traceExporter = new OTLPTraceExporter(exporterOptions);
    const sdk = new opentelemetry.NodeSDK({
      traceExporter,
      instrumentations: [getNodeAutoInstrumentations()],
      resource: new Resource({
        // highlight-next-line
        [SemanticResourceAttributes.SERVICE_NAME]: 'node_app'
      })
    });
    
    // initialize the SDK and register with the OpenTelemetry API
    // this enables the API to record telemetry
    sdk.start()
    
    // gracefully shut down the SDK on process exit
    process.on('SIGTERM', () => {
      sdk.shutdown()
        .then(() => console.log('Tracing terminated'))
        .catch((error) => console.log('Error terminating tracing', error))
        .finally(() => process.exit(0));
    });
    
  3. Run the application with OTEL_EXPORTER_OTLP_HEADERS env as follows

    OTEL_EXPORTER_OTLP_HEADERS="api-key=ATATUS_INGESTION_KEY" node -r ./tracing.js app.js
    

Send Traces Using the OTel Collector Binary:

To collect and send traces to Atatus, it's recommended to install the OpenTelemetry (OTel) Collector binary. The OTel Collector helps gather logs, host metrics, resource, and infrastructure attributes, enabling richer contextual data and easier signal correlation.

For detailed instructions on setting up the OTel Collector binary on your VM, please refer to the official installation guide. Once the Collector is up and running, you can proceed with instrumenting your Java application to start sending telemetry data to Atatus.

Option 1: No Code Automatic Instrumentation

  1. Install OpenTelemetry packages

    npm install --save @opentelemetry/api
    npm install --save @opentelemetry/auto-instrumentations-node
    
  2. Run your application

    export OTEL_TRACES_EXPORTER="otlp"
    export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318/v1/traces"
    export OTEL_NODE_RESOURCE_DETECTORS="env,host,os"
    export OTEL_SERVICE_NAME="<APP_NAME>"
    export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"
    <your_run_command>
    

    You can get the ATATUS_INGESTION_KEY from Settings -> Account Settings -> API Keys in Atatus dashboard.

Option 2: Code Level Automatic Instrumentation

  1. Install OpenTelemetry packages

    npm install --save @opentelemetry/api@^1.6.0
    npm install --save @opentelemetry/sdk-node@^0.45.0
    npm install --save @opentelemetry/auto-instrumentations-node@^0.39.4
    npm install --save @opentelemetry/exporter-trace-otlp-http@^0.45.0
    
  2. Create tracing.js file

    // tracing.js
    'use strict'
    const process = require('process');
    const opentelemetry = require('@opentelemetry/sdk-node');
    const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
    const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
    const { Resource } = require('@opentelemetry/resources');
    const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
    
    // do not set headers in exporterOptions, the OTel spec recommends setting headers through ENV variables
    // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specifying-headers-via-environment-variables
    
    // highlight-start
    const exporterOptions = {
      url: 'https://otel-rx.atatus.com:443/v1/traces'
    }
    // highlight-end
    
    const traceExporter = new OTLPTraceExporter(exporterOptions);
    const sdk = new opentelemetry.NodeSDK({
      traceExporter,
      instrumentations: [getNodeAutoInstrumentations()],
      resource: new Resource({
        // highlight-next-line
        [SemanticResourceAttributes.SERVICE_NAME]: 'node_app'
      })
    });
    
    // initialize the SDK and register with the OpenTelemetry API
    // this enables the API to record telemetry
    sdk.start()
    
    // gracefully shut down the SDK on process exit
    process.on('SIGTERM', () => {
      sdk.shutdown()
        .then(() => console.log('Tracing terminated'))
        .catch((error) => console.log('Error terminating tracing', error))
        .finally(() => process.exit(0));
    });
    
  3. Run the application with OTEL_EXPORTER_OTLP_HEADERS env as follows

    OTEL_EXPORTER_OTLP_HEADERS="api-key=ATATUS_INGESTION_KEY" node -r ./tracing.js app.js