getsentry / getsentry/sentry-javascript

Expand SentryTracingInterceptor to ws context

Abierto
#13,810 2 comentarios 0 reacciones 0 asignados Ver en GitHub
Improvement Meta: Good First Issue Nest.js
Lenguaje dominante
TypeScript
Estrellas
8.7k
Forks
1.8k
Merge medio
1 d 17 h
PR fusionados (30 d)
515

Descripción

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

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.