1- import { Attributes , RawAttribute , RawAttributes } from '../attributes' ;
21import type { Client } from '../client' ;
32import { getClient , getGlobalScope } from '../currentScopes' ;
43import { DEBUG_BUILD } from '../debug-build' ;
@@ -17,13 +16,12 @@ import {
1716} from '../semanticAttributes' ;
1817import { getCapturedScopesOnSpan } from '../tracing/utils' ;
1918import type { SerializedAttributes } from '../types-hoist/attributes' ;
20- import { Contexts } from '../types-hoist/context' ;
2119import type { Span , SpanV2JSON } from '../types-hoist/span' ;
2220import { mergeScopeData } from '../utils/applyScopeDataToEvent' ;
2321import { isV2BeforeSendSpanCallback } from '../utils/beforeSendSpan' ;
2422import { debug } from '../utils/debug-logger' ;
2523import { INTERNAL_getSegmentSpan , spanToV2JSON } from '../utils/spanUtils' ;
26- import { applyBeforeSendSpanCallback , safeSetSpanJSONAttributes } from './spanFirstUtils' ;
24+ import { applyBeforeSendSpanCallback , contextsToAttributes , safeSetSpanJSONAttributes } from './spanFirstUtils' ;
2725/**
2826 * Captures a span and returns a JSON representation to be enqueued for sending.
2927 *
@@ -43,14 +41,13 @@ export function captureSpan(span: Span, client = getClient()): void {
4341 const serializedSegmentSpan = spanToV2JSON ( segmentSpan ) ;
4442
4543 const { isolationScope : spanIsolationScope , scope : spanScope } = getCapturedScopesOnSpan ( span ) ;
46- const finalScopeData = getFinalScopeData ( spanIsolationScope , spanScope ) ;
4744
48- const originalAttributes = serializedSegmentSpan . attributes ?? { } ;
45+ const finalScopeData = getFinalScopeData ( spanIsolationScope , spanScope ) ;
4946
50- applyCommonSpanAttributes ( spanJSON , serializedSegmentSpan , client , finalScopeData , originalAttributes ) ;
47+ applyCommonSpanAttributes ( spanJSON , serializedSegmentSpan , client , finalScopeData ) ;
5148
5249 if ( span === segmentSpan ) {
53- applyScopeToSegmentSpan ( spanJSON , finalScopeData , originalAttributes ) ;
50+ applyScopeToSegmentSpan ( spanJSON , finalScopeData ) ;
5451 }
5552
5653 // Allow integrations to add additional data to the span JSON
@@ -69,49 +66,40 @@ export function captureSpan(span: Span, client = getClient()): void {
6966 client . emit ( 'enqueueSpan' , spanWithRef ) ;
7067}
7168
72- function applyScopeToSegmentSpan (
73- segmentSpanJSON : SpanV2JSON ,
74- scopeData : ScopeData ,
75- originalAttributes : SerializedAttributes ,
76- ) : void {
69+ function applyScopeToSegmentSpan ( segmentSpanJSON : SpanV2JSON , scopeData : ScopeData ) : void {
7770 // TODO: Apply all scope and request data from auto instrumentation (contexts, request) to segment span
7871 const { contexts } = scopeData ;
7972
80- safeSetSpanJSONAttributes ( segmentSpanJSON , contextsToAttributes ( contexts ) , originalAttributes ) ;
73+ safeSetSpanJSONAttributes ( segmentSpanJSON , contextsToAttributes ( contexts ) ) ;
8174}
8275
8376function applyCommonSpanAttributes (
8477 spanJSON : SpanV2JSON ,
8578 serializedSegmentSpan : SpanV2JSON ,
8679 client : Client ,
8780 scopeData : ScopeData ,
88- originalAttributes : SerializedAttributes ,
8981) : void {
9082 const sdk = client . getSdkMetadata ( ) ;
9183 const { release, environment, sendDefaultPii } = client . getOptions ( ) ;
9284
9385 // avoid overwriting any previously set attributes (from users or potentially our SDK instrumentation)
94- safeSetSpanJSONAttributes (
95- spanJSON ,
96- {
97- [ SEMANTIC_ATTRIBUTE_SENTRY_RELEASE ] : release ,
98- [ SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT ] : environment ,
99- [ SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME ] : serializedSegmentSpan . name ,
100- [ SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID ] : serializedSegmentSpan . span_id ,
101- [ SEMANTIC_ATTRIBUTE_SENTRY_SDK_NAME ] : sdk ?. sdk ?. name ,
102- [ SEMANTIC_ATTRIBUTE_SENTRY_SDK_VERSION ] : sdk ?. sdk ?. version ,
103- ...( sendDefaultPii
104- ? {
105- [ SEMANTIC_ATTRIBUTE_USER_ID ] : scopeData . user ?. id ,
106- [ SEMANTIC_ATTRIBUTE_USER_EMAIL ] : scopeData . user ?. email ,
107- [ SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS ] : scopeData . user ?. ip_address ?? undefined ,
108- [ SEMANTIC_ATTRIBUTE_USER_USERNAME ] : scopeData . user ?. username ,
109- }
110- : { } ) ,
111- ...scopeData . attributes ,
112- } ,
113- originalAttributes ,
114- ) ;
86+ safeSetSpanJSONAttributes ( spanJSON , {
87+ [ SEMANTIC_ATTRIBUTE_SENTRY_RELEASE ] : release ,
88+ [ SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT ] : environment ,
89+ [ SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME ] : serializedSegmentSpan . name ,
90+ [ SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID ] : serializedSegmentSpan . span_id ,
91+ [ SEMANTIC_ATTRIBUTE_SENTRY_SDK_NAME ] : sdk ?. sdk ?. name ,
92+ [ SEMANTIC_ATTRIBUTE_SENTRY_SDK_VERSION ] : sdk ?. sdk ?. version ,
93+ ...( sendDefaultPii
94+ ? {
95+ [ SEMANTIC_ATTRIBUTE_USER_ID ] : scopeData . user ?. id ,
96+ [ SEMANTIC_ATTRIBUTE_USER_EMAIL ] : scopeData . user ?. email ,
97+ [ SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS ] : scopeData . user ?. ip_address ,
98+ [ SEMANTIC_ATTRIBUTE_USER_USERNAME ] : scopeData . user ?. username ,
99+ }
100+ : { } ) ,
101+ ...scopeData . attributes ,
102+ } ) ;
115103}
116104
117105// TODO: Extract this to a helper in core. It's used in multiple places.
@@ -125,37 +113,3 @@ function getFinalScopeData(isolationScope: Scope | undefined, scope: Scope | und
125113 }
126114 return finalScopeData ;
127115}
128-
129- // TODO: This should likely live in the Context integration since most of this data is only avialable in server runtime contexts
130- function contextsToAttributes ( contexts : Contexts ) : RawAttributes < Record < string , unknown > > {
131- return {
132- // os context
133- 'os.build_id' : contexts . os ?. build ,
134- 'os.name' : contexts . os ?. name ,
135- 'os.version' : contexts . os ?. version ,
136- // TODO: Add to Sentry SemConv
137- 'os.kernel_version' : contexts . os ?. kernel_version ,
138-
139- // runtime context
140- // TODO: Add to Sentry SemConv
141- 'runtime.name' : contexts . runtime ?. name ,
142- // TODO: Add to Sentry SemConv
143- 'runtime.version' : contexts . runtime ?. version ,
144-
145- // TODO: All of them need to be added to Sentry SemConv (except family and model)
146- ...( contexts . app
147- ? Object . fromEntries ( Object . entries ( contexts . app ) . map ( ( [ key , value ] ) => [ `app.${ key } ` , value ] ) )
148- : { } ) ,
149- ...( contexts . device
150- ? Object . fromEntries ( Object . entries ( contexts . device ) . map ( ( [ key , value ] ) => [ `device.${ key } ` , value ] ) )
151- : { } ) ,
152- ...( contexts . culture
153- ? Object . fromEntries ( Object . entries ( contexts . culture ) . map ( ( [ key , value ] ) => [ `culture.${ key } ` , value ] ) )
154- : { } ) ,
155- ...( contexts . cloud_resource
156- ? Object . fromEntries (
157- Object . entries ( contexts . cloud_resource ) . map ( ( [ key , value ] ) => [ `cloud_resource.${ key } ` , value ] ) ,
158- )
159- : { } ) ,
160- } ;
161- }
0 commit comments