Skip to content

Add request information to Angular ssr events #12757

Closed as not planned
Closed as not planned

Description

Problem Statement

We run Angular in ssr mode and use Sentry to track errors. The errors themselves come through, but they don't have any metadata associated, since the angular sdk expects to run in a browser.
image

It would be great, if Sentry would automagically collect metadata from the ssr environment.

Solution Brainstorm

We managed to get some meta information added but had to jump through quite a few hoops:

  1. Inject node integration token into angular:
    tokens.ts
import { type Integration } from '@sentry/types';

export const SSR_SENTRY_INTEGRATIONS = new InjectionToken<Integration[]>(
    'SSR_SENTRY_INTEGRATIONS'
);

server.ts

import * as Sentry from '@sentry/node';
...
            await commonEngine.render({
                ....
                providers: [
                    { provide: APP_BASE_HREF, useValue: baseUrl },
                    { provide: RESPONSE, useValue: res },
                    { provide: REQUEST, useValue: req },
                    {
                        provide: SSR_SENTRY_INTEGRATIONS,
                        useValue: [
                            sentry.requestDataIntegration(),
                            sentry.nodeContextIntegration(),
                        ],
                    },
                ],
            });
  1. Extend the sentry ErrorHandler
    constructor(
        @Optional() @Inject(SSR_SENTRY_INTEGRATIONS) private ssrIntegrations: Integration[],
        @Optional() @Inject(REQUEST) private request: Request
    ){ 
        super();
        if(this.ssrIntegrations){
          for (const integration of this.ssrIntegrations) {
            Sentry.addIntegration(integration);
          }
        }
    }

   handleError(error: any) {
      if (this.request) {
          Sentry
              .getIsolationScope()
              .setSDKProcessingMetadata({
                  request: this.request,
              });
      }
      super().handleError(error)
   }

The main challenge was that if we loaded the node integrations directly(or even lazy), we would get compilation errors, since the frontend code would complain that there's no node api. By injecting the integrations from server.ts, we managed to avoid those errors, since sentry node will never appear in a browser bundle.

I hope that makes some sense but I have no idea, how viable it is to integrate into the sdk.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Projects

  • Status

    Waiting for: Community

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions