Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nestjs WS Trace Interceptor #13810

Open
Tracked by #12504 ...
CSenshi opened this issue Sep 25, 2024 · 2 comments
Open
Tracked by #12504 ...

Nestjs WS Trace Interceptor #13810

CSenshi opened this issue Sep 25, 2024 · 2 comments
Labels
Package: nestjs Issues related to the Sentry Nestjs SDK

Comments

@CSenshi
Copy link

CSenshi commented Sep 25, 2024

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 * 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<unknown> {
    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

@getsantry
Copy link

getsantry bot commented Sep 25, 2024

Assigning to @getsentry/support for routing ⏲️

@InterstellarStella InterstellarStella transferred this issue from getsentry/sentry Sep 26, 2024
@getsantry getsantry bot moved this from Waiting for: Support to Waiting for: Product Owner in GitHub Issues with 👀 3 Sep 26, 2024
@chargome chargome added the Package: nestjs Issues related to the Sentry Nestjs SDK label Sep 26, 2024
@chargome chargome mentioned this issue Sep 26, 2024
@chargome
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nestjs Issues related to the Sentry Nestjs SDK
Projects
Status: Waiting for: Product Owner
Development

No branches or pull requests

2 participants