Skip to content

Commit 40d437e

Browse files
Lms24lizokm
andauthored
ref(angular): Add code snippets for app.config.ts-based Angular apps (#11356)
--------- Co-authored-by: Liza Mock <[email protected]>
1 parent 1f5508c commit 40d437e

File tree

2 files changed

+103
-15
lines changed

2 files changed

+103
-15
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Track Angular Components
33
description: "Learn how Sentry's Angular SDK allows you to monitor the rendering performance of your application and its components."
44
---
55

6-
Sentry's Angular SDK offers a feature to monitor the performance of your Angular components: Component Tracking. Enabling this feature provides you with spans in your transactions that show the initialization and update cycles of your Angular components. This allows you to drill down into how your components are behaving so you can identify slow initializations or frequent updates, which might have an impact on your app's performance.
6+
Sentry's Angular SDK offers a component-tracking feature to monitor the performance of your Angular components. Enabling this feature starts spans that show the initialization and update cycles of your Angular components. This allows you to drill down into how your components are behaving so you can identify slow initializations or frequent updates, which may be impacting your app's performance.
77

88
<Alert>
99

@@ -13,25 +13,37 @@ To set up component tracking, you need to configure tracing. For details on how
1313

1414
To track your components as part of your transactions, use any (or a combination) of the following options.
1515

16-
## Using the `trace` directive
16+
## Using the `trace` Directive
1717

18-
Our `TraceDirective` tracks a duration between the `OnInit` and `AfterViewInit` lifecycle hooks in your component template. It adds spans called **`ui.angular.init`** to the currently active transaction that allows you to track specific individual instances of your components. If you want to track all instances instead, use [`TraceClass`](#using-traceclass).
18+
Our `TraceDirective` tracks the duration between the `OnInit` and `AfterViewInit` lifecycle hooks in your component template. It adds spans called `ui.angular.init` to the currently active transaction that allows you to track specific individual instances of your components. To track all instances, use [`TraceClass`](#using-traceclass).
1919

20-
Import `TraceModule` either globally in your application's `app.module.ts` file or in the module(s) in which
21-
you want to track your components:
20+
For both standalone components and NGModule-based app configs, register the `TraceModule` in the `imports` array of the standalone component or module that you want to use it in:
2221

23-
```typescript {filename:app.module.ts} {1,5}
22+
```typescript {tabTitle:Standalone Component} {filename:app.component.ts} {3,8}
23+
import { Component } from "@angular/core";
24+
import { RouterOutlet } from "@angular/router";
2425
import * as Sentry from "@sentry/angular";
2526

26-
@NgModule({
27+
@Component({
28+
selector: "app-root",
29+
standalone: true,
30+
imports: [Sentry.TraceModule],
2731
// ...
32+
})
33+
export class AppComponent {}
34+
```
35+
36+
```typescript {tabTitle:NGModule Config} {filename:app.module.ts} {1,4}
37+
import * as Sentry from "@sentry/angular";
38+
39+
@NgModule({
2840
imports: [Sentry.TraceModule],
2941
// ...
3042
})
3143
export class AppModule {}
3244
```
3345

34-
Then, in your component's template, add the directive to all components you want to track. Remember to give the `trace` attribute a name, which will be shown in the span's description:
46+
Then, in your component's template, add the `trace` directive to all components you want to track. Remember to give the `trace` attribute a name, which will be shown in the span's description:
3547

3648
```html {filename:app.component.(ts|html)}
3749
<app-header trace="header"></app-header>

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

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,49 @@
11
Once this is done, Sentry's Angular SDK captures all unhandled exceptions and transactions.
22

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";
37

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";
78
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";
843
import { AppModule } from "./app/app.module";
944

45+
import * as Sentry from "@sentry/angular";
46+
1047
Sentry.init({
1148
dsn: "___PUBLIC_DSN___",
1249
integrations: [
@@ -40,9 +77,40 @@ platformBrowserDynamic()
4077

4178
### Register Sentry Providers
4279

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+
```
44112

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"}}
46114
import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core";
47115
import { Router } from "@angular/router";
48116

@@ -73,15 +141,23 @@ export class AppModule {}
73141

74142
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).
75143

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+
76150
<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:
78151

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}
80155
@NgModule({
81156
// ...
82157
})
83158
export class AppModule {
84159
constructor(trace: Sentry.TraceService) {}
85160
}
86161
```
162+
87163
</OnboardingOption>

0 commit comments

Comments
 (0)