getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.89k stars 1.56k forks source link

Nestjs WS Trace Interceptor #13810

Open CSenshi opened 4 days ago

CSenshi commented 4 days ago

Problem Statement

Nestjs supports websocket gateway. 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

import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
import { getIsolationScope } from '@sentry/core';
import { Observable } from 'rxjs';

export class SentryWsTracingInterceptor implements NestInterceptor {
  public intercept(
    context: ExecutionContext,
    next: CallHandler,
  ): Observable<unknown> {
    if (context.getType() === 'ws') {
      const client = context.switchToWs().getClient();
      const pattern = context.switchToWs().getPattern();
      getIsolationScope().setTransactionName(
        `WS ${client.nsp.name} ${pattern}`,
      );
    }

    return next.handle();
  }
}

Product Area

Profiling

getsantry[bot] commented 4 days ago

Assigning to @getsentry/support for routing ⏲️

chargome commented 3 days ago

Hi @CSenshi, thank you – sounds like a reasonable improvement. But I would suggest we include this functionality in the existing SentryTracingInterceptor. I put this on our list of improvements for nestjs