Otel sdk setup

From Intense-Visions/harness-engineering · View interactive version

Otel sdk setup is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Initialize the OpenTelemetry Node.js SDK with resource attributes, exporters, and auto-instrumentation

How to use this skill

Skill file

OpenTelemetry SDK Setup

Initialize the OpenTelemetry Node.js SDK with resource attributes, exporters, and auto-instrumentation

When to Use

Instructions

  1. Install the core SDK and auto-instrumentation packages.
  2. Create the instrumentation file (instrumentation.ts) — it must run before any other application code.
  3. Define the resource with service name, version, and environment.
  4. Configure exporters for traces, metrics, and logs (OTLP is the standard protocol).
  5. Register auto-instrumentations for libraries your application uses.
  6. Load the instrumentation file first: node --require ./instrumentation.ts or import './instrumentation' at the top of your entry point.
npm install @opentelemetry/sdk-node \
  @opentelemetry/auto-instrumentations-node \
  @opentelemetry/exporter-trace-otlp-http \
  @opentelemetry/exporter-metrics-otlp-http \
  @opentelemetry/resources \
  @opentelemetry/semantic-conventions
// instrumentation.ts — must be loaded before application code
import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { Resource } from '@opentelemetry/resources';
import {
  ATTR_SERVICE_NAME,
  ATTR_SERVICE_VERSION,
  ATTR_DEPLOYMENT_ENVIRONMENT,
} from '@opentelemetry/semantic-conventions';

const resource = new Resource({
  [ATTR_SERVICE_NAME]: process.env.OTEL_SERVICE_NAME || 'my-service',
  [ATTR_SERVICE_VERSION]: process.env.npm_package_version || '0.0.0',
  [ATTR_DEPLOYMENT_ENVIRONMENT]: process.env.NODE_ENV || 'development',
});

const sdk = new NodeSDK({
  resource,
  traceExporter: new OTLPTraceExporter({
    url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://localhost:4318/v1/traces',
  }),
  metricReader: new PeriodicExportingMetricReader({
    exporter: new OTLPMetricExporter({
      url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://localhost:4318/v1/metrics',
    }),
    exportIntervalMillis: 15000,
  }),
  instrumentations: [
    getNodeAutoInstrumentations({
      // Disable noisy instrumentations
      '@opentelemetry/instrumentation-fs': { enabled: false },
      '@opentelemetry/instrumentation-dns': { enabled: false },
    }),
  ],
});

sdk.start();

// Graceful shutdown
process.on('SIGTERM', () => {
  sdk.shutdown().then(
    () => console.log('Telemetry shut down'),
    (err) => console.error('Telemetry shutdown error', err)
  );
});
// package.json — load instrumentation first
{
  "scripts": {
    "start": "node --require ./dist/instrumentation.js ./dist/index.js",
    "dev": "tsx --require ./src/instrumentation.ts ./src/index.ts"
  }
}

Details

Load order is critical. OpenTelemetry hooks into library internals (HTTP, pg, express) by monkey-patching. If you import express before OpenTelemetry initializes, the instrumentation will not work. Always load instrumentation.ts first.

Auto-instrumentations included:

Environment variables (standard):

Local development: Run the OpenTelemetry Collector and Jaeger locally:

docker run -d -p 4318:4318 -p 16686:16686 jaegertracing/all-in-one:latest

Then view traces at http://localhost:16686.

Common mistakes:

Source

https://opentelemetry.io/docs/languages/js/getting-started/nodejs/

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.