|
| 1 | +<p align="center"> |
| 2 | + <a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank"> |
| 3 | + <img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84"> |
| 4 | + </a> |
| 5 | +</p> |
| 6 | + |
| 7 | +# Official Sentry SDK for Angular with Ivy Compatibility |
| 8 | + |
| 9 | +## Links |
| 10 | + |
| 11 | +- [Official SDK Docs](https://docs.sentry.io/platforms/javascript/angular/) |
| 12 | + |
| 13 | +## Angular Version Compatibility |
| 14 | + |
| 15 | +**Note**: This SDK is still experimental and not yet stable. |
| 16 | +We do not yet make guarantees in terms of breaking changes, version compatibilities or semver. |
| 17 | +Please open a Github issue if you experience bugs or would like to share feedback. |
| 18 | + |
| 19 | +This SDK officially supports Angular 12-15 with Angular's new rendering engine, Ivy. |
| 20 | + |
| 21 | +If you're using Angular 10, 11 or a newer Angular version with View Engine instead of Ivy, please use [`@sentry/angular`](https://github.com/getsentry/sentry-javascript/blob/develop/packages/angular/README.md). |
| 22 | + |
| 23 | +If you're using an older version of Angular and experience problems with the Angular SDK, we recommend downgrading the SDK to version 6.x. |
| 24 | +Please note that we don't provide any support for Angular versions below 10. |
| 25 | + |
| 26 | +## General |
| 27 | + |
| 28 | +This package is a wrapper around `@sentry/browser`, with added functionality related to Angular. All methods available |
| 29 | +in `@sentry/browser` can be imported from `@sentry/angular-ivy`. |
| 30 | + |
| 31 | +To use this SDK, call `Sentry.init(options)` before you bootstrap your Angular application. |
| 32 | + |
| 33 | +```javascript |
| 34 | +import { enableProdMode } from '@angular/core'; |
| 35 | +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; |
| 36 | +import { init } from '@sentry/angular-ivy'; |
| 37 | + |
| 38 | +import { AppModule } from './app/app.module'; |
| 39 | + |
| 40 | +init({ |
| 41 | + dsn: '__DSN__', |
| 42 | + // ... |
| 43 | +}); |
| 44 | + |
| 45 | +// ... |
| 46 | + |
| 47 | +enableProdMode(); |
| 48 | +platformBrowserDynamic() |
| 49 | + .bootstrapModule(AppModule) |
| 50 | + .then(success => console.log(`Bootstrap success`)) |
| 51 | + .catch(err => console.error(err)); |
| 52 | +``` |
| 53 | + |
| 54 | +### ErrorHandler |
| 55 | + |
| 56 | +`@sentry/angular-ivy` exports a function to instantiate an ErrorHandler provider that will automatically send Javascript errors |
| 57 | +captured by the Angular's error handler. |
| 58 | + |
| 59 | +```javascript |
| 60 | +import { NgModule, ErrorHandler } from '@angular/core'; |
| 61 | +import { createErrorHandler } from '@sentry/angular-ivy'; |
| 62 | + |
| 63 | +@NgModule({ |
| 64 | + // ... |
| 65 | + providers: [ |
| 66 | + { |
| 67 | + provide: ErrorHandler, |
| 68 | + useValue: createErrorHandler({ |
| 69 | + showDialog: true, |
| 70 | + }), |
| 71 | + }, |
| 72 | + ], |
| 73 | + // ... |
| 74 | +}) |
| 75 | +export class AppModule {} |
| 76 | +``` |
| 77 | + |
| 78 | +Additionally, `createErrorHandler` accepts a set of options that allows you to configure its behavior. For more details |
| 79 | +see `ErrorHandlerOptions` interface in `src/errorhandler.ts`. |
| 80 | + |
| 81 | +### Tracing |
| 82 | + |
| 83 | +`@sentry/angular-ivy` exports a Trace Service, Directive and Decorators that leverage the `@sentry/tracing` Tracing |
| 84 | +integration to add Angular related spans to transactions. If the Tracing integration is not enabled, this functionality |
| 85 | +will not work. The service itself tracks route changes and durations, where directive and decorators are tracking |
| 86 | +components initializations. |
| 87 | + |
| 88 | +#### Install |
| 89 | + |
| 90 | +Registering a Trace Service is a 3-step process. |
| 91 | + |
| 92 | +1. Register and configure the `BrowserTracing` integration from `@sentry/tracing`, including custom Angular routing |
| 93 | + instrumentation: |
| 94 | + |
| 95 | +```javascript |
| 96 | +import { init, instrumentAngularRouting } from '@sentry/angular-ivy'; |
| 97 | +import { Integrations as TracingIntegrations } from '@sentry/tracing'; |
| 98 | + |
| 99 | +init({ |
| 100 | + dsn: '__DSN__', |
| 101 | + integrations: [ |
| 102 | + new TracingIntegrations.BrowserTracing({ |
| 103 | + tracingOrigins: ['localhost', 'https://yourserver.io/api'], |
| 104 | + routingInstrumentation: instrumentAngularRouting, |
| 105 | + }), |
| 106 | + ], |
| 107 | + tracesSampleRate: 1, |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +2. Register `SentryTrace` as a provider in Angular's DI system, with a `Router` as its dependency: |
| 112 | + |
| 113 | +```javascript |
| 114 | +import { NgModule } from '@angular/core'; |
| 115 | +import { Router } from '@angular/router'; |
| 116 | +import { TraceService } from '@sentry/angular-ivy'; |
| 117 | + |
| 118 | +@NgModule({ |
| 119 | + // ... |
| 120 | + providers: [ |
| 121 | + { |
| 122 | + provide: TraceService, |
| 123 | + deps: [Router], |
| 124 | + }, |
| 125 | + ], |
| 126 | + // ... |
| 127 | +}) |
| 128 | +export class AppModule {} |
| 129 | +``` |
| 130 | + |
| 131 | +3. Either require the `TraceService` from inside `AppModule` or use `APP_INITIALIZER` to force-instantiate Tracing. |
| 132 | + |
| 133 | +```javascript |
| 134 | +@NgModule({ |
| 135 | + // ... |
| 136 | +}) |
| 137 | +export class AppModule { |
| 138 | + constructor(trace: TraceService) {} |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +or |
| 143 | + |
| 144 | +```javascript |
| 145 | +import { APP_INITIALIZER } from '@angular/core'; |
| 146 | + |
| 147 | +@NgModule({ |
| 148 | + // ... |
| 149 | + providers: [ |
| 150 | + { |
| 151 | + provide: APP_INITIALIZER, |
| 152 | + useFactory: () => () => {}, |
| 153 | + deps: [TraceService], |
| 154 | + multi: true, |
| 155 | + }, |
| 156 | + ], |
| 157 | + // ... |
| 158 | +}) |
| 159 | +export class AppModule {} |
| 160 | +``` |
| 161 | + |
| 162 | +#### Use |
| 163 | + |
| 164 | +To track Angular components as part of your transactions, you have 3 options. |
| 165 | + |
| 166 | +_TraceDirective:_ used to track a duration between `OnInit` and `AfterViewInit` lifecycle hooks in template: |
| 167 | + |
| 168 | +```javascript |
| 169 | +import { TraceModule } from '@sentry/angular-ivy'; |
| 170 | + |
| 171 | +@NgModule({ |
| 172 | + // ... |
| 173 | + imports: [TraceModule], |
| 174 | + // ... |
| 175 | +}) |
| 176 | +export class AppModule {} |
| 177 | +``` |
| 178 | + |
| 179 | +Then inside your components template (keep in mind that directive name attribute is required): |
| 180 | + |
| 181 | +```html |
| 182 | +<app-header trace="header"></app-header> |
| 183 | +<articles-list trace="articles-list"></articles-list> |
| 184 | +<app-footer trace="footer"></app-footer> |
| 185 | +``` |
| 186 | + |
| 187 | +_TraceClassDecorator:_ used to track a duration between `OnInit` and `AfterViewInit` lifecycle hooks in components: |
| 188 | + |
| 189 | +```javascript |
| 190 | +import { Component } from '@angular/core'; |
| 191 | +import { TraceClassDecorator } from '@sentry/angular-ivy'; |
| 192 | + |
| 193 | +@Component({ |
| 194 | + selector: 'layout-header', |
| 195 | + templateUrl: './header.component.html', |
| 196 | +}) |
| 197 | +@TraceClassDecorator() |
| 198 | +export class HeaderComponent { |
| 199 | + // ... |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +_TraceMethodDecorator:_ used to track a specific lifecycle hooks as point-in-time spans in components: |
| 204 | + |
| 205 | +```javascript |
| 206 | +import { Component, OnInit } from '@angular/core'; |
| 207 | +import { TraceMethodDecorator } from '@sentry/angular-ivy'; |
| 208 | + |
| 209 | +@Component({ |
| 210 | + selector: 'app-footer', |
| 211 | + templateUrl: './footer.component.html', |
| 212 | +}) |
| 213 | +export class FooterComponent implements OnInit { |
| 214 | + @TraceMethodDecorator() |
| 215 | + ngOnInit() {} |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +You can also add your own custom spans by attaching them to the current active transaction using `getActiveTransaction` |
| 220 | +helper. For example, if you'd like to track the duration of Angular boostraping process, you can do it as follows: |
| 221 | + |
| 222 | +```javascript |
| 223 | +import { enableProdMode } from '@angular/core'; |
| 224 | +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; |
| 225 | +import { init, getActiveTransaction } from '@sentry/angular-ivy'; |
| 226 | + |
| 227 | +import { AppModule } from './app/app.module'; |
| 228 | + |
| 229 | +// ... |
| 230 | + |
| 231 | +const activeTransaction = getActiveTransaction(); |
| 232 | +const boostrapSpan = |
| 233 | + activeTransaction && |
| 234 | + activeTransaction.startChild({ |
| 235 | + description: 'platform-browser-dynamic', |
| 236 | + op: 'ui.angular.bootstrap', |
| 237 | + }); |
| 238 | + |
| 239 | +platformBrowserDynamic() |
| 240 | + .bootstrapModule(AppModule) |
| 241 | + .then(() => console.log(`Bootstrap success`)) |
| 242 | + .catch(err => console.error(err)); |
| 243 | + .finally(() => { |
| 244 | + if (bootstrapSpan) { |
| 245 | + boostrapSpan.finish(); |
| 246 | + } |
| 247 | + }) |
| 248 | +``` |
0 commit comments