@@ -34,21 +34,6 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
34
34
const scope = hub . getScope ( ) ;
35
35
const client = hub . getClient ( ) ;
36
36
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
-
52
37
// This is our safeguard so people always get a Transaction in return.
53
38
// We set `_isTransaction: true` in {@link Sentry.startTransaction} to have a runtime check
54
39
// if the user really wanted to create a Transaction.
@@ -57,14 +42,17 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
57
42
logger . warn ( 'Will fall back to <unlabeled transaction>, use `transaction.setName()` to change it.' ) ;
58
43
}
59
44
60
- if ( ( newSpanContext as TransactionContext ) . name ) {
45
+ if ( ( context as TransactionContext ) . name ) {
61
46
// We are dealing with a Transaction
62
- const transaction = new Transaction ( newSpanContext as TransactionContext , hub ) ;
47
+ const transaction = new Transaction ( context as TransactionContext , hub ) ;
63
48
64
49
// We only roll the dice on sampling for root spans of transactions because all child spans inherit this state
65
50
if ( transaction . sampled === undefined ) {
66
51
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 ;
68
56
}
69
57
70
58
// We only want to create a span list if we sampled the transaction
@@ -77,7 +65,16 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
77
65
return transaction ;
78
66
}
79
67
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 ) ;
81
78
}
82
79
83
80
/**
0 commit comments