From harness-claude
Sets up OpenTelemetry Node.js SDK with resource attributes, OTLP trace/metric exporters, and auto-instrumentation for HTTP, DB, frameworks. Use for new apps or vendor SDK migrations.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Initialize the OpenTelemetry Node.js SDK with resource attributes, exporters, and auto-instrumentation
Guides OpenTelemetry instrumentation setup for traces, metrics, logs including spans, resources, SDKs for Node.js, Python, Java, Go, .NET, Ruby, PHP, Next.js, browser, and Kubernetes best practices.
Configures OTLP exporters for OpenTelemetry traces, metrics, and logs to backends like Jaeger, Grafana Tempo, Datadog or via Collectors. Covers direct export, batching, retries, and YAML pipelines.
Auto-instruments Node.js apps with Azure Monitor OpenTelemetry for distributed tracing, metrics, and logs. Enables observability via env vars and config options like sampling and instrumentation libraries.
Share bugs, ideas, or general feedback.
Initialize the OpenTelemetry Node.js SDK with resource attributes, exporters, and auto-instrumentation
instrumentation.ts) — it must run before any other application code.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"
}
}
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:
@opentelemetry/instrumentation-http — HTTP/HTTPS client and server@opentelemetry/instrumentation-express — Express routes and middleware@opentelemetry/instrumentation-pg — PostgreSQL queries@opentelemetry/instrumentation-redis — Redis commands@opentelemetry/instrumentation-mongodb — MongoDB operations@opentelemetry/instrumentation-grpc — gRPC callsauto-instrumentations-nodeEnvironment variables (standard):
OTEL_SERVICE_NAME — service name (overrides code)OTEL_EXPORTER_OTLP_ENDPOINT — collector URLOTEL_EXPORTER_OTLP_PROTOCOL — grpc or http/protobufOTEL_TRACES_SAMPLER — always_on, always_off, traceidratioOTEL_TRACES_SAMPLER_ARG — sampler argument (e.g., 0.1 for 10%)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:
sdk.shutdown() on process exit (traces may be lost)/v1/traces)https://opentelemetry.io/docs/languages/js/getting-started/nodejs/