NodeJS

Send traces to Aspecto directly from your code using exporter-trace-otlp-proto .

Step 1 - Install Dependencies

npm install @opentelemetry/api \
    @opentelemetry/sdk-trace-base \
    @opentelemetry/sdk-trace-node \
    @opentelemetry/resources \
    @opentelemetry/semantic-conventions \
    @opentelemetry/exporter-trace-otlp-proto \
    @opentelemetry/instrumentation

Step 2 - Install Instrumentation Libraries

Install all instrumentation libraries with the @opentelemetry/auto-instrumentations-node package along with other common instrumentations:

npm install @opentelemetry/auto-instrumentations-node \
    opentelemetry-instrumentation-elasticsearch \
    opentelemetry-instrumentation-express \
    opentelemetry-instrumentation-kafkajs \
    opentelemetry-instrumentation-neo4j \
    opentelemetry-instrumentation-sequelize \
    opentelemetry-instrumentation-typeorm

Step 3 - Add OpenTelemetry SDK Setup Code

Add this code to your application in a file named opentelemetry.ts

This code contains parts you need to fill in yourself in step 4

import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { registerInstrumentations } from '@opentelemetry/instrumentation';

// instrumentation libraries
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { ExpressInstrumentation } from 'opentelemetry-instrumentation-express';
import { ElasticsearchInstrumentation } from 'opentelemetry-instrumentation-elasticsearch';
import { KafkaJsInstrumentation } from 'opentelemetry-instrumentation-kafkajs';
import { Neo4jInstrumentation } from 'opentelemetry-instrumentation-neo4j';
import { SequelizeInstrumentation } from 'opentelemetry-instrumentation-sequelize';
import { TypeormInstrumentation } from 'opentelemetry-instrumentation-typeorm';

const aspectoToken = '--YOUR ASPECTO TOKEN--';
const serviceName = '--YOUR SERVICE NAME--';

const provider = new NodeTracerProvider({
    resource: new Resource({
        [SemanticResourceAttributes.SERVICE_NAME]: serviceName
    }),
});

provider.register();
provider.addSpanProcessor(
    new BatchSpanProcessor(
        new OTLPTraceExporter({
            url: 'https://otelcol.aspecto.io/v1/traces',
            headers: {
                // Aspecto API-Key is required
                Authorization: aspectoToken
            }
        })
    )
);

registerInstrumentations({
  instrumentations: [
    getNodeAutoInstrumentations({
        // some instrumentation is noisy and commonly not useful
        '@opentelemetry/instrumentation-fs': {
            enabled: false
        },
        '@opentelemetry/instrumentation-net': {
            enabled: false
        },
        '@opentelemetry/instrumentation-dns': {
            enabled: false
        },
        '@opentelemetry/instrumentation-express': {
            enabled: false
        },
    }),
    new ExpressInstrumentation(),
    new ElasticsearchInstrumentation(),
    new KafkaJsInstrumentation(),
    new Neo4jInstrumentation(),
    new SequelizeInstrumentation(),
    new TypeormInstrumentation(),
  ],
});

Step 4 - Configure the Installation

Fill in the following in the code above in order to export traces to Aspecto:

1) aspectoToken - You can get your token here.

2) serviceName - The name of your web application. You will be able to filter and search for it in Aspecto trace viewer and use it for sampling rules and alerts.

Step 5 - Register OpenTelemetry in Your Code

Invoke the code above in your main index.ts file by importing / requireing it as early as possible.

import './opentelemetry'; // you may need to change the import depending on where you saved `opentelemetry.ts`

Step 6 - View Traces

Start your service and invoke it. Go to Aspecto app and view your nodejs traces.

Last updated