|
| 1 | +This snippet includes automatic instrumentation to [monitor the performance](/platforms/javascript/performance/) of your application, which registers and configures the Tracing integration, including custom Angular routing instrumentation. |
| 2 | + |
| 3 | +**Note:** To reduce the volume of performance data captured, or disable it entirely, change `tracesSampleRate` to a value between 0 and 1 in production. |
| 4 | + |
| 5 | +```javascript |
| 6 | +import { enableProdMode } from "@angular/core"; |
| 7 | +import { platformBrowserDynamic } from "@angular/platform-browser-dynamic"; |
| 8 | +import * as Sentry from "@sentry/angular"; |
| 9 | +import { Integrations } from "@sentry/tracing"; |
| 10 | +import { AppModule } from "./app/app.module"; |
| 11 | + |
| 12 | +Sentry.init({ |
| 13 | + dsn: "___PUBLIC_DSN___" , |
| 14 | + integrations: [ |
| 15 | + new Integrations.BrowserTracing({ |
| 16 | + tracingOrigins: ["localhost", "https://yourserver.io/api"], |
| 17 | + routingInstrumentation: Sentry.routingInstrumentation, |
| 18 | + }), |
| 19 | + ], |
| 20 | + tracesSampleRate: 1.0, // We recommend adjusting this in production |
| 21 | +}); |
| 22 | + |
| 23 | + |
| 24 | +platformBrowserDynamic() |
| 25 | + .bootstrapModule(AppModule) |
| 26 | + .then(success => console.log(`Bootstrap success`)) |
| 27 | + .catch(err => console.error(err)); |
| 28 | +``` |
| 29 | + |
| 30 | +You can also configure `@sentry/angular` to catch any Angular-specific exceptions reported through the [@angular/core/ErrorHandler](https://angular.io/api/core/ErrorHandler) provider. |
| 31 | + |
| 32 | +`@sentry/angular` exports a Trace Service, Directive, and Decorators that leverages the `@sentry/tracing`, Sentry's Tracing integration, to add Angular-related spans to transactions. The service itself tracks route changes and durations, where directive and decorators are tracking component initializations. |
| 33 | + |
| 34 | +### Automatically Send Errors with `ErrorHandler` |
| 35 | + |
| 36 | +`@sentry/angular` exports a function to instantiate an `ErrorHandler` provider that will automatically send JavaScript errors captured by the Angular's error handler. |
| 37 | + |
| 38 | +```javascript |
| 39 | +import { NgModule, ErrorHandler } from "@angular/core"; |
| 40 | +import * as Sentry from "@sentry/angular"; |
| 41 | + |
| 42 | +@NgModule({ |
| 43 | + // ... |
| 44 | + providers: [ |
| 45 | + { |
| 46 | + provide: ErrorHandler, |
| 47 | + useValue: Sentry.createErrorHandler({ |
| 48 | + showDialog: true, |
| 49 | + }), |
| 50 | + }, |
| 51 | + ], |
| 52 | + // ... |
| 53 | +}) |
| 54 | +export class AppModule {} |
| 55 | +``` |
| 56 | + |
| 57 | +You can configure the behavior of `createErrorHandler`. For more details see the `ErrorHandlerOptions` interface in [our repository](https://github.com/getsentry/sentry-javascript/blob/master/packages/angular/src/errorhandler.ts). |
| 58 | + |
| 59 | +### Register `TraceService` |
| 60 | + |
| 61 | +In Angular's DI system, register `TraceService` as a provider with a `Router` as its dependency: |
| 62 | + |
| 63 | +```javascript |
| 64 | +import { NgModule } from "@angular/core"; |
| 65 | +import { Router } from "@angular/router"; |
| 66 | +import * as Sentry from "@sentry/angular"; |
| 67 | + |
| 68 | +@NgModule({ |
| 69 | + // ... |
| 70 | + providers: [ |
| 71 | + { |
| 72 | + provide: Sentry.TraceService, |
| 73 | + deps: [Router], |
| 74 | + }, |
| 75 | + ], |
| 76 | + // ... |
| 77 | +}) |
| 78 | +export class AppModule {} |
| 79 | +``` |
| 80 | +Then, either require the `TraceService` from inside `AppModule` or use `APP_INITIALIZER` to force instantiate Tracing. |
| 81 | +```javascript |
| 82 | +@NgModule({ |
| 83 | + // ... |
| 84 | +}) |
| 85 | +export class AppModule { |
| 86 | + constructor(trace: Sentry.TraceService) {} |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +or |
| 91 | + |
| 92 | +```javascript |
| 93 | +import { APP_INITIALIZER } from "@angular/core"; |
| 94 | +@NgModule({ |
| 95 | + // ... |
| 96 | + providers: [ |
| 97 | + { |
| 98 | + provide: APP_INITIALIZER, |
| 99 | + useFactory: () => () => {}, |
| 100 | + deps: [Sentry.TraceService], |
| 101 | + multi: true, |
| 102 | + }, |
| 103 | + ], |
| 104 | + // ... |
| 105 | +}) |
| 106 | +export class AppModule {} |
| 107 | +``` |
0 commit comments