@@ -41,46 +41,57 @@ module.exports.init = (_config, cls) => {
4141 value . module = instrumentation ;
4242 } ) ;
4343
44- const transformToInstanaSpan = ( otelSpan , kind , instrumentationModule , instrumentationName ) => {
45- let traceId ;
46- let parentSpanId ;
44+ const extractDataFromOtelSpan = ( otelSpan , instrumentationModule ) => {
45+ const obj = {
46+ kind : constants . EXIT
47+ } ;
48+
49+ if ( instrumentationModule . getKind ) {
50+ obj . kind = instrumentationModule . getKind ( otelSpan ) ;
51+ }
4752
4853 if ( instrumentationModule . getTraceId ) {
49- traceId = instrumentationModule . getTraceId ( kind , otelSpan ) ;
54+ obj . traceId = instrumentationModule . getTraceId ( obj . kind , otelSpan ) ;
5055 }
5156
5257 if ( instrumentationModule . getParentId ) {
53- parentSpanId = instrumentationModule . getParentId ( kind , otelSpan ) ;
58+ obj . parentSpanId = instrumentationModule . getParentId ( obj . kind , otelSpan ) ;
5459 }
5560
56- if ( instrumentationModule . transform ) {
57- otelSpan = instrumentationModule . transform ( otelSpan ) ;
58- }
61+ return obj ;
62+ } ;
5963
64+ const transformToInstanaSpan = ( otelSpan , data , instrumentationModule , instrumentationName ) => {
6065 if ( cls . tracingSuppressed ( ) ) {
6166 return ;
6267 }
6368
64- if ( kind === constants . EXIT && cls . skipExitTracing ( ) ) {
69+ if ( data . kind === constants . EXIT && cls . skipExitTracing ( ) ) {
6570 return ;
6671 }
6772
6873 try {
6974 const createInstanaSpan = onEndCallback => {
75+ const otelAttrs = instrumentationModule . getOtelAttributes
76+ ? instrumentationModule . getOtelAttributes ( otelSpan )
77+ : otelSpan . attributes ;
78+
79+ const otelResourceAttrs = otelSpan . resource . attributes ;
80+
7081 // NOTE: 'service.name' is "unknown" - probably because we don't setup otel completely.
7182 // The removal fixes incorrect infrastructure correlation.
72- delete otelSpan . resource . attributes [ 'service.name' ] ;
83+ delete otelResourceAttrs [ 'service.name' ] ;
7384
74- const spanAttributes = traceId
85+ const spanAttributes = data . traceId
7586 ? {
7687 spanName : 'otel' ,
77- kind : kind ,
78- traceId : traceId ,
79- parentSpanId : parentSpanId
88+ kind : data . kind ,
89+ traceId : data . traceId ,
90+ parentSpanId : data . parentSpanId
8091 }
8192 : {
8293 spanName : 'otel' ,
83- kind : kind
94+ kind : data . kind
8495 } ;
8596
8697 const instanaSpan = cls . startSpan ( spanAttributes ) ;
@@ -92,8 +103,8 @@ module.exports.init = (_config, cls) => {
92103 // span.data.operation is mapped to endpoint for otel span plugin in BE.
93104 // We need to set the endpoint otherwise the ui will show unspecified
94105 operation,
95- tags : Object . assign ( { name : otelSpan . name } , otelSpan . attributes ) ,
96- resource : otelSpan . resource . attributes
106+ tags : Object . assign ( { name : otelSpan . name } , otelAttrs ) ,
107+ resource : otelResourceAttrs
97108 } ;
98109
99110 otelSpan . _instanaSpan = instanaSpan ;
@@ -111,7 +122,7 @@ module.exports.init = (_config, cls) => {
111122 // For ENTRY spans, we need to keep the CLS context active for the duration of the span
112123 // so that async operations (like HTTP requests) can find the parent span.
113124 // For EXIT spans, we can use runAndReturn since they complete quickly.
114- if ( kind === constants . ENTRY ) {
125+ if ( data . kind === constants . ENTRY ) {
115126 const clsContext = cls . ns . createContext ( ) ;
116127 cls . ns . enter ( clsContext ) ;
117128
@@ -163,21 +174,18 @@ module.exports.init = (_config, cls) => {
163174 const instrumentationName = otelSpan . instrumentationScope ?. name || otelSpan . instrumentationLibrary ?. name ;
164175 const instrumentationModule = instrumentations [ instrumentationName ] ?. module ;
165176
177+ // CASE: we don't support this instrumentation
166178 if ( ! instrumentationModule ) {
167179 return orig . apply ( this , arguments ) ;
168180 }
169181
170- let kind = constants . EXIT ;
171-
172- if ( instrumentationModule . getKind ) {
173- kind = instrumentationModule . getKind ( otelSpan ) ;
174- }
182+ const data = extractDataFromOtelSpan ( otelSpan , instrumentationModule ) ;
175183
176- transformToInstanaSpan ( otelSpan , kind , instrumentationModule , instrumentationName ) ;
184+ transformToInstanaSpan ( otelSpan , data , instrumentationModule , instrumentationName ) ;
177185 const originalCtx = orig . apply ( this , arguments ) ;
178186
179187 if ( otelSpan && otelSpan . _instanaSpan ) {
180- if ( instrumentationModule . modifyContext && kind === constants . EXIT ) {
188+ if ( instrumentationModule . modifyContext && data . kind === constants . EXIT ) {
181189 return instrumentationModule . modifyContext ( api , otelSpan , otelSpan . _instanaSpan , originalCtx ) ;
182190 }
183191 }
0 commit comments