Skip to content

Commit 556e41c

Browse files
Lms24ranma42lizokm
authored
ref(angular): Update Angular SDK docs for @sentry/angular-ivy (#6356)
To ensure first-class compatibility with Angular's "new" rendering engine, Ivy, we needed to create a new SDK package that's compiled differently than the original `@sentry/angular` package. This new SDK is compatible with Angular 12 and newer and hence we're going to make it the default SDK in the docs going forward. This PR * Updates the Angular version <-> Sentry SDK compatibility description with a table * I added entries for Angular 1/AngularJS (this came up recently in a Slack convo) and Angular 2-9 with a message that we do not support these versions anymore in v7 of the SDKs. Happy to make changes here if reviewers think this shouldn't be promoted. * Updates imports and code snippets with versions for Angular 12+ (ivy) and Angular 10/11 * Updates the SDK onboarding wizard docs (+ Replay onboarding) * Adds an Angular-specific verify code snippet which was missing before * Adds file names to all angular code snippets (this came up in #6326 and it was easiest to just do it in this PR) * streamlines the Angular "Getting started" page and fixes a few typos/wording issues Co-authored-by: Andrea Canciani <[email protected]> Co-authored-by: Liza Mock <[email protected]>
1 parent 7558bbe commit 556e41c

File tree

11 files changed

+246
-39
lines changed

11 files changed

+246
-39
lines changed

src/platform-includes/capture-error/javascript.angular.mdx

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
You can pass an `Error` object to `captureException()` to get it captured as event. It's also possible to pass non-`Error` objects and strings, but be aware that the resulting events in Sentry may be missing a stacktrace.
22

3-
```javascript
3+
```javascript{tabTitle: Angular 12+}
4+
import * as Sentry from "@sentry/angular-ivy";
5+
6+
try {
7+
aFunctionThatMightFail();
8+
} catch (err) {
9+
Sentry.captureException(err);
10+
}
11+
```
12+
13+
```javascript{tabTitle: Angular 10/11}
414
import * as Sentry from "@sentry/angular";
515
616
try {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
```javascript
1+
```javascript {tabTitle: Angular 12+}
2+
import * as Sentry from "@sentry/angular-ivy";
3+
```
4+
5+
```javascript {tabTitle: Angular 10/11}
26
import * as Sentry from "@sentry/angular";
37
```

src/platform-includes/getting-started-config/javascript.angular.mdx

+77-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
Once this is done, Sentry's Angular SDK captures all unhandled exceptions and transactions.
22

3-
```javascript
3+
```javascript{tabTitle: Angular 12+}{filename: main.ts}
44
import { enableProdMode } from "@angular/core";
55
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
6-
import * as Sentry from "@sentry/angular";
6+
import * as Sentry from "@sentry/angular-ivy";
77
import { BrowserTracing } from "@sentry/tracing";
88
import { AppModule } from "./app/app.module";
99
1010
Sentry.init({
11-
dsn: "___PUBLIC_DSN___" ,
11+
dsn: "___PUBLIC_DSN___",
1212
integrations: [
1313
// Registers and configures the Tracing integration,
1414
// which automatically instruments your application to monitor its
@@ -25,22 +25,67 @@ Sentry.init({
2525
tracesSampleRate: 1.0,
2626
});
2727
28-
2928
platformBrowserDynamic()
3029
.bootstrapModule(AppModule)
3130
.then(success => console.log(`Bootstrap success`))
3231
.catch(err => console.error(err));
3332
```
3433

35-
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.
34+
```javascript{tabTitle: Angular 10/11}{filename: main.ts}
35+
import { enableProdMode } from "@angular/core";
36+
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
37+
import * as Sentry from "@sentry/angular";
38+
import { BrowserTracing } from "@sentry/tracing";
39+
import { AppModule } from "./app/app.module";
40+
41+
Sentry.init({
42+
dsn: "___PUBLIC_DSN___",
43+
integrations: [
44+
// Registers and configures the Tracing integration,
45+
// which automatically instruments your application to monitor its
46+
// performance, including custom Angular routing instrumentation
47+
new BrowserTracing({
48+
tracePropagationTargets: ["localhost", "https://yourserver.io/api"],
49+
routingInstrumentation: Sentry.routingInstrumentation,
50+
}),
51+
],
52+
53+
// Set tracesSampleRate to 1.0 to capture 100%
54+
// of transactions for performance monitoring.
55+
// We recommend adjusting this value in production
56+
tracesSampleRate: 1.0,
57+
});
3658
37-
`@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.
59+
platformBrowserDynamic()
60+
.bootstrapModule(AppModule)
61+
.then(success => console.log(`Bootstrap success`))
62+
.catch(err => console.error(err));
63+
```
3864

3965
### Automatically Send Errors with `ErrorHandler`
4066

41-
`@sentry/angular` exports a function to instantiate an `ErrorHandler` provider that will automatically send JavaScript errors captured by the Angular's error handler.
67+
The Angular SDK exports a function to instantiate an `ErrorHandler` provider that will automatically send JavaScript errors captured by Angular's error handler.
68+
69+
```javascript{tabTitle: Angular 12+}{filename: app.module.ts}
70+
import { NgModule, ErrorHandler } from "@angular/core";
71+
import * as Sentry from "@sentry/angular-ivy";
72+
73+
@NgModule({
74+
// ...
75+
providers: [
76+
{
77+
provide: ErrorHandler,
78+
useValue: Sentry.createErrorHandler({
79+
showDialog: true,
80+
}),
81+
},
82+
],
83+
// ...
84+
})
85+
export class AppModule {}
86+
```
4287

43-
```javascript
88+
```javascript{tabTitle: Angular 10/11}{filename: app.module.ts}
4489
import { NgModule, ErrorHandler } from "@angular/core";
4590
import * as Sentry from "@sentry/angular";
4691
@@ -63,9 +108,27 @@ You can configure the behavior of `createErrorHandler`. For more details see the
63108

64109
### Register `TraceService`
65110

66-
In Angular's DI system, register `TraceService` as a provider with a `Router` as its dependency:
111+
For performance monitoring, register `TraceService` as a provider with a `Router` as its dependency:
112+
113+
```javascript{tabTitle: Angular 12+}{filename: app.module.ts}
114+
import { NgModule } from "@angular/core";
115+
import { Router } from "@angular/router";
116+
import * as Sentry from "@sentry/angular-ivy";
117+
118+
@NgModule({
119+
// ...
120+
providers: [
121+
{
122+
provide: Sentry.TraceService,
123+
deps: [Router],
124+
},
125+
],
126+
// ...
127+
})
128+
export class AppModule {}
129+
```
67130

68-
```javascript
131+
```javascript{tabTitle: Angular 10/11}{filename: app.module.ts}
69132
import { NgModule } from "@angular/core";
70133
import { Router } from "@angular/router";
71134
import * as Sentry from "@sentry/angular";
@@ -82,8 +145,10 @@ import * as Sentry from "@sentry/angular";
82145
})
83146
export class AppModule {}
84147
```
148+
85149
Then, either require the `TraceService` from inside `AppModule` or use `APP_INITIALIZER` to force instantiate Tracing.
86-
```javascript
150+
151+
```javascript {filename: app.module.ts}
87152
@NgModule({
88153
// ...
89154
})
@@ -94,7 +159,7 @@ export class AppModule {
94159

95160
or
96161

97-
```javascript
162+
```javascript {filename: app.module.ts}
98163
import { APP_INITIALIZER } from "@angular/core";
99164
@NgModule({
100165
// ...
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
```bash {tabTitle:npm}
2+
# Angular 12 and newer:
3+
npm install --save @sentry/angular-ivy @sentry/tracing
4+
5+
# Angular 10 and 11:
26
npm install --save @sentry/angular @sentry/tracing
37
```
48

59
```bash {tabTitle:Yarn}
10+
# Angular 12 and newer:
11+
yarn add @sentry/angular-ivy @sentry/tracing
12+
13+
# Angular 10 and 11:
614
yarn add @sentry/angular @sentry/tracing
715
```
816

9-
<Alert level="info" title="Angular Version Compatibility" >
17+
### Angular Version Compatibility
18+
19+
Because of the way Angular libraries are compiled, you need to use a specific version of the Sentry SDK for each corresponding version of Angular as shown below:
1020

11-
The latest version of the Sentry Angular SDK officially supports Angular 10 and newer.
12-
If you need to use Angular 9 or older and you experience problems with the latest version of the Sentry SDK,
13-
try downgrading the SDK to version 6 (`@sentry/angular@^6.x`). If you are using Sentry Tracing,
14-
be sure to also downgrade it to the same version (`@sentry/tracing@^6.x`).
15-
Version 6 of the Sentry SDK was compiled differently and might work with older versions of Angular.
16-
Please note that this combination of packages is not being maintained or tested.
21+
| Angular version | Recommended Sentry SDK |
22+
| --------------- | ---------------------- |
23+
| 12 and newer | `@sentry/angular-ivy` |
24+
| 10, 11 | `@sentry/angular` |
25+
| Older versions | See note below |
1726

27+
<Alert level="warning">
28+
Support for any Angular version below 10 was discontinued in version 7 of the SDK.
29+
If you need to use Angular 9 or older, try version 6 (<code>@sentry/angular@^6.x</code>) of the SDK.
30+
For AngularJS/1.x, use <code>@sentry/browser@^6.x</code> and our <a href="./angular1">AngularJS integration</a>.
31+
Note, that these versions of the SDK are no longer maintained or tested.
1832
</Alert>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Trigger a test error somewhere in your Angular app, for example in your main app component:
2+
3+
```html {filename: app.component.html}
4+
<button (click)="throwTestError()">Test Sentry Error</button>
5+
```
6+
7+
Then, in your `app.component.ts` add:
8+
9+
```javascript {filename: app.component.ts}
10+
public throwTestError(): void {
11+
throw new Error("Sentry Test Error");
12+
}
13+
```
14+
15+
<Note>
16+
17+
Errors triggered from within Browser DevTools are sandboxed and won't trigger an error handler. Place the snippet directly in your code instead.
18+
19+
</Note>

src/platform-includes/performance/configure-sample-rate/javascript.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
```javascript {tabTitle: ESM}
2-
// If you're using one of our integration packages, like `@sentry/angular`,
2+
// If you're using one of our framework SDK packages, like `@sentry/angular`,
33
// substitute its name for `@sentry/browser` here
44
import * as Sentry from "@sentry/browser";
55

src/platform-includes/performance/enable-automatic-instrumentation/javascript.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ To enable tracing, include the `BrowserTracing` integration in your SDK configur
33
After configuration, you will see both `pageload` and `navigation` transactions in the Sentry UI.
44

55
```javascript {tabTitle: ESM}
6-
// If you're using one of our integration packages, like `@sentry/angular`,
6+
// If you're using one of our framework SDK packages, like `@sentry/angular`,
77
// substitute its name for `@sentry/browser` here
88
import * as Sentry from "@sentry/browser";
99
import { BrowserTracing } from "@sentry/tracing"; // Must import second

src/platforms/javascript/guides/angular/features/component-tracking.mdx

+91-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ To track your components as part of your transactions, use any (or a combination
2020
Import `TraceModule` either globally in your application's `app.module.ts` file or in the module(s) in which
2121
you want to track your components:
2222

23-
```typescript {filename:app.module.ts}
23+
```typescript {filename:app.module.ts} {tabTitle:Angular 12+}
24+
import * as Sentry from "@sentry/angular-ivy";
25+
26+
@NgModule({
27+
// ...
28+
imports: [Sentry.TraceModule],
29+
// ...
30+
})
31+
export class AppModule {}
32+
```
33+
34+
```typescript {filename:app.module.ts} {tabTitle:Angular 10/11}
2435
import * as Sentry from "@sentry/angular";
2536

2637
@NgModule({
@@ -56,7 +67,21 @@ below to track your Angular components.
5667

5768
Just add `TraceClassDecorator` to the components you want to track:
5869

59-
```typescript {filename:header.component.ts}
70+
```javascript {filename:header.component.ts} {tabTitle: Angular 12+}
71+
import { Component } from "@angular/core";
72+
import * as Sentry from "@sentry/angular-ivy";
73+
74+
@Component({
75+
selector: "app-header",
76+
templateUrl: "./header.component.html",
77+
})
78+
@Sentry.TraceClassDecorator()
79+
export class HeaderComponent {
80+
// ...
81+
}
82+
```
83+
84+
```javascript {filename:header.component.ts} {tabTitle: Angular 10/11}
6085
import { Component } from "@angular/core";
6186
import * as Sentry from "@sentry/angular";
6287

@@ -74,7 +99,21 @@ export class HeaderComponent {
7499

75100
`TraceMethodDecorator` tracks specific component lifecycle hooks as point-in-time spans. The added spans are called **`ui.angular.[methodname]`** (like, `ui.angular.ngOnChanges`). For example, you can use this decorator to track how often component changes are detected during an ongoing transaction:
76101

77-
```typescript {filename:login.component.ts}
102+
```javascript {filename:login.component.ts} {tabTitle: Angular 12+}
103+
import { Component, OnInit } from "@angular/core";
104+
import * as Sentry from "@sentry/angular-ivy";
105+
106+
@Component({
107+
selector: "app-login",
108+
templateUrl: "./login.component.html",
109+
})
110+
export class LoginComponent implements OnChanges {
111+
@Sentry.TraceMethodDecorator()
112+
ngOnChanges(changes: SimpleChanges) {}
113+
}
114+
```
115+
116+
```javascript {filename:login.component.ts} {tabTitle: Angular 10/11}
78117
import { Component, OnInit } from "@angular/core";
79118
import * as Sentry from "@sentry/angular";
80119

@@ -96,7 +135,25 @@ You can combine our tracking utilities and track the bootstrapping duration of y
96135

97136
To get the best insights into your components' performance, you can combine `TraceDirective`, `TraceClassDecorator`, and `TraceMethodDecorator`. This allows you to track component initialization durations as well as arbitrary lifecycle events, such as change and destroy events:
98137

99-
```typescript {filename:user-card.component.ts}
138+
```javascript {filename:user-card.component.ts} {tabTitle: Angular 12+}
139+
import { Component, OnInit } from "@angular/core";
140+
import * as Sentry from "@sentry/angular-ivy";
141+
142+
@Component({
143+
selector: "app-user-card",
144+
templateUrl: "./user-card.component.html",
145+
})
146+
@Sentry.TraceClassDecorator()
147+
export class UserCardComponent implements OnChanges, OnDestroy {
148+
@Sentry.TraceMethodDecorator()
149+
ngOnChanges(changes: SimpleChanges) {}
150+
151+
@Sentry.TraceMethodDecorator()
152+
ngOnDestroy() {}
153+
}
154+
```
155+
156+
```javascript {filename:user-card.component.ts} {tabTitle: Angular 10/11}
100157
import { Component, OnInit } from "@angular/core";
101158
import * as Sentry from "@sentry/angular";
102159

@@ -116,7 +173,7 @@ export class UserCardComponent implements OnChanges, OnDestroy {
116173

117174
User `TraceDirective` if you only want to track components in certain instances or locations:
118175

119-
```html {user-card.component.html}
176+
```html {filename: user-card.component.html}
120177
<div>
121178
<app-icon trace="user-icon">user</app-icon>
122179
<label>{{ user.name }}</label>
@@ -130,7 +187,35 @@ User `TraceDirective` if you only want to track components in certain instances
130187
You can add your own custom spans by attaching them to the currently active transaction using the `getActiveTransaction`
131188
helper. For example, you can track the duration of the Angular bootstrapping process to find out how long your app takes to bootstrap on your users' devices:
132189

133-
```javascript
190+
```javascript {tabTitle: Angular 12+} {filename:main.ts}
191+
import { enableProdMode } from "@angular/core";
192+
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
193+
import * as Sentry from "@sentry/angular-ivy";
194+
195+
import { AppModule } from "./app/app.module";
196+
197+
// ...
198+
199+
const activeTransaction = Sentry.getActiveTransaction();
200+
const bootstrapSpan =
201+
activeTransaction &&
202+
activeTransaction.startChild({
203+
description: "platform-browser-dynamic",
204+
op: "ui.angular.bootstrap",
205+
});
206+
207+
platformBrowserDynamic()
208+
.bootstrapModule(AppModule)
209+
.then(() => console.log(`Bootstrap success`))
210+
.catch(err => console.error(err))
211+
.finally(() => {
212+
if (bootstrapSpan) {
213+
bootstrapSpan.finish();
214+
}
215+
});
216+
```
217+
218+
```javascript {tabTitle: Angular 10/11} {filename:main.ts}
134219
import { enableProdMode } from "@angular/core";
135220
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
136221
import * as Sentry from "@sentry/angular";

0 commit comments

Comments
 (0)