Skip to content

Commit a551313

Browse files
authored
feat: Pick up sentry-trace in JS <meta/> tag (#2703)
* feat: Pick up sentry-trace in frontend * meta: Add changelog * ref: Code review
1 parent 99745d6 commit a551313

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- [tracing] fix: Add manual Location typing (#2700)
77
- [react] feat: Expose eventId on ErrorBoundary component
88

9+
- [tracing] feat: Pick up sentry-trace in JS <meta/> tag (#2703)
10+
- [tracing] fix: Respect sample decision when continuing trace from header in node (#2703)
11+
912
## 5.18.1
1013

1114
- [react] feat: Update peer dependencies for `react` and `react-dom` (#2694)

packages/apm/src/integrations/tracing.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,44 @@ export class Tracing implements Integration {
285285
});
286286
}
287287

288+
/**
289+
* Returns a new Transaction either continued from sentry-trace meta or a new one
290+
*/
291+
private static _getNewTransaction(hub: Hub, transactionContext: TransactionContext): Transaction {
292+
let traceId;
293+
let parentSpanId;
294+
let sampled;
295+
296+
const header = Tracing._getMeta('sentry-trace');
297+
if (header) {
298+
const span = SpanClass.fromTraceparent(header);
299+
if (span) {
300+
traceId = span.traceId;
301+
parentSpanId = span.parentSpanId;
302+
sampled = span.sampled;
303+
Tracing._log(
304+
`[Tracing] found 'sentry-meta' '<meta />' continuing trace with: trace_id: ${traceId} span_id: ${parentSpanId}`,
305+
);
306+
}
307+
}
308+
309+
return hub.startTransaction({
310+
parentSpanId,
311+
sampled,
312+
traceId,
313+
trimEnd: true,
314+
...transactionContext,
315+
}) as Transaction;
316+
}
317+
318+
/**
319+
* Returns the value of a meta tag
320+
*/
321+
private static _getMeta(metaName: string): string | null {
322+
const el = document.querySelector(`meta[name=${metaName}]`);
323+
return el ? el.getAttribute('content') : null;
324+
}
325+
288326
/**
289327
* Pings the heartbeat
290328
*/
@@ -456,10 +494,7 @@ export class Tracing implements Integration {
456494
return undefined;
457495
}
458496

459-
Tracing._activeTransaction = hub.startTransaction({
460-
trimEnd: true,
461-
...transactionContext,
462-
}) as Transaction;
497+
Tracing._activeTransaction = Tracing._getNewTransaction(hub, transactionContext);
463498

464499
// We set the transaction here on the scope so error events pick up the trace context and attach it to the error
465500
hub.configureScope(scope => scope.setSpan(Tracing._activeTransaction));

packages/node/src/handlers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function tracingHandler(): (
3434

3535
let traceId;
3636
let parentSpanId;
37+
let sampled;
3738

3839
// If there is a trace header set, we extract the data from it and set the span on the scope
3940
// to be the origin an created transaction set the parent_span_id / trace_id
@@ -42,13 +43,15 @@ export function tracingHandler(): (
4243
if (span) {
4344
traceId = span.traceId;
4445
parentSpanId = span.parentSpanId;
46+
sampled = span.sampled;
4547
}
4648
}
4749

4850
const transaction = startTransaction({
4951
name: `${reqMethod} ${reqUrl}`,
5052
op: 'http.server',
5153
parentSpanId,
54+
sampled,
5255
traceId,
5356
});
5457

0 commit comments

Comments
 (0)