• Home
  • Extend Dynatrace
  • Send data to Dynatrace with OpenTelemetry
  • OpenTelemetry Metrics
  • Instrument for metrics
  • Instrument Node.js applications with OpenTelemetry Metrics

Instrument Node.js applications with OpenTelemetry Metrics

This guide shows how to instrument your Node.js application with OpenTelemetry and export the metrics to Dynatrace.

  • To learn more about how Dynatrace works with OpenTelemetry, see Send data to Dynatrace with OpenTelemetry.

Prerequisites

  • Dynatrace version 1.222+

Overview

To monitor your Node.js application with OpenTelemetry

  1. Instrument your application
  2. Send the data to Dynatrace
  3. Restart your application and verify the data in Dynatrace

1. Instrument your application

To instrument manually, install the packages below and add the code snippet below to any Node.js method you want to monitor.

  • Set names for the meter and the instrument.
  • Add a description and attributes to the instrument as you see fit.
bash
npm install @opentelemetry/api npm install @opentelemetry/api-metrics npm install @opentelemetry/sdk-metrics-base
js
const { MeterProvider, PeriodicExportingMetricReader, ConsoleMetricExporter, AggregationTemporality } = require('@opentelemetry/sdk-metrics-base'); const provider = new MeterProvider({ resource: new Resource({ 'service.name': 'opentelemetry-metrics-sample-dynatrace' }) }); //TODO Replace with the name of your application const reader = new PeriodicExportingMetricReader({ exporter, exportIntervalMillis: 1000 }); provider.addMetricReader(reader); const meter = provider.getMeter('my-meter'); //TODO Replace with the name of your meter const requestCounter = meter.createCounter('my-counter', { //TODO Replace with the name of your isntrument description: 'Example of a Counter', //TODO Replace with the description of your isntrument }); const upDownCounter = meter.createUpDownCounter('my-updowncounter', { //TODO Replace with the name of your isntrument description: 'Example of an UpDownCounter', //TODO Replace with the description of your isntrument });

Add the code below to any call you want to monitor. You can also add attributes to each measurement. The data is generated randomly and needs to be replaced by your data. In this case, we used a Counter and an UpDownCounter instrument. To find the instrument that fits your needs, see the official OpenTelemetry documentation.

js
const attributes = { "my-key-1": "my-value-1", "my-key-2": "my-value-2" }; //TODO Replace with your attributes requestCounter.add(Math.round(Math.random() * 1000), attributes); upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes);

2. Send data to Dynatrace

To send data to Dynatrace, you need to add the dependencies and then add and configure the code snippet below in your Node.js application code.

bash
npm install @dynatrace/opentelemetry-exporter-metrics
js
const { DynatraceMetricExporter } = require('@dynatrace/opentelemetry-exporter-metrics'); let exporter = new DynatraceMetricExporter({ url: '<URL>', apiToken: '<TOKEN>' });

To make sure your data arrives where it should be, you need to define the correct endpoint and token.

  • To set the endpoint
    1. Use your Environment ID to set the endpoint to which your app will send traces as follows:
      • Dynatrace SaaS https://{your-environment-id}.live.dynatrace.com/api/v2/metrics/ingest
      • Dynatrace Managed https://{your-domain}/e/{your-environment-id}/api/v2/metrics/ingest
    2. Replace <URL> in the code snippet above with your endpoint.
  • To create an authentication token
    1. In the Dynatrace menu, go to Access tokens and select Generate new token.
    2. Provide a Token name.
    3. In the Search scopes box, search for Ingest metrics (under API v2 scopes), and select the checkbox.
    4. Select Generate token.
    5. Select Copy to copy the token to your clipboard.
    6. Save the token in a safe place; you can't display it again.
    7. Replace <TOKEN> in the code snippet above with your token.

3. Verify that the traces are ingested into Dynatrace

A few minutes after restarting your app, look for your metrics:

  • In the Dynatrace menu, go to Metrics and filter for your metrics (by the names you have given them).
  • To get more detailed information, or to filter for different criteria, expand Details for a metric and select Create chart.

If your application does not receive any traffic, there will be no metrics.