Or you can set the environment variables in your .env file by using the dotenv library.
3
Create instrumentation.ts
Create an instrumentation.ts file under the src folder.
instrumentation.ts
import { Infrastack } from '@infrastack/otel';Infrastack.init({ serviceName: 'nestjs-otel-example',});
If you don’t provide a service name, we will create a random name for you.
4
Import the instrumentation module in your main.ts file
main.ts
import './instrumentation';import { NestFactory } from '@nestjs/core';import { AppModule } from './app.module';async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(8081); console.log('Server is running on port 8081');}bootstrap();
5
Run your application
Run your Nest.js application with the default Nest.js command.
npm run start
After creating some traffic, you can check your data from the infrastack.ai dashboard.
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
Import the instrumentation module in your main.ts file
main.ts
import './instrumentation';import { NestFactory } from '@nestjs/core';import { AppModule } from './app.module';async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(8081); console.log('Server is running on port 8081');}bootstrap();
5
Run your application
Run your Nest.js application with the default Nest.js command.
npm run start
After creating some traffic, you can check your data from the infrastack.ai dashboard.