@@ -15,6 +15,7 @@ import type {
15
15
Integration ,
16
16
IntegrationClass ,
17
17
Outcome ,
18
+ PropagationContext ,
18
19
SdkMetadata ,
19
20
Session ,
20
21
SessionAggregates ,
@@ -29,6 +30,7 @@ import {
29
30
addItemToEnvelope ,
30
31
checkOrSetAlreadyCaught ,
31
32
createAttachmentEnvelopeItem ,
33
+ dropUndefinedKeys ,
32
34
isPlainObject ,
33
35
isPrimitive ,
34
36
isThenable ,
@@ -41,6 +43,7 @@ import {
41
43
} from '@sentry/utils' ;
42
44
43
45
import { getEnvelopeEndpointWithUrlEncodedAuth } from './api' ;
46
+ import { DEFAULT_ENVIRONMENT } from './constants' ;
44
47
import { createEventEnvelope , createSessionEnvelope } from './envelope' ;
45
48
import type { IntegrationIndex } from './integration' ;
46
49
import { setupIntegration , setupIntegrations } from './integration' ;
@@ -507,7 +510,49 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
507
510
if ( ! hint . integrations && integrations . length > 0 ) {
508
511
hint . integrations = integrations ;
509
512
}
510
- return prepareEvent ( options , event , hint , scope ) ;
513
+ return prepareEvent ( options , event , hint , scope ) . then ( evt => {
514
+ if ( evt === null ) {
515
+ return evt ;
516
+ }
517
+
518
+ // If a trace context is not set on the event, we use the propagationContext set on the event to
519
+ // generate a trace context. If the propagationContext does not have a dynamic sampling context, we
520
+ // also generate one for it.
521
+ const { propagationContext } = evt . sdkProcessingMetadata || { } ;
522
+ const trace = evt . contexts && evt . contexts . trace ;
523
+ if ( ! trace && propagationContext ) {
524
+ const { traceId : trace_id , spanId, parentSpanId, dsc } = propagationContext as PropagationContext ;
525
+ evt . contexts = {
526
+ trace : {
527
+ trace_id,
528
+ span_id : spanId ,
529
+ parent_span_id : parentSpanId ,
530
+ } ,
531
+ ...evt . contexts ,
532
+ } ;
533
+
534
+ const { publicKey : public_key } = this . getDsn ( ) || { } ;
535
+ const { segment : user_segment } = ( scope && scope . getUser ( ) ) || { } ;
536
+
537
+ let dynamicSamplingContext = dsc ;
538
+ if ( ! dsc ) {
539
+ dynamicSamplingContext = dropUndefinedKeys ( {
540
+ environment : options . environment || DEFAULT_ENVIRONMENT ,
541
+ release : options . release ,
542
+ user_segment,
543
+ public_key,
544
+ trace_id,
545
+ } ) ;
546
+ this . emit && this . emit ( 'createDsc' , dynamicSamplingContext ) ;
547
+ }
548
+
549
+ evt . sdkProcessingMetadata = {
550
+ dynamicSamplingContext,
551
+ ...evt . sdkProcessingMetadata ,
552
+ } ;
553
+ }
554
+ return evt ;
555
+ } ) ;
511
556
}
512
557
513
558
/**
0 commit comments