Or you can set the environment variables in your .env file by using the dotenv library.
3
Create instrumentation.ts
Here, you will create a file that will be used to instrument your application. You will have lower level control over the instrumentation process.
instrumentation.ts
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';import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base';import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';// Exporter options with compression set to Gzipconst exporterOptions = { compression: CompressionAlgorithm.GZIP,};// Trace exporter initializationconst traceExporter = new OTLPTraceExporter(exporterOptions);// SDK initialization with exporter, auto-instrumentation, and resource attributesconst sdk = new NodeSDK({ traceExporter, instrumentations: [getNodeAutoInstrumentations()], resource: new Resource({ [ATTR_SERVICE_NAME]: "{{YOUR_SERVICE_NAME}}", [ATTR_SERVICE_VERSION]: "{{YOUR_SERVICE_VERSION}}", }),});// Start the SDK and register it with the OpenTelemetry APIsdk.start();// Graceful shutdown of SDK on process exitprocess.on("SIGTERM", () => { sdk .shutdown() .then(() => console.log("Tracing terminated")) .catch((error) => console.log("Error terminating tracing", error)) .finally(() => process.exit(0));});
4
Build and run your application
Run your Express.js application with the node required command.