getsentry / getsentry/sentry-javascript

Expand SentryTracingInterceptor to ws context

Aperta
#13,810 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Improvement Meta: Good First Issue Nest.js
Lingua principale
TypeScript
Stelle
8.7k
Fork
1.8k
Merge medio
1g 17h
PR unite (30g)
515

Descrizione

### 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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.