11/* eslint-disable max-lines */
2- import { getCurrentHub } from '@sentry/hub' ;
3- import { Hub , Primitive , Span as SpanInterface , SpanContext , TraceHeaders , Transaction } from '@sentry/types' ;
2+ import { getCurrentHub , Hub } from '@sentry/hub' ;
3+ import { Primitive , Span as SpanInterface , SpanContext , TraceHeaders , Transaction } from '@sentry/types' ;
44import { dropUndefinedKeys , logger , timestampWithMs , uuid4 } from '@sentry/utils' ;
55
66import { SpanStatus } from './spanstatus' ;
@@ -288,6 +288,14 @@ export class Span implements SpanInterface {
288288 * @inheritDoc
289289 */
290290 public getTraceHeaders ( ) : TraceHeaders {
291+ // if this span is part of a transaction, but that transaction doesn't yet have a tracestate value, create one
292+ if ( this . transaction && ! this . transaction ?. metadata . tracestate ?. sentry ) {
293+ this . transaction . metadata . tracestate = {
294+ ...this . transaction . metadata . tracestate ,
295+ sentry : this . _getNewTracestate ( ) ,
296+ } ;
297+ }
298+
291299 const tracestate = this . _toTracestate ( ) ;
292300
293301 return {
@@ -357,8 +365,11 @@ export class Span implements SpanInterface {
357365 *
358366 * @returns The new Sentry tracestate entry, or undefined if there's no client or no dsn
359367 */
360- protected _getNewTracestate ( hub : Hub = getCurrentHub ( ) ) : string | undefined {
368+ protected _getNewTracestate ( ) : string | undefined {
369+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
370+ const hub = ( ( this . transaction as any ) ?. _hub as Hub ) || getCurrentHub ( ) ;
361371 const client = hub . getClient ( ) ;
372+ const { id : userId , segment : userSegment } = hub . getScope ( ) ?. getUser ( ) || { } ;
362373 const dsn = client ?. getDsn ( ) ;
363374
364375 if ( ! client || ! dsn ) {
@@ -372,11 +383,12 @@ export class Span implements SpanInterface {
372383 // `dsn.publicKey` required and remove the `!`.
373384
374385 return `sentry=${ computeTracestateValue ( {
375- traceId : this . traceId ,
386+ trace_id : this . traceId ,
376387 environment,
377388 release,
378389 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
379- publicKey : dsn . publicKey ! ,
390+ public_key : dsn . publicKey ! ,
391+ user : { id : userId , segment : userSegment } ,
380392 } ) } `;
381393 }
382394
@@ -392,9 +404,11 @@ export class Span implements SpanInterface {
392404 }
393405
394406 /**
395- * Return a tracestate-compatible header string. Returns undefined if there is no client or no DSN.
407+ * Return a tracestate-compatible header string, including both sentry and third-party data (if any). Returns
408+ * undefined if there is no client or no DSN.
396409 */
397410 private _toTracestate ( ) : string | undefined {
411+ // if this is an orphan span, create a new tracestate value
398412 const sentryTracestate = this . transaction ?. metadata ?. tracestate ?. sentry || this . _getNewTracestate ( ) ;
399413 let thirdpartyTracestate = this . transaction ?. metadata ?. tracestate ?. thirdparty ;
400414
0 commit comments