Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,74 @@ import { DatadogTraceModule } from 'nestjs-ddtrace';
export class AppModule {}
```

## Exception Filtering

You can filter which exceptions are recorded in spans using the `exceptionFilter` option. This is useful for excluding recoverable errors, expected exceptions, or specific error types from your traces.

The filter function receives the error, span name, and method name, and should return `true` to record the exception or `false` to skip it.

### Basic Exception Filtering

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

@Module({
imports: [DatadogTraceModule.forRoot({
controllers: true,
providers: true,
exceptionFilter: (error, spanName, methodName) => {
// Skip recording 404 errors
if (error && typeof error === 'object' && 'status' in error) {
return error.status !== 404;
}

// Record all other exceptions
return true;
}
})],
})
export class AppModule {}
```

### Advanced Exception Filtering

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

@Module({
imports: [DatadogTraceModule.forRoot({
controllers: true,
providers: true,
exceptionFilter: (error, spanName, methodName) => {
// Skip client errors (4xx) but record server errors (5xx)
if (error && typeof error === 'object' && 'status' in error) {
const status = (error as any).status;
if (status >= 400 && status < 500) {
return false; // Don't record 4xx errors
}
}

// Skip validation errors in user service methods
if (spanName.includes('UserService') &&
error instanceof Error &&
error.name === 'ValidationError') {
return false;
}

// Skip expected business logic errors
if (error instanceof Error &&
error.message.includes('EXPECTED_')) {
return false;
}

// Record everything else
return true;
}
})],
})
export class AppModule {}
```

## Miscellaneous

Inspired by the [nestjs-otel](https://github.com/pragmaticivan/nestjs-otel) and [nestjs-opentelemetry](https://github.com/MetinSeylan/Nestjs-OpenTelemetry#readme) repository.
13 changes: 13 additions & 0 deletions src/datadog-trace-module-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ExceptionFilter } from './exception-filter.types';

export interface DatadogTraceModuleOptions {
/**
* if true, automatically add a span to all controllers.
Expand All @@ -15,4 +17,15 @@ export interface DatadogTraceModuleOptions {
* list of provider names to exclude when controllers option is true.
*/
excludeProviders?: string[];
/**
* Optional filter function to determine which exceptions should be recorded in spans.
* Returns true if the exception should be recorded, false to skip recording.
* If not provided, all exceptions will be recorded (default behavior).
*
* @param error - The error/exception that was thrown (can be any type)
* @param spanName - The name of the span where the error occurred
* @param methodName - The name of the method where the error occurred
* @returns boolean indicating whether to record the exception in the span
*/
exceptionFilter?: ExceptionFilter;
}
Loading