|
| 1 | +# Integration with Sentry |
| 2 | + |
| 3 | +If you are using Sentry as your Error Tracker, you can use this recipe to trace and log actions as *breadcrumbs*. |
| 4 | + |
| 5 | +First you'll need to initialize Sentry in your app using `Sentry.init(...)` in the `main.ts` file. |
| 6 | + |
| 7 | +```ts |
| 8 | +Sentry.init({ |
| 9 | + dsn: ... |
| 10 | + // ... |
| 11 | +}); |
| 12 | +``` |
| 13 | + |
| 14 | +Next, you need to provide `Sentry`'s `ErrorHandler` and `TraceService`. |
| 15 | + |
| 16 | +```ts |
| 17 | +// app.module.ts |
| 18 | +@NgModule({ |
| 19 | + providers: [ |
| 20 | + // ... |
| 21 | + { |
| 22 | + provide: ErrorHandler, |
| 23 | + useValue: Sentry.createErrorHandler(), |
| 24 | + }, |
| 25 | + { |
| 26 | + provide: Sentry.TraceService, |
| 27 | + deps: [Router], |
| 28 | + }, |
| 29 | + { |
| 30 | + provide: APP_INITIALIZER, |
| 31 | + useFactory: () => () => {}, |
| 32 | + deps: [Sentry.TraceService], |
| 33 | + multi: true, |
| 34 | + }, |
| 35 | + ], |
| 36 | +}) |
| 37 | +export class AppModule {} |
| 38 | + |
| 39 | +//app.config.ts |
| 40 | +export const appConfig: ApplicationConfig = { |
| 41 | + providers: [ |
| 42 | + // ... |
| 43 | + { |
| 44 | + provide: ErrorHandler, |
| 45 | + useValue: Sentry.createErrorHandler(), |
| 46 | + }, |
| 47 | + { |
| 48 | + provide: Sentry.TraceService, |
| 49 | + deps: [Router], |
| 50 | + }, |
| 51 | + { |
| 52 | + provide: APP_INITIALIZER, |
| 53 | + useFactory: () => () => {}, |
| 54 | + deps: [Sentry.TraceService], |
| 55 | + multi: true, |
| 56 | + }, |
| 57 | + ], |
| 58 | +}; |
| 59 | +//or |
| 60 | +bootstrapApplication(App, { |
| 61 | + providers: [ |
| 62 | + //... |
| 63 | + provideAppInitializer(() => inject(Sentry.TraceService)) |
| 64 | + ], |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +Now, let's go over how to log NGXS actions as breadcrumbs. |
| 69 | +Create a service `NgxsSentryBreadcrumbsService`. |
| 70 | +This service uses NGXS `Actions` dependency to listen to all events and send them to Sentry using `Sentry.addBreadcrumb`. |
| 71 | + |
| 72 | +```ts |
| 73 | +@Injectable({providedIn:root}) |
| 74 | +export class NgxsSentryBreadcrumbsService implements OnDestroy { |
| 75 | + #actions = inject(Actions); |
| 76 | + #destroyed$ = new Subject<void>(); |
| 77 | + |
| 78 | + constructor() { |
| 79 | + this.#subscribeToActions(); |
| 80 | + } |
| 81 | + |
| 82 | + #subscribeToActions() { |
| 83 | + const excludeAction = [] //You can add the actions you want to exclude from your breadcrumbs here; |
| 84 | + this.#actions.subscribe( |
| 85 | + (ctx) => { |
| 86 | + const actionType = getActionTypeFromInstance(ctx.action); |
| 87 | + if (actionType && !excludeActions.some((excludeAction) => actionType.startsWith(excludeAction))) { |
| 88 | + Sentry.addBreadcrumb({ |
| 89 | + category: 'NGXS', |
| 90 | + message: `${actionType} ${ctx.status}`, |
| 91 | + level: 'info', |
| 92 | + type: 'info', |
| 93 | + data: typeof ctx.action === 'string' ? { data: ctx.action } : ctx.action, |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + ngOnDestroy() { |
| 101 | + this.#destroyed$.next(); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Lastly provide the `NgxsSentryBreadcrumbsService` and include it as dependency of the `APP_INITIALIZER`, this will allow to instantiate the service when the app initializes and starts monitoring and tracing actions as breadcrumns to Sentry. |
| 107 | + |
| 108 | +```ts |
| 109 | +{ |
| 110 | + provide: NgxsSentryBreadcrumbsService, |
| 111 | + useClass: NgxsSentryBreadcrumbsService, |
| 112 | +}, |
| 113 | +{ |
| 114 | + provide: APP_INITIALIZER, |
| 115 | + useFactory: () => () => {}, |
| 116 | + deps: [NgxsSentryBreadcrumbsService], |
| 117 | + multi: true, |
| 118 | +} |
| 119 | +// or |
| 120 | +bootstrapApplication(App, { |
| 121 | + providers: [ |
| 122 | + //... |
| 123 | + provideAppInitializer(() => inject(NgxsSentryBreadcrumbsService)) |
| 124 | + ], |
| 125 | +}); |
| 126 | +``` |
| 127 | + |
| 128 | +That's it! This is a simple implementation on how to integrate NGXS and Sentry to trace actions as breadcrumbs. This will result in Sentry displaying the executed actions previous to an error. |
| 129 | + |
| 130 | + |
0 commit comments