Last active
March 21, 2026 12:28
-
-
Save victorfernandesraton/8ccd4929a7425bbba5741624057ffc39 to your computer and use it in GitHub Desktop.
Opentelemetry autoconfig for fastify based project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { NodeSDK } from "@opentelemetry/sdk-node"; | |
| import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; | |
| import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http"; | |
| import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics"; | |
| import { resourceFromAttributes } from "@opentelemetry/resources"; | |
| import { | |
| ATTR_SERVICE_NAME, | |
| ATTR_SERVICE_VERSION, | |
| } from "@opentelemetry/semantic-conventions"; | |
| import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node"; | |
| import { FastifyOtelInstrumentation } from "@fastify/otel"; | |
| import { W3CTraceContextPropagator } from "@opentelemetry/core"; | |
| import { env } from "node:process"; | |
| import pkg from "../package.json" with { type: "json" }; | |
| if (env.OTEL_EXPORTER_OTLP_ENDPOINT && env.OTEL_EXPORTER_OTLP_ENDPOINT != "") { | |
| const traceExporter = new OTLPTraceExporter({ | |
| url: `${env.OTEL_EXPORTER_OTLP_ENDPOINT}/v1/traces`, | |
| }); | |
| const metricReader = new PeriodicExportingMetricReader({ | |
| exporter: new OTLPMetricExporter({ | |
| url: `${env.OTEL_EXPORTER_OTLP_ENDPOINT}/v1/metrics`, | |
| }), | |
| exportIntervalMillis: 10000, | |
| }); | |
| const resource = resourceFromAttributes({ | |
| [ATTR_SERVICE_NAME]: pkg.name, | |
| [ATTR_SERVICE_VERSION]: pkg.version, | |
| }); | |
| const sdk = new NodeSDK({ | |
| resource, | |
| textMapPropagator: new W3CTraceContextPropagator(), | |
| traceExporter, | |
| metricReader, | |
| instrumentations: [ | |
| new FastifyOtelInstrumentation({ | |
| registerOnInitialization: true, | |
| }), | |
| ...getNodeAutoInstrumentations({ | |
| "@opentelemetry/instrumentation-fs": { enabled: false }, | |
| "@opentelemetry/instrumentation-dns": { enabled: false }, | |
| "@opentelemetry/instrumentation-net": { enabled: false }, | |
| "@opentelemetry/instrumentation-pino": { enabled: false }, | |
| "@opentelemetry/instrumentation-http": { | |
| enabled: true, | |
| }, | |
| "@opentelemetry/instrumentation-amqplib": {enabled: true} | |
| }), | |
| ], | |
| }); | |
| sdk.start(); | |
| console.log("[OTEL] SDK started successfully"); | |
| } else { | |
| console.log("[OTEL] SDK disabled"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment