99 callback | 1 | 2
1010 */
1111import { BasePlugin } from '@opentelemetry/core' ;
12- import { Span , StatusCode , Attributes , SpanKind , context , setSpan , suppressInstrumentation } from '@opentelemetry/api' ;
12+ import {
13+ Span ,
14+ StatusCode ,
15+ Attributes ,
16+ SpanKind ,
17+ context ,
18+ setSpan ,
19+ suppressInstrumentation ,
20+ Context ,
21+ } from '@opentelemetry/api' ;
1322import * as shimmer from 'shimmer' ;
1423import AWS from 'aws-sdk' ;
1524import { AttributeNames } from './enums' ;
@@ -43,16 +52,13 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
4352 shimmer . unwrap ( this . _moduleExports ?. Request . prototype , 'promise' ) ;
4453 }
4554
46- private _bindPromise ( target : Promise < any > , span : Span ) {
47- const thisPlugin = this ;
48-
55+ private _bindPromise ( target : Promise < any > , contextForCallbacks : Context ) {
4956 const origThen = target . then ;
5057 target . then = function ( onFulfilled , onRejected ) {
51- const newOnFulfilled = context . bind ( onFulfilled , setSpan ( context . active ( ) , span ) ) ;
52- const newOnRejected = context . bind ( onRejected , setSpan ( context . active ( ) , span ) ) ;
58+ const newOnFulfilled = context . bind ( onFulfilled , contextForCallbacks ) ;
59+ const newOnRejected = context . bind ( onRejected , contextForCallbacks ) ;
5360 return origThen . call ( this , newOnFulfilled , newOnRejected ) ;
5461 } ;
55-
5662 return target ;
5763 }
5864
@@ -96,25 +102,28 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
96102 }
97103 }
98104
99- private _registerCompletedEvent ( span : Span , request : AWS . Request < any , any > ) {
105+ private _registerCompletedEvent ( span : Span , request : AWS . Request < any , any > , completedEventContext : Context ) {
100106 const thisPlugin = this ;
101107 request . on ( 'complete' , ( response ) => {
102- if ( ! request [ thisPlugin . REQUEST_SPAN_KEY ] ) {
103- return ;
104- }
105- request [ thisPlugin . REQUEST_SPAN_KEY ] = undefined ;
106-
107- if ( response . error ) {
108- span . setAttribute ( AttributeNames . AWS_ERROR , response . error ) ;
109- }
110-
111- this . _callUserResponseHook ( span , response ) ;
112- this . servicesExtensions . responseHook ( response , span ) ;
113-
114- span . setAttributes ( {
115- [ AttributeNames . AWS_REQUEST_ID ] : response . requestId ,
108+ // read issue https://github.com/aspecto-io/opentelemetry-ext-js/issues/60
109+ context . with ( completedEventContext , ( ) => {
110+ if ( ! request [ thisPlugin . REQUEST_SPAN_KEY ] ) {
111+ return ;
112+ }
113+ request [ thisPlugin . REQUEST_SPAN_KEY ] = undefined ;
114+
115+ if ( response . error ) {
116+ span . setAttribute ( AttributeNames . AWS_ERROR , response . error ) ;
117+ }
118+
119+ this . _callUserResponseHook ( span , response ) ;
120+ this . servicesExtensions . responseHook ( response , span ) ;
121+
122+ span . setAttributes ( {
123+ [ AttributeNames . AWS_REQUEST_ID ] : response . requestId ,
124+ } ) ;
125+ span . end ( ) ;
116126 } ) ;
117- span . end ( ) ;
118127 } ) ;
119128 }
120129
@@ -137,11 +146,13 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
137146 requestMetadata . spanKind ,
138147 requestMetadata . spanName
139148 ) ;
149+ const activeContextWithSpan = setSpan ( context . active ( ) , span ) ;
150+ const callbackWithContext = context . bind ( callback , activeContextWithSpan ) ;
151+
140152 thisPlugin . _callUserPreRequestHook ( span , awsRequest ) ;
141- thisPlugin . _registerCompletedEvent ( span , awsRequest ) ;
153+ thisPlugin . _registerCompletedEvent ( span , awsRequest , activeContextWithSpan ) ;
142154
143- const callbackWithContext = context . bind ( callback , setSpan ( context . active ( ) , span ) ) ;
144- return context . with ( setSpan ( context . active ( ) , span ) , ( ) => {
155+ return context . with ( activeContextWithSpan , ( ) => {
145156 thisPlugin . servicesExtensions . requestPostSpanHook ( awsRequest ) ;
146157 return thisPlugin . _callOriginalFunction ( ( ) => original . call ( awsRequest , callbackWithContext ) ) ;
147158 } ) ;
@@ -167,15 +178,19 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
167178 requestMetadata . spanKind ,
168179 requestMetadata . spanName
169180 ) ;
181+
182+ const activeContextWithSpan = setSpan ( context . active ( ) , span ) ;
170183 thisPlugin . _callUserPreRequestHook ( span , awsRequest ) ;
171- thisPlugin . _registerCompletedEvent ( span , awsRequest ) ;
184+ thisPlugin . _registerCompletedEvent ( span , awsRequest , activeContextWithSpan ) ;
172185
173- const origPromise : Promise < any > = context . with ( setSpan ( context . active ( ) , span ) , ( ) => {
186+ const origPromise : Promise < any > = context . with ( activeContextWithSpan , ( ) => {
174187 thisPlugin . servicesExtensions . requestPostSpanHook ( awsRequest ) ;
175188 return thisPlugin . _callOriginalFunction ( ( ) => original . call ( awsRequest , arguments ) ) ;
176189 } ) ;
177190
178- return requestMetadata . isIncoming ? thisPlugin . _bindPromise ( origPromise , span ) : origPromise ;
191+ return requestMetadata . isIncoming
192+ ? thisPlugin . _bindPromise ( origPromise , activeContextWithSpan )
193+ : origPromise ;
179194 } ;
180195 }
181196
0 commit comments