Node + OpenTelemetry

Configure a node service to send trace data to CodeSee

Summary: you can see a complete working example at our Github example repo here:
https://github.com/Codesee-io/examples/tree/3017ca4d40d3c4609ed01f6314ad60fe0c437c56/serverless-lambda-otel-nodejs-lib-example

To start sending data to CodeSee Service Maps from your Node application, first ensure you have the SDK package and OpenTelemetry API installed:

npm install \
    @grpc/grpc-js \
    @opentelemetry/api \
    @opentelemetry/auto-instrumentations-node \
    @opentelemetry/exporter-trace-otlp-grpc \
    @opentelemetry/sdk-node
yarn add \
    @grpc/grpc-js \
    @opentelemetry/api \
    @opentelemetry/auto-instrumentations-node \
    @opentelemetry/exporter-trace-otlp-grpc \
    @opentelemetry/sdk-node

Create a tracing.js|ts file. Here's an example file, with a couple blanks to fill:

  1. Be sure to enter your CodeSee Ingestion Token into the Metadata config
  2. Set the SERVICE_NAME for this service. This is a unique identifier that should remain unchanged across different environments and releases.
  3. Set the DEPLOYMENT_ENVIRONMENT for this service. Each deployment environment is treated as a separate Service Map
  4. (optional) Set the SERVICE_VERSION for this service. Each release should get a new version string.
import * as grpc from "@grpc/grpc-js";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-grpc";
import { Resource } from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";

// For troubleshooting, set the log level to DiagLogLevel.DEBUG
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const codeseeToken = process.env.CODESEE_TOKEN; // !! CodeSee auth token
const metadata = new grpc.Metadata();
metadata.set('Authorization', `Bearer ${codeseeToken}`);

const sdk = new NodeSDK({
  traceExporter: new OTLPTraceExporter({
    url: "https://in-otel.codesee.io:443/v1/traces", // CodeSee endpoint
    credentials: grpc.credentials.createSsl(),
    metadata,
  }),
  resource: Resource.default().merge(
    new Resource({
      [SemanticResourceAttributes.SERVICE_NAME]: process.env.SERVICE_NAME,  // !! NAME YOUR SERVICE !!
      [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: process.env.DEPLOYMENT_ENVIRONMENT, // !! SET YOUR ENVIRONMENT
      [SemanticResourceAttributes.SERVICE_VERSION]: process.env.SERVICE_VERSION, // (optional) set version
    })
  ),
  instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();

const grpc = require('@grpc/grpc-js');
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-grpc");
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');

// For troubleshooting, set the log level to DiagLogLevel.DEBUG
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const codeseeToken = process.env.CODESEE_TOKEN; // !! CodeSee auth token
const metadata = new grpc.Metadata();
metadata.set('Authorization', `Bearer ${codeseeToken}`);

const sdk = new NodeSDK({
  traceExporter: new OTLPTraceExporter({
    url: "https://in-otel.codesee.io:443/v1/traces", // CodeSee endpoint
    credentials: grpc.credentials.createSsl(),
    metadata,
  }),
  resource: Resource.default().merge(
    new Resource({
      [SemanticResourceAttributes.SERVICE_NAME]: process.env.SERVICE_NAME,  // !! NAME YOUR SERVICE !!
      [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: process.env.DEPLOYMENT_ENVIRONMENT, // !! SET YOUR ENVIRONMENT
      [SemanticResourceAttributes.SERVICE_VERSION]: process.env.SERVICE_VERSION, // (optional) set version
    })
  ),
  instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();

Finally, either ensure that tracing.js|ts is required in your node invocation. For example:

ts-node --require ./tracing.ts <app-file.ts>
node --require ./tracing.js <app-file.js>

Or else require tracing.js|ts as soon as possible in your entry point file.

import "./tracing";
...
require("./tracing");

📘

Open Telemetry Javascript Documentation

To customize your OpenTelemetry configuration further, explore the official docs.

Otel JS Home: https://opentelemetry.io/docs/instrumentation/js/