# @nestpro/dd-trace

> NestJS Datadog Trace Library

Latest version **10.3.1** (published 2024-06-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nestpro/dd-trace
pnpm add @nestpro/dd-trace
yarn add @nestpro/dd-trace
bun add @nestpro/dd-trace
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 10.3.1 |
| Published | 2024-06-19 |
| First published | 2023-08-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= v18 |
| Dependencies | 0 |
| Unpacked size | 26.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | wonshikkim.kr@gmail.com |
| Maintainers | cojack |
| Keywords | nestjs, datadog, ddtrace, dd-trace, nest.js |

## Links

- npm: https://www.npmjs.com/package/@nestpro/dd-trace
- Repository: https://github.com/cojack/nestjs-ddtrace
- Homepage: https://github.com/cojack/nestjs-ddtrace#readme
- Issues: https://github.com/cojack/nestjs-ddtrace/issues
- npm.io page: https://npm.io/package/@nestpro/dd-trace

## Recent versions

- 10.3.1 (latest) — 2024-06-19
- 10.3.0 — 2024-06-18
- 10.0.4 — 2023-08-21
- 10.0.3 — 2023-08-15
- 10.0.2 — 2023-08-15

## README

# NestJS Datadog Trace

dd-trace 5.x
nest 10.3

## Install

```sh
npm i @nestpro/dd-trace
```

## Setup

1. Create tracing file (tracing.ts):

   ```ts
   import DataDogTrace from 'dd-trace';
   
   const tracer = DataDogTrace.init({
     logInjection: true,
   });
   
   export { tracer };
   ```
   
   Use already initialized DataDog tracer instance


2. Import the tracing file:

    ```ts
    import './tracing';
    import { NestFactory } from '@nestjs/core';
    import { AppModule } from './app.module';
    import { Logger as PinoLogger } from 'nestjs-pino';
    import { Logger } from '@nestjs/common';

    async function bootstrap() {
      const app = await NestFactory.create(AppModule, { bufferLogs: true });
      app.useLogger(app.get(PinoLogger));

      const logger = new Logger('main');
      const port = process.env.PORT || 3000;
      await app.listen(3000).then(() => {
        logger.log(`Listening on port: ${port}`);
      });
    }
    bootstrap();
    ```

   Use `APP_` prefix for your application configuration environment variables.


3. Add *LoggerModule* and *DatadogModule* to *AppModule*:

    ```ts
    import { Module } from '@nestjs/common';
    import { LoggerModule } from 'nestjs-pino';
    import { DatadogTraceModule } from 'nestjs-ddtrace';

    @Module({
      imports: [
        LoggerModule.forRoot({
          pinoHttp: {
            level: process.env.APP_ENV !== 'prod' ? 'trace' : 'info'
          },
          exclude: [{ method: RequestMethod.ALL, path: 'health' }], // if you have /health path, exclude it from logs
        }), 
        DatadogTraceModule.forRoot({
          controllers: true,
          providers: true
        })
      ],
    })
    export class AppModule {}
    ```

## Span Decorator

If you need, you can define a custom Tracing Span for a method or class. It works async or sync. Span takes its name from the parameter; but by default, it is the same as the method's name.

```ts
import { DatadogTraceModule } from 'nestjs-ddtrace';

@Module({
  imports: [DatadogTraceModule.forRoot()],
})

export class AppModule {}
```

### Tracing Service

In case you need to access native span methods for special logics in the method block:

```ts
import { Span, TraceService } from 'nestjs-ddtrace';

@Injectable()
export class BookService {
  constructor(private readonly traceService: TraceService) {}

  @Span()
  async getBooks() {
    const currentSpan = this.traceService.getActiveSpan(); // --> retrives current span, comes from http or @Span
    await this.doSomething();
    currentSpan.addTags({
      'getBooks': 'true'
    });

    const childSpan = this.traceService.getTracer().startSpan('ms', {childOf: currentSpan});
    childSpan.setTag('userId', 1);
    await this.doSomethingElse();
    childSpan.finish(); // new span ends

    try {
      doSomething();
    } catch (e) {
      currentSpan.setTag('error', e);
      throw e;
    }
    return [`Harry Potter and the Philosopher's Stone`];
  }
}
```

```ts
import { Span } from 'nestjs-ddtrace';

@Injectable()
@Span()
export class BookService {
  async getBooks() { /* ... */ }
  async deleteBook(id: string) { /* ... */ }
}

@Controller()
@Span()
export class HelloController {
  @Get('/books')
  getBooks() { /* ... */ }

  @Delete('/books/:id')
  deleteBooks() { /* ... */ }
}
```

## Miscellaneous

Inspired by the [nestjs-otel](https://github.com/pragmaticivan/nestjs-otel) and [nestjs-opentelemetry](https://github.com/MetinSeylan/Nestjs-OpenTelemetry#readme) repository.

---
_Source: https://npm.io/package/@nestpro/dd-trace · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
