|
1 | 1 | Once this is done, Sentry's Angular SDK captures all unhandled exceptions and transactions. |
2 | 2 |
|
| 3 | +```typescript {tabTitle: App Config} {filename: main.ts} {5-31} {"onboardingOptions": {"performance": "10-13, 18-25", "session-replay": "14-16, 26-30"}} |
| 4 | +import { bootstrapApplication } from "@angular/platform-browser"; |
| 5 | +import { appConfig } from "./app/app.config"; |
| 6 | +import { AppComponent } from "./app/app.component"; |
3 | 7 |
|
4 | | -```typescript {filename: main.ts} {3, 6-30} {"onboardingOptions": {"performance": "9-12, 17-24", "session-replay": "13-15, 25-29"}} |
5 | | -import { enableProdMode } from "@angular/core"; |
6 | | -import { platformBrowserDynamic } from "@angular/platform-browser-dynamic"; |
7 | 8 | import * as Sentry from "@sentry/angular"; |
| 9 | + |
| 10 | +Sentry.init({ |
| 11 | + dsn: "___PUBLIC_DSN___", |
| 12 | + integrations: [ |
| 13 | + // Registers and configures the Tracing integration, |
| 14 | + // which automatically instruments your application to monitor its |
| 15 | + // performance, including custom Angular routing instrumentation |
| 16 | + Sentry.browserTracingIntegration(), |
| 17 | + // Registers the Replay integration, |
| 18 | + // which automatically captures Session Replays |
| 19 | + Sentry.replayIntegration(), |
| 20 | + ], |
| 21 | + |
| 22 | + // Set tracesSampleRate to 1.0 to capture 100% |
| 23 | + // of transactions for tracing. |
| 24 | + // We recommend adjusting this value in production |
| 25 | + tracesSampleRate: 1.0, |
| 26 | + |
| 27 | + // Set `tracePropagationTargets` to control for which URLs trace propagation should be enabled |
| 28 | + tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/], |
| 29 | + |
| 30 | + // Capture Replay for 10% of all sessions, |
| 31 | + // plus for 100% of sessions with an error |
| 32 | + replaysSessionSampleRate: 0.1, |
| 33 | + replaysOnErrorSampleRate: 1.0, |
| 34 | +}); |
| 35 | + |
| 36 | +bootstrapApplication(AppComponent, appConfig).catch((err) => |
| 37 | + console.error(err) |
| 38 | +); |
| 39 | +``` |
| 40 | + |
| 41 | +```typescript {tabTitle: NGModule Config} {filename: main.ts} {4-30} {"onboardingOptions": {"performance": "9-12, 17-24", "session-replay": "13-15, 25-29"}} |
| 42 | +import { platformBrowserDynamic } from "@angular/platform-browser-dynamic"; |
8 | 43 | import { AppModule } from "./app/app.module"; |
9 | 44 |
|
| 45 | +import * as Sentry from "@sentry/angular"; |
| 46 | + |
10 | 47 | Sentry.init({ |
11 | 48 | dsn: "___PUBLIC_DSN___", |
12 | 49 | integrations: [ |
@@ -40,9 +77,40 @@ platformBrowserDynamic() |
40 | 77 |
|
41 | 78 | ### Register Sentry Providers |
42 | 79 |
|
43 | | -The Sentry Angular SDK exports a couple of Angular providers that are necessary to fully instrument your application. We recommend registering them in your main `AppModule`: |
| 80 | +The Sentry Angular SDK exports a couple of Angular providers that are necessary to fully instrument your application. We recommend registering them in your `app.config.ts` or main `app.module.ts`: |
| 81 | + |
| 82 | +```typescript {tabTitle: App Config} {filename: app.config.ts} {8, 13-26} {"onboardingOptions": {"performance": "17-26"}} |
| 83 | +import { |
| 84 | + APP_INITIALIZER, |
| 85 | + ApplicationConfig, |
| 86 | + ErrorHandler, |
| 87 | +} from "@angular/core"; |
| 88 | +import { Router } from "@angular/router"; |
| 89 | + |
| 90 | +import * as Sentry from "@sentry/angular"; |
| 91 | + |
| 92 | +export const appConfig: ApplicationConfig = { |
| 93 | + providers: [ |
| 94 | + // ... |
| 95 | + { |
| 96 | + provide: ErrorHandler, |
| 97 | + useValue: Sentry.createErrorHandler(), |
| 98 | + }, |
| 99 | + { |
| 100 | + provide: Sentry.TraceService, |
| 101 | + deps: [Router], |
| 102 | + }, |
| 103 | + { |
| 104 | + provide: APP_INITIALIZER, |
| 105 | + useFactory: () => () => {}, |
| 106 | + deps: [Sentry.TraceService], |
| 107 | + multi: true, |
| 108 | + }, |
| 109 | + ], |
| 110 | +}; |
| 111 | +``` |
44 | 112 |
|
45 | | -```typescript {filename: app.module.ts} {4, 9-22} {"onboardingOptions": {"performance": "13-22"}} |
| 113 | +```typescript {tabTitle: NGModule Config} {filename: app.module.ts} {4, 9-22} {"onboardingOptions": {"performance": "13-22"}} |
46 | 114 | import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core"; |
47 | 115 | import { Router } from "@angular/router"; |
48 | 116 |
|
@@ -73,15 +141,23 @@ export class AppModule {} |
73 | 141 |
|
74 | 142 | The `Sentry.createErrorHandler` function initializes a Sentry-specific `ErrorHandler` that automatically sends errors caught by Angular to Sentry. You can also customize the behavior by setting a couple of handler [options](https://github.com/getsentry/sentry-javascript/blob/master/packages/angular/src/errorhandler.ts). |
75 | 143 |
|
| 144 | +<Alert> |
| 145 | + |
| 146 | +If your angular app is configured for SSR, make sure that the Sentry providers are not accidentally passed to the SSR config (`app.config.server.ts`). The Sentry Angular SDK can only be used in the browser. |
| 147 | + |
| 148 | +</Alert> |
| 149 | + |
76 | 150 | <OnboardingOption optionId="performance"> |
77 | | -The `Sentry.TraceService` listens to the Angular router for tracing. To inject `TraceService`, register the `APP_INITIALIZER` provider as shown above. Alternatively, you can also require the `TraceService` from inside your `AppModule` constructor: |
78 | 151 |
|
79 | | -```javascript {filename: app.module.ts} {5} |
| 152 | +If you're using an NGModule-based application (`app.module.ts`), you can also dependency-inject the `TraceService` from inside your `AppModule` constructor, instead of providing `APP_INITIALIZER`: |
| 153 | + |
| 154 | +```javascript {tabTitle: NGModule Config} {filename: app.module.ts} {5} |
80 | 155 | @NgModule({ |
81 | 156 | // ... |
82 | 157 | }) |
83 | 158 | export class AppModule { |
84 | 159 | constructor(trace: Sentry.TraceService) {} |
85 | 160 | } |
86 | 161 | ``` |
| 162 | + |
87 | 163 | </OnboardingOption> |
0 commit comments