@@ -21,7 +21,7 @@ import {
21
21
safeExecuteInTheMiddle ,
22
22
} from '@opentelemetry/instrumentation' ;
23
23
import {
24
- endSpan ,
24
+ endSpan ,
25
25
getTracedCreateClient ,
26
26
getTracedCreateStreamTrace ,
27
27
} from './utils' ;
@@ -31,15 +31,15 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
31
31
import { RedisPluginClientTypes } from './internal-types' ;
32
32
import { SpanKind , context , trace } from '@opentelemetry/api' ;
33
33
import {
34
- DBSYSTEMVALUES_REDIS ,
35
- SEMATTRS_DB_CONNECTION_STRING ,
36
- SEMATTRS_DB_STATEMENT ,
37
- SEMATTRS_DB_SYSTEM ,
38
- SEMATTRS_NET_PEER_NAME ,
39
- SEMATTRS_NET_PEER_PORT ,
40
- } from '@opentelemetry/semantic-conventions' ;
34
+ DBSYSTEMVALUES_REDIS ,
35
+ SEMATTRS_DB_CONNECTION_STRING ,
36
+ SEMATTRS_DB_STATEMENT ,
37
+ SEMATTRS_DB_SYSTEM ,
38
+ SEMATTRS_NET_PEER_NAME ,
39
+ SEMATTRS_NET_PEER_PORT ,
40
+ } from '@opentelemetry/semantic-conventions' ;
41
41
import { defaultDbStatementSerializer } from '@opentelemetry/redis-common' ;
42
-
42
+
43
43
const DEFAULT_CONFIG : RedisInstrumentationConfig = {
44
44
requireParentSpan : false ,
45
45
} ;
@@ -115,91 +115,97 @@ export class RedisInstrumentation extends InstrumentationBase<RedisInstrumentati
115
115
private _getPatchInternalSendCommand ( ) {
116
116
const instrumentation = this ;
117
117
return function internal_send_command ( original : Function ) {
118
- return function internal_send_command_trace (
119
- this : RedisPluginClientTypes ,
120
- cmd ?: RedisCommand
121
- ) {
122
- // Versions of redis (2.4+) use a single options object
123
- // instead of named arguments
124
- if ( arguments . length !== 1 || typeof cmd !== 'object' ) {
125
- // We don't know how to trace this call, so don't start/stop a span
126
- return original . apply ( this , arguments ) ;
127
- }
118
+ return function internal_send_command_trace (
119
+ this : RedisPluginClientTypes ,
120
+ cmd ?: RedisCommand
121
+ ) {
122
+ // Versions of redis (2.4+) use a single options object
123
+ // instead of named arguments
124
+ if ( arguments . length !== 1 || typeof cmd !== 'object' ) {
125
+ // We don't know how to trace this call, so don't start/stop a span
126
+ return original . apply ( this , arguments ) ;
127
+ }
128
128
129
- const config = instrumentation . getConfig ( ) ;
130
-
131
- const hasNoParentSpan = trace . getSpan ( context . active ( ) ) === undefined ;
132
- if ( config . requireParentSpan === true && hasNoParentSpan ) {
133
- return original . apply ( this , arguments ) ;
134
- }
135
-
136
- const dbStatementSerializer =
137
- config ?. dbStatementSerializer || defaultDbStatementSerializer ;
138
- const span = instrumentation . tracer . startSpan (
139
- `${ RedisInstrumentation . COMPONENT } -${ cmd . command } ` ,
140
- {
141
- kind : SpanKind . CLIENT ,
142
- attributes : {
143
- [ SEMATTRS_DB_SYSTEM ] : DBSYSTEMVALUES_REDIS ,
144
- [ SEMATTRS_DB_STATEMENT ] : dbStatementSerializer ( cmd . command , cmd . args ) ,
129
+ const config = instrumentation . getConfig ( ) ;
130
+
131
+ const hasNoParentSpan = trace . getSpan ( context . active ( ) ) === undefined ;
132
+ if ( config . requireParentSpan === true && hasNoParentSpan ) {
133
+ return original . apply ( this , arguments ) ;
134
+ }
135
+
136
+ const dbStatementSerializer =
137
+ config ?. dbStatementSerializer || defaultDbStatementSerializer ;
138
+ const span = instrumentation . tracer . startSpan (
139
+ `${ RedisInstrumentation . COMPONENT } -${ cmd . command } ` ,
140
+ {
141
+ kind : SpanKind . CLIENT ,
142
+ attributes : {
143
+ [ SEMATTRS_DB_SYSTEM ] : DBSYSTEMVALUES_REDIS ,
144
+ [ SEMATTRS_DB_STATEMENT ] : dbStatementSerializer (
145
+ cmd . command ,
146
+ cmd . args
147
+ ) ,
148
+ } ,
149
+ }
150
+ ) ;
151
+
152
+ // Set attributes for not explicitly typed RedisPluginClientTypes
153
+ if ( this . connection_options ) {
154
+ span . setAttributes ( {
155
+ [ SEMATTRS_NET_PEER_NAME ] : this . connection_options . host ,
156
+ [ SEMATTRS_NET_PEER_PORT ] : this . connection_options . port ,
157
+ } ) ;
158
+ }
159
+ if ( this . address ) {
160
+ span . setAttribute (
161
+ SEMATTRS_DB_CONNECTION_STRING ,
162
+ `redis://${ this . address } `
163
+ ) ;
164
+ }
165
+
166
+ const originalCallback = arguments [ 0 ] . callback ;
167
+ if ( originalCallback ) {
168
+ const originalContext = context . active ( ) ;
169
+ ( arguments [ 0 ] as RedisCommand ) . callback = function callback < T > (
170
+ this : unknown ,
171
+ err : Error | null ,
172
+ reply : T
173
+ ) {
174
+ if ( config ?. responseHook ) {
175
+ const responseHook = config . responseHook ;
176
+ safeExecuteInTheMiddle (
177
+ ( ) => {
178
+ responseHook ( span , cmd . command , cmd . args , reply ) ;
145
179
} ,
146
- }
147
- ) ;
148
-
149
- // Set attributes for not explicitly typed RedisPluginClientTypes
150
- if ( this . connection_options ) {
151
- span . setAttributes ( {
152
- [ SEMATTRS_NET_PEER_NAME ] : this . connection_options . host ,
153
- [ SEMATTRS_NET_PEER_PORT ] : this . connection_options . port ,
154
- } ) ;
155
- }
156
- if ( this . address ) {
157
- span . setAttribute (
158
- SEMATTRS_DB_CONNECTION_STRING ,
159
- `redis://${ this . address } `
180
+ err => {
181
+ if ( err ) {
182
+ instrumentation . _diag . error (
183
+ 'Error executing responseHook' ,
184
+ err
185
+ ) ;
186
+ }
187
+ } ,
188
+ true
160
189
) ;
161
190
}
162
-
163
- const originalCallback = arguments [ 0 ] . callback ;
164
- if ( originalCallback ) {
165
- const originalContext = context . active ( ) ;
166
- ( arguments [ 0 ] as RedisCommand ) . callback = function callback < T > (
167
- this : unknown ,
168
- err : Error | null ,
169
- reply : T
170
- ) {
171
- if ( config ?. responseHook ) {
172
- const responseHook = config . responseHook ;
173
- safeExecuteInTheMiddle (
174
- ( ) => {
175
- responseHook ( span , cmd . command , cmd . args , reply ) ;
176
- } ,
177
- err => {
178
- if ( err ) {
179
- instrumentation . _diag . error ( 'Error executing responseHook' , err ) ;
180
- }
181
- } ,
182
- true
183
- ) ;
184
- }
185
-
186
- endSpan ( span , err ) ;
187
- return context . with (
188
- originalContext ,
189
- originalCallback ,
190
- this ,
191
- ...arguments
192
- ) ;
193
- } ;
194
- }
195
- try {
196
- // Span will be ended in callback
197
- return original . apply ( this , arguments ) ;
198
- } catch ( rethrow : any ) {
199
- endSpan ( span , rethrow ) ;
200
- throw rethrow ; // rethrow after ending span
201
- }
191
+
192
+ endSpan ( span , err ) ;
193
+ return context . with (
194
+ originalContext ,
195
+ originalCallback ,
196
+ this ,
197
+ ...arguments
198
+ ) ;
202
199
} ;
200
+ }
201
+ try {
202
+ // Span will be ended in callback
203
+ return original . apply ( this , arguments ) ;
204
+ } catch ( rethrow : any ) {
205
+ endSpan ( span , rethrow ) ;
206
+ throw rethrow ; // rethrow after ending span
207
+ }
208
+ } ;
203
209
} ;
204
210
}
205
211
0 commit comments