Skip to content

Commit 4bafc30

Browse files
committed
feat: Pick up sentry-trace in frontend
1 parent 016fc00 commit 4bafc30

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

packages/apm/src/integrations/tracing.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,52 @@ export class Tracing implements Integration {
283283
});
284284
}
285285

286+
/**
287+
* Returns a new Transaction either continued from sentry-trace meta or a new one
288+
*/
289+
private static _getNewTransaction(hub: Hub, transactionContext: TransactionContext): Transaction {
290+
let traceId;
291+
let parentSpanId;
292+
let sampled;
293+
294+
const header = Tracing._getMeta('sentry-trace');
295+
if (header) {
296+
const span = SpanClass.fromTraceparent(header);
297+
if (span) {
298+
traceId = span.traceId;
299+
parentSpanId = span.parentSpanId;
300+
sampled = span.sampled;
301+
}
302+
Tracing._log(
303+
`[Tracing] found 'sentry-meta' '<meta />' continuing trace with: trace_id: ${traceId} span_id: ${parentSpanId}`,
304+
);
305+
}
306+
307+
return hub.startTransaction({
308+
parentSpanId,
309+
sampled,
310+
traceId,
311+
trimEnd: true,
312+
...transactionContext,
313+
}) as Transaction;
314+
}
315+
316+
/**
317+
* Returns the value of a meta tag
318+
*/
319+
private static _getMeta(metaName: string): string | null {
320+
const metas = document.getElementsByTagName('meta');
321+
322+
// tslint:disable-next-line: prefer-for-of
323+
for (let i = 0; i < metas.length; i++) {
324+
if (metas[i].getAttribute('name') === metaName) {
325+
return metas[i].getAttribute('content');
326+
}
327+
}
328+
329+
return null;
330+
}
331+
286332
/**
287333
* Pings the heartbeat
288334
*/
@@ -454,10 +500,7 @@ export class Tracing implements Integration {
454500
return undefined;
455501
}
456502

457-
Tracing._activeTransaction = hub.startTransaction({
458-
trimEnd: true,
459-
...transactionContext,
460-
}) as Transaction;
503+
Tracing._activeTransaction = Tracing._getNewTransaction(hub, transactionContext);
461504

462505
// We set the transaction here on the scope so error events pick up the trace context and attach it to the error
463506
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)