getsentry / getsentry/sentry-javascript
Expand SentryTracingInterceptor to ws context
- Vorherrschende Sprache
- TypeScript
- Sterne
- 8.7k
- Forks
- 1.8k
- Ø Merge
- 1 T. 17 Std.
- Gemergte PRs (30 T.)
- 515
Beschreibung
### Problem Statement
Nestjs supports [websocket gateway](https://docs.nestjs.com/websockets/gateways). It would be nice to have built in tracing interceptor like sentry has for http requests
### Solution Brainstorm
This is implementation which we wrote in our project
```typescript
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
import * as Sentry from '@sentry/core';
import { Observable } from 'rxjs';
import { tap } from 'rxjs';
export class SentryWsTracingInterceptor implements NestInterceptor {
/**
* Intercepts WS requests to set the transaction name for Sentry tracing.
*/
public intercept(
context: ExecutionContext,
next: CallHandler,
): Observable {
if (context.getType() === 'ws') {
const client = context.switchToWs().getClient();
const pattern = context.switchToWs().getPattern();
Sentry.getIsolationScope().setTransactionName(
`WS ${client.nsp.name} ${pattern}`,
);
return Sentry.startSpanManual(
{
op: 'ws',
name: `WS ${client.nsp.name} ${pattern}`,
},
(span) => {
return next.handle().pipe(
tap({
finalize: () => span.end(),
}),
);
},
);
}
return next.handle();
}
}
```
### Product Area
Profiling
Beitragsleitfaden
Bewertung
Dieses Issue wurde noch nicht bewertet.