Skip to content

Commit 8b6aa07

Browse files
HazATrhcarvalho
andauthored
fix: Update JS Docs (#1556)
* fix: Sampling rate * ref: Descript activity and add another JS example * fix: Typo * Update src/collections/_documentation/performance/distributed-tracing.md Co-Authored-By: Rodolfo Carvalho <[email protected]> * Update src/collections/_documentation/performance/distributed-tracing.md Co-Authored-By: Rodolfo Carvalho <[email protected]> * feat: Explain tracingOrigins and CORS * Apply suggestions from code review Co-Authored-By: Rodolfo Carvalho <[email protected]> * Update src/collections/_documentation/performance/distributed-tracing.md Co-Authored-By: Rodolfo Carvalho <[email protected]> * fix: CodeReview Co-authored-by: Rodolfo Carvalho <[email protected]>
1 parent 90d7d3c commit 8b6aa07

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

src/collections/_documentation/performance/distributed-tracing.md

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,16 @@ def process_item(item):
154154

155155
### JavaScript
156156

157-
To access our tracing features, you will need to install our Tracing integration:
157+
To access our tracing features, you will need to install our Tracing package `@sentry/apm`:
158158

159159
```bash
160160
$ npm install @sentry/browser
161161
$ npm install @sentry/apm
162162
```
163163

164-
**Sending Traces**
164+
**Sending Traces/Transactions/Spans**
165165

166-
Tracing resides in the `@sentry/apm` package. You can add it to your `Sentry.init` call:
166+
The `Tracing` integration resides in the `@sentry/apm` package. You can add it to your `Sentry.init` call:
167167

168168
```javascript
169169
import * as Sentry from '@sentry/browser';
@@ -174,6 +174,7 @@ Sentry.init({
174174
integrations: [
175175
new ApmIntegrations.Tracing(),
176176
],
177+
tracesSampleRate: 0.1,
177178
});
178179
```
179180

@@ -186,10 +187,9 @@ import { Integrations as ApmIntegrations } from '@sentry/apm';
186187
Sentry.init({
187188
dsn: '___PUBLIC_DSN___',
188189
integrations: [
189-
new Integrations.Tracing({
190-
tracesSampleRate: 0.1,
191-
}),
190+
new ApmIntegrations.Tracing(),
192191
],
192+
tracesSampleRate: 0.1,
193193
});
194194
```
195195

@@ -198,6 +198,22 @@ You can pass many different options to tracing, but it comes with reasonable def
198198

199199
- A [transaction]({%- link _documentation/performance/performance-glossary.md -%}#transaction) for every page load
200200
- All XHR/fetch requests as spans
201+
- If available: Browser Resources (Scripts, CSS, Images ...)
202+
- If available: Browser Performance API Marks
203+
204+
*tracingOrigins Option*
205+
206+
The default value of `tracingOrigins` is : `new ApmIntegrations.Tracing({tracingOrigins: ['localhost', /^\//]})`. The JavaScript SDK will attach the `sentry-trace` header to all outgoing XHR/fetch requests that contain a string in the list or match a regex. In case your frontend is making requests to a different domain you might want to add it there in order to propagate the `sentry-trace` header to the backend services, required to link transactions together as part of a single trace.
207+
208+
*Example:*
209+
210+
- A frontend application is served from `example.com`
211+
- A backend service is served from `api.example.com`
212+
- The frontend application makes API calls to the backend
213+
- Then the option need to be configured like this `new ApmIntegrations.Tracing({tracingOrigins: ['api.example.com']})`
214+
- Outgoing XHR/fetch requests to `api.example.com` would get the `sentry-trace` header attached
215+
216+
*NOTE:* You need to make sure your web server CORS is configured to allow the `sentry-trace` header. The configuration might look like this: `"Access-Control-Allow-Headers: sentry-trace"`, this depends a lot on your configuration. But if you are not whitelisting the `sentry-trace` header the request might be blocked.
201217

202218
**Using Tracing Integration for Manual Instrumentation**
203219

@@ -210,24 +226,58 @@ The tracing integration will create a [transaction]({%- link _documentation/perf
210226
const activity = ApmIntegrations.Tracing.pushActivity(displayName, {
211227
data: {},
212228
op: 'react',
213-
description: `<${displayName}>`,
229+
description: `${displayName}`,
214230
});
215231

216232
// Sometime later ...
217233
// When we pop the activity, the Integration will finish the span and after the timeout finish the transaction and send it to Sentry
218-
Integrations.ApmIntegrations.popActivity(activity);
234+
// Keep in mind, as long as you do not pop the activity, the transaction will be kept alive and not sent to Sentry
235+
ApmIntegrations.Tracing.popActivity(activity);
236+
```
237+
238+
Keep in mind, if there is no active transaction, you need to create one before pushing an activity otherwise nothing will happen.
239+
So given a different example where you want to create an Transaction for a user interaction on you page you need to do the following:
240+
241+
242+
```javascript
243+
// Let's say this function is invoked when a user clicks on the checkout button of your shop
244+
shopCheckout() {
245+
// This will create a new Transaction for you
246+
ApmIntegrations.Tracing.startIdleTransaction('shopCheckout');
247+
248+
const result = validateShoppingCartOnServer(); // Consider this function making an xhr/fetch call
249+
const activity = ApmIntegrations.Tracing.pushActivity('task', {
250+
data: {
251+
result
252+
},
253+
op: 'task',
254+
description: `processing shopping cart result`,
255+
});
256+
processAndValidateShoppingCart(result);
257+
ApmIntegrations.Tracing.popActivity(activity);
258+
259+
ApmIntegrations.Tracing.finishIdleTransaction(); // This is optional
260+
}
219261
```
220262

263+
This example will send a [transaction]({%- link _documentation/performance/performance-glossary.md -%}#transaction) `shopCheckout` to Sentry, containing all outgoing requests that might have happened in `validateShoppingCartOnServer`. It also contains a `task` span that measured how long the processing took. Finally the call to `ApmIntegrations.Tracing.finshIdleTransaction()` will finish the [transaction]({%- link _documentation/performance/performance-glossary.md -%}#transaction) and send it to Sentry. Calling this is optional since the Integration would send the [transaction]({%- link _documentation/performance/performance-glossary.md -%}#transaction) after the defined `idleTimeout` (default 500ms) itself.
264+
265+
**What is an activity?**
266+
267+
The concept of an activity only exists in the `Tracing` Integration in JavaScript Browser. It's a helper that tells the Integration for how long it should keep the `IdleTransaction` alive. An activity can be pushed and popped. Once all activities of an `IdleTransaction` have been popped, the `IdleTransaction` will be sent to Sentry.
268+
221269
**Manual Transactions**
222270

223271
To manually instrument certain regions of your code, you can create a [transaction]({%- link _documentation/performance/performance-glossary.md -%}#transaction) to capture them.
272+
This is both valid for JavaScript Browser and Node and works besides the `Tracing` integration.
224273

225274
The following example creates a transaction for a scope that contains an expensive operation (for example, `process_item`), and sends the result to Sentry:
226275

227276
```javascript
228277
const transaction = Sentry.getCurrentHub().startSpan({
278+
description: 'My optional description',
229279
op: "task",
230-
transaction: item.getTransaction()
280+
transaction: item.getTransaction() // A name describing the operation
231281
})
232282

233283
// processItem may create more spans internally (see next example)
@@ -280,7 +330,7 @@ const Apm = require("@sentry/apm"); // This is required since it patches functio
280330

281331
Sentry.init({
282332
dsn: "___PUBLIC_DSN___",
283-
tracesSampleRate: 0.1
333+
tracesSampleRate: 1
284334
});
285335
```
286336

0 commit comments

Comments
 (0)