1- import type {
2- ClientOptions ,
3- Scope ,
4- SentrySpanArguments ,
5- Span ,
6- SpanTimeInput ,
7- StartSpanOptions ,
8- TransactionArguments ,
9- } from '@sentry/types' ;
1+ import type { ClientOptions , Scope , SentrySpanArguments , Span , SpanTimeInput , StartSpanOptions } from '@sentry/types' ;
102
113import { propagationContextFromHeaders } from '@sentry/utils' ;
124import type { AsyncContextStrategy } from '../asyncContext' ;
135import { getMainCarrier } from '../asyncContext' ;
146import { getClient , getCurrentScope , getIsolationScope , withScope } from '../currentScopes' ;
157
16- import { getAsyncContextStrategy , getCurrentHub } from '../hub' ;
17- import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE } from '../semanticAttributes' ;
8+ import { getAsyncContextStrategy } from '../hub' ;
9+ import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE , SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '../semanticAttributes' ;
1810import { handleCallbackErrors } from '../utils/handleCallbackErrors' ;
1911import { hasTracingEnabled } from '../utils/hasTracingEnabled' ;
2012import { _getSpanForScope , _setSpanForScope } from '../utils/spanOnScope' ;
@@ -31,7 +23,6 @@ import { sampleSpan } from './sampling';
3123import { SentryNonRecordingSpan } from './sentryNonRecordingSpan' ;
3224import { SentrySpan } from './sentrySpan' ;
3325import { SPAN_STATUS_ERROR } from './spanstatus' ;
34- import { Transaction } from './transaction' ;
3526import { setCapturedScopesOnSpan } from './utils' ;
3627
3728/**
@@ -220,7 +211,7 @@ function createChildSpanOrTransaction({
220211 scope,
221212} : {
222213 parentSpan : SentrySpan | undefined ;
223- spanContext : TransactionArguments ;
214+ spanContext : SentrySpanArguments ;
224215 forceTransaction ?: boolean ;
225216 scope : Scope ;
226217} ) : Span {
@@ -232,34 +223,43 @@ function createChildSpanOrTransaction({
232223
233224 let span : Span ;
234225 if ( parentSpan && ! forceTransaction ) {
235- span = _startChild ( parentSpan , spanContext ) ;
226+ span = _startChildSpan ( parentSpan , spanContext ) ;
236227 addChildSpanToSpan ( parentSpan , span ) ;
237228 } else if ( parentSpan ) {
238229 // If we forced a transaction but have a parent span, make sure to continue from the parent span, not the scope
239230 const dsc = getDynamicSamplingContextFromSpan ( parentSpan ) ;
240231 const { traceId, spanId : parentSpanId } = parentSpan . spanContext ( ) ;
241- const sampled = spanIsSampled ( parentSpan ) ;
232+ const parentSampled = spanIsSampled ( parentSpan ) ;
242233
243- span = _startTransaction ( {
244- traceId,
245- parentSpanId,
246- parentSampled : sampled ,
247- ...spanContext ,
248- } ) ;
234+ span = _startRootSpan (
235+ {
236+ traceId,
237+ parentSpanId,
238+ ...spanContext ,
239+ } ,
240+ parentSampled ,
241+ ) ;
249242
250243 freezeDscOnSpan ( span , dsc ) ;
251244 } else {
252- const { traceId, dsc, parentSpanId, sampled } = {
245+ const {
246+ traceId,
247+ dsc,
248+ parentSpanId,
249+ sampled : parentSampled ,
250+ } = {
253251 ...isolationScope . getPropagationContext ( ) ,
254252 ...scope . getPropagationContext ( ) ,
255253 } ;
256254
257- span = _startTransaction ( {
258- traceId,
259- parentSpanId,
260- parentSampled : sampled ,
261- ...spanContext ,
262- } ) ;
255+ span = _startRootSpan (
256+ {
257+ traceId,
258+ parentSpanId,
259+ ...spanContext ,
260+ } ,
261+ parentSampled ,
262+ ) ;
263263
264264 if ( dsc ) {
265265 freezeDscOnSpan ( span , dsc ) ;
@@ -274,15 +274,15 @@ function createChildSpanOrTransaction({
274274}
275275
276276/**
277- * This converts StartSpanOptions to TransactionArguments .
277+ * This converts StartSpanOptions to SentrySpanArguments .
278278 * For the most part (for now) we accept the same options,
279279 * but some of them need to be transformed.
280280 *
281281 * Eventually the StartSpanOptions will be more aligned with OpenTelemetry.
282282 */
283- function normalizeContext ( context : StartSpanOptions ) : TransactionArguments {
283+ function normalizeContext ( context : StartSpanOptions ) : SentrySpanArguments {
284284 if ( context . startTime ) {
285- const ctx : TransactionArguments & { startTime ?: SpanTimeInput } = { ...context } ;
285+ const ctx : SentrySpanArguments & { startTime ?: SpanTimeInput } = { ...context } ;
286286 ctx . startTimestamp = spanTimeInputToSeconds ( context . startTime ) ;
287287 delete ctx . startTime ;
288288 return ctx ;
@@ -296,20 +296,29 @@ function getAcs(): AsyncContextStrategy {
296296 return getAsyncContextStrategy ( carrier ) ;
297297}
298298
299- function _startTransaction ( transactionContext : TransactionArguments ) : Transaction {
299+ function _startRootSpan ( spanArguments : SentrySpanArguments , parentSampled ?: boolean ) : SentrySpan {
300300 const client = getClient ( ) ;
301301 const options : Partial < ClientOptions > = ( client && client . getOptions ( ) ) || { } ;
302302
303- const { name, parentSampled , attributes } = transactionContext ;
303+ const { name = '' , attributes } = spanArguments ;
304304 const [ sampled , sampleRate ] = sampleSpan ( options , {
305305 name,
306306 parentSampled,
307307 attributes,
308- transactionContext,
308+ transactionContext : {
309+ name,
310+ parentSampled,
311+ } ,
309312 } ) ;
310313
311- // eslint-disable-next-line deprecation/deprecation
312- const transaction = new Transaction ( { ...transactionContext , sampled } , getCurrentHub ( ) ) ;
314+ const transaction = new SentrySpan ( {
315+ ...spanArguments ,
316+ attributes : {
317+ [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'custom' ,
318+ ...spanArguments . attributes ,
319+ } ,
320+ sampled,
321+ } ) ;
313322 if ( sampleRate !== undefined ) {
314323 transaction . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE , sampleRate ) ;
315324 }
@@ -325,12 +334,12 @@ function _startTransaction(transactionContext: TransactionArguments): Transactio
325334 * Creates a new `Span` while setting the current `Span.id` as `parentSpanId`.
326335 * This inherits the sampling decision from the parent span.
327336 */
328- function _startChild ( parentSpan : Span , spanContext : SentrySpanArguments ) : SentrySpan {
337+ function _startChildSpan ( parentSpan : Span , spanArguments : SentrySpanArguments ) : SentrySpan {
329338 const { spanId, traceId } = parentSpan . spanContext ( ) ;
330339 const sampled = spanIsSampled ( parentSpan ) ;
331340
332341 const childSpan = new SentrySpan ( {
333- ...spanContext ,
342+ ...spanArguments ,
334343 parentSpanId : spanId ,
335344 traceId,
336345 sampled,
@@ -342,7 +351,7 @@ function _startChild(parentSpan: Span, spanContext: SentrySpanArguments): Sentry
342351 if ( client ) {
343352 client . emit ( 'spanStart' , childSpan ) ;
344353 // If it has an endTimestamp, it's already ended
345- if ( spanContext . endTimestamp ) {
354+ if ( spanArguments . endTimestamp ) {
346355 client . emit ( 'spanEnd' , childSpan ) ;
347356 }
348357 }
0 commit comments