11import type { Scope } from '@sentry/core' ;
2- import { addTracingExtensions , BaseClient , createCheckInEnvelope , SDK_VERSION } from '@sentry/core' ;
2+ import {
3+ addTracingExtensions ,
4+ BaseClient ,
5+ createCheckInEnvelope ,
6+ getDynamicSamplingContextFromClient ,
7+ SDK_VERSION ,
8+ } from '@sentry/core' ;
39import type {
410 CheckIn ,
511 ClientOptions ,
12+ DynamicSamplingContext ,
613 Event ,
714 EventHint ,
815 MonitorConfig ,
916 SerializedCheckIn ,
1017 Severity ,
1118 SeverityLevel ,
19+ TraceContext ,
1220} from '@sentry/types' ;
1321import { logger , uuid4 } from '@sentry/utils' ;
1422
@@ -72,7 +80,7 @@ export class EdgeClient extends BaseClient<EdgeClientOptions> {
7280 * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
7381 * to create a monitor automatically when sending a check in.
7482 */
75- public captureCheckIn ( checkIn : CheckIn , monitorConfig ?: MonitorConfig ) : string {
83+ public captureCheckIn ( checkIn : CheckIn , monitorConfig ?: MonitorConfig , scope ?: Scope ) : string {
7684 const id = checkIn . status !== 'in_progress' && checkIn . checkInId ? checkIn . checkInId : uuid4 ( ) ;
7785 if ( ! this . _isEnabled ( ) ) {
7886 __DEBUG_BUILD__ && logger . warn ( 'SDK not enabled, will not capture checkin.' ) ;
@@ -103,7 +111,20 @@ export class EdgeClient extends BaseClient<EdgeClientOptions> {
103111 } ;
104112 }
105113
106- const envelope = createCheckInEnvelope ( serializedCheckIn , this . getSdkMetadata ( ) , tunnel , this . getDsn ( ) ) ;
114+ const [ dynamicSamplingContext , traceContext ] = this . _getTraceInfoFromScope ( scope ) ;
115+ if ( traceContext ) {
116+ serializedCheckIn . contexts = {
117+ trace : traceContext ,
118+ } ;
119+ }
120+
121+ const envelope = createCheckInEnvelope (
122+ serializedCheckIn ,
123+ dynamicSamplingContext ,
124+ this . getSdkMetadata ( ) ,
125+ tunnel ,
126+ this . getDsn ( ) ,
127+ ) ;
107128
108129 __DEBUG_BUILD__ && logger . info ( 'Sending checkin:' , checkIn . monitorSlug , checkIn . status ) ;
109130 void this . _sendEnvelope ( envelope ) ;
@@ -124,4 +145,30 @@ export class EdgeClient extends BaseClient<EdgeClientOptions> {
124145 event . server_name = event . server_name || process . env . SENTRY_NAME ;
125146 return super . _prepareEvent ( event , hint , scope ) ;
126147 }
148+
149+ /** Extract trace information from scope */
150+ private _getTraceInfoFromScope (
151+ scope : Scope | undefined ,
152+ ) : [ dynamicSamplingContext : Partial < DynamicSamplingContext > | undefined , traceContext : TraceContext | undefined ] {
153+ if ( ! scope ) {
154+ return [ undefined , undefined ] ;
155+ }
156+
157+ const span = scope . getSpan ( ) ;
158+ if ( span ) {
159+ return [ span ?. transaction ?. getDynamicSamplingContext ( ) , span ?. getTraceContext ( ) ] ;
160+ }
161+
162+ const { traceId, spanId, parentSpanId, dsc } = scope . getPropagationContext ( ) ;
163+ const traceContext : TraceContext = {
164+ trace_id : traceId ,
165+ span_id : spanId ,
166+ parent_span_id : parentSpanId ,
167+ } ;
168+ if ( dsc ) {
169+ return [ dsc , traceContext ] ;
170+ }
171+
172+ return [ getDynamicSamplingContextFromClient ( traceId , this , scope ) , traceContext ] ;
173+ }
127174}
0 commit comments