Skip to content

Commit 19840c3

Browse files
committed
ref: StartSpan creates a child of the span on scope
1 parent 6269bfd commit 19840c3

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

packages/apm/src/hubextensions.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
3434
const scope = hub.getScope();
3535
const client = hub.getClient();
3636

37-
let newSpanContext = context;
38-
39-
if (scope) {
40-
// If there is a Span on the Scope we use the span_id / trace_id
41-
// To define the parent <-> child relationship
42-
const parentSpan = scope.getSpan();
43-
if (parentSpan) {
44-
const { trace_id } = parentSpan.getTraceContext();
45-
newSpanContext = {
46-
traceId: trace_id,
47-
...context,
48-
};
49-
}
50-
}
51-
5237
// This is our safeguard so people always get a Transaction in return.
5338
// We set `_isTransaction: true` in {@link Sentry.startTransaction} to have a runtime check
5439
// if the user really wanted to create a Transaction.
@@ -57,14 +42,17 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
5742
logger.warn('Will fall back to <unlabeled transaction>, use `transaction.setName()` to change it.');
5843
}
5944

60-
if ((newSpanContext as TransactionContext).name) {
45+
if ((context as TransactionContext).name) {
6146
// We are dealing with a Transaction
62-
const transaction = new Transaction(newSpanContext as TransactionContext, hub);
47+
const transaction = new Transaction(context as TransactionContext, hub);
6348

6449
// We only roll the dice on sampling for root spans of transactions because all child spans inherit this state
6550
if (transaction.sampled === undefined) {
6651
const sampleRate = (client && client.getOptions().tracesSampleRate) || 0;
67-
transaction.sampled = Math.random() <= sampleRate;
52+
// if true = we want to have the transaction
53+
// if false = we don't want to have it
54+
// Math.random (inclusive of 0, but not 1)
55+
transaction.sampled = Math.random() < sampleRate;
6856
}
6957

7058
// We only want to create a span list if we sampled the transaction
@@ -77,7 +65,16 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
7765
return transaction;
7866
}
7967

80-
return new Span(newSpanContext);
68+
if (scope) {
69+
// If there is a Span on the Scope we start a child and return that instead
70+
const parentSpan = scope.getSpan();
71+
if (parentSpan) {
72+
return parentSpan.startChild(context);
73+
}
74+
}
75+
76+
// Otherwise we return a new Span
77+
return new Span(context);
8178
}
8279

8380
/**

packages/apm/src/integrations/express.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { logger } from '@sentry/utils';
33
// tslint:disable-next-line:no-implicit-dependencies
44
import { Application, ErrorRequestHandler, NextFunction, Request, RequestHandler, Response } from 'express';
55

6+
/**
7+
* Internal helper for `__sentry_transaction`
8+
* @hidden
9+
*/
610
interface SentryTracingResponse {
711
__sentry_transaction?: Transaction;
812
}
@@ -73,9 +77,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
7377
op: 'middleware',
7478
});
7579
res.once('finish', () => {
76-
if (span) {
77-
span.finish();
78-
}
80+
span.finish();
7981
});
8082
}
8183
return fn.apply(this, arguments);

packages/node/src/handlers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ export function tracingHandler(): (
4040
if (req.headers && isString(req.headers['sentry-trace'])) {
4141
const span = Span.fromTraceparent(req.headers['sentry-trace'] as string);
4242
if (span) {
43-
const spanData = span.toJSON();
44-
traceId = spanData.trace_id;
45-
parentSpanId = spanData.span_id;
43+
traceId = span.traceId;
44+
parentSpanId = span.parentSpanId;
4645
}
4746
}
4847

@@ -57,6 +56,7 @@ export function tracingHandler(): (
5756
getCurrentHub().configureScope(scope => {
5857
scope.setSpan(transaction);
5958
});
59+
6060
// We also set __sentry_transaction on the response so people can grab the transaction there to add
6161
// spans to it later.
6262
(res as any).__sentry_transaction = transaction;

0 commit comments

Comments
 (0)