## @leroymerlin/nestjs-opentelemetry

[Nest.js](https://nestjs.com/) module wrapper for [OpenTelemetry](https://opentelemetry.io).

## Installation

```bash
npm install --save @leroymerlin/nestjs-opentelemetry
```

## Usage

Import OpenTelemetryModule into the root AppModule and use the OpenTelemetryModule#forRoot() method to configure it.

```typescript
// app.module.ts

import { ExporterConfig, PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { OpenTelemetryModule, OpenTelemetryModuleOptions } from '@leroymerlin/nestjs-opentelemetry';
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
import { TraceIdRatioBasedSampler } from '@opentelemetry/core';
import { Module } from '@nestjs/common';

import { AppController } from './app.controller';
import { AppService } from './app.service';

const ignoreIncomingPaths = [/\/health/i, /\/api-docs/i];
const openTelemetryConfig = { endpoint: 'http://path.to.jaeger:14268/api/traces', samplerRatio: 1 };

const httpInstrumentation = new HttpInstrumentation({ ignoreIncomingPaths });
const sampler = new TraceIdRatioBasedSampler(openTelemetryConfig.samplerRatio);
const traceExporter = new JaegerExporter({ endpoint: openTelemetryConfig.endpoint });
const exporterConfig: ExporterConfig = { port: 9464, endpoint: 'metrics' };
const metricExporter = new PrometheusExporter(exporterConfig);

const openTelemetryModuleOptions: OpenTelemetryModuleOptions = {
  sampler,
  traceExporter,
  metricExporter,
  serviceName: 'application-name',
  instrumentations: [httpInstrumentation],
};

const openTelemetryDynamicModule = OpenTelemetryModule.forRoot(openTelemetryModuleOptions);

@Module({
  imports: [openTelemetryDynamicModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```

## License

@leroymerlin/nestjs-opentelemetry is [MIT](LICENSE).
