@@ -21,14 +21,14 @@ import {
2121 SpanStatus ,
2222 trace ,
2323 Span ,
24+ ROOT_CONTEXT ,
2425} from '@opentelemetry/api' ;
25- import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node' ;
2626import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks' ;
2727import * as testUtils from '@opentelemetry/contrib-test-utils' ;
2828import {
29- InMemorySpanExporter ,
30- SimpleSpanProcessor ,
31- } from '@opentelemetry/sdk-trace-base ' ;
29+ getTestSpans ,
30+ registerInstrumentationTesting ,
31+ } from '@opentelemetry/contrib-test-utils ' ;
3232import * as assert from 'assert' ;
3333import { RedisInstrumentation } from '../../src' ;
3434import {
@@ -40,15 +40,13 @@ import {
4040 SEMATTRS_NET_PEER_PORT ,
4141} from '@opentelemetry/semantic-conventions' ;
4242
43- const instrumentation = new RedisInstrumentation ( ) ;
44- instrumentation . enable ( ) ;
45- instrumentation . disable ( ) ;
43+ const instrumentation = registerInstrumentationTesting (
44+ new RedisInstrumentation ( )
45+ ) ;
4646
4747import * as redisTypes from 'redis' ;
4848import { RedisResponseCustomAttributeFunction } from '../../src/types' ;
4949
50- const memoryExporter = new InMemorySpanExporter ( ) ;
51-
5250const CONFIG = {
5351 host : process . env . OPENTELEMETRY_REDIS_HOST || 'localhost' ,
5452 port : Number ( process . env . OPENTELEMETRY_REDIS_PORT || 63790 ) ,
@@ -68,26 +66,10 @@ const unsetStatus: SpanStatus = {
6866} ;
6967
7068describe ( '[email protected] ' , ( ) => { 71- const provider = new NodeTracerProvider ( {
72- spanProcessors : [ new SimpleSpanProcessor ( memoryExporter ) ] ,
73- } ) ;
74- const tracer = provider . getTracer ( 'external' ) ;
7569 let redis : typeof redisTypes ;
7670 const shouldTestLocal = process . env . RUN_REDIS_TESTS_LOCAL ;
7771 const shouldTest = process . env . RUN_REDIS_TESTS || shouldTestLocal ;
78-
79- let contextManager : AsyncLocalStorageContextManager ;
80- beforeEach ( ( ) => {
81- contextManager = new AsyncLocalStorageContextManager ( ) . enable ( ) ;
82- context . setGlobalContextManager ( contextManager ) ;
83- // set the default tracer provider before each test
84- // specific ones can override it to assert certain things
85- instrumentation . setTracerProvider ( provider ) ;
86- } ) ;
87-
88- afterEach ( ( ) => {
89- context . disable ( ) ;
90- } ) ;
72+ const tracer = trace . getTracer ( 'external' ) ;
9173
9274 before ( function ( ) {
9375 // needs to be "function" to have MochaContext "this" context
10385 }
10486
10587 redis = require ( 'redis' ) ;
106- instrumentation . setTracerProvider ( provider ) ;
107- instrumentation . enable ( ) ;
10888 } ) ;
10989
11090 after ( ( ) => {
144124 expectedDbStatement : string ;
145125 method : ( cb : redisTypes . Callback < unknown > ) => unknown ;
146126 } > = [
147- {
148- description : 'insert' ,
149- command : 'hset' ,
150- args : [ 'hash' , 'random' , 'random' ] ,
151- expectedDbStatement : 'hash random [1 other arguments]' ,
152- method : ( cb : redisTypes . Callback < number > ) =>
153- client . hset ( 'hash' , 'random' , 'random' , cb ) ,
154- } ,
155- {
156- description : 'get' ,
157- command : 'get' ,
158- args : [ 'test' ] ,
159- expectedDbStatement : 'test' ,
160- method : ( cb : redisTypes . Callback < string | null > ) =>
161- client . get ( 'test' , cb ) ,
162- } ,
163- {
164- description : 'delete' ,
165- command : 'del' ,
166- args : [ 'test' ] ,
167- expectedDbStatement : 'test' ,
168- method : ( cb : redisTypes . Callback < number > ) => client . del ( 'test' , cb ) ,
169- } ,
170- ] ;
127+ {
128+ description : 'insert' ,
129+ command : 'hset' ,
130+ args : [ 'hash' , 'random' , 'random' ] ,
131+ expectedDbStatement : 'hash random [1 other arguments]' ,
132+ method : ( cb : redisTypes . Callback < number > ) =>
133+ client . hset ( 'hash' , 'random' , 'random' , cb ) ,
134+ } ,
135+ {
136+ description : 'get' ,
137+ command : 'get' ,
138+ args : [ 'test' ] ,
139+ expectedDbStatement : 'test' ,
140+ method : ( cb : redisTypes . Callback < string | null > ) =>
141+ client . get ( 'test' , cb ) ,
142+ } ,
143+ {
144+ description : 'delete' ,
145+ command : 'del' ,
146+ args : [ 'test' ] ,
147+ expectedDbStatement : 'test' ,
148+ method : ( cb : redisTypes . Callback < number > ) => client . del ( 'test' , cb ) ,
149+ } ,
150+ ] ;
171151
172152 before ( done => {
173153 client = redis . createClient ( URL ) ;
179159
180160 beforeEach ( done => {
181161 client . set ( 'test' , 'data' , ( ) => {
182- memoryExporter . reset ( ) ;
162+ testUtils . resetMemoryExporter ( )
183163 done ( ) ;
184164 } ) ;
185165 } ) ;
190170
191171 afterEach ( done => {
192172 client . del ( 'hash' , ( ) => {
193- memoryExporter . reset ( ) ;
173+ testUtils . resetMemoryExporter ( ) ;
194174 done ( ) ;
195175 } ) ;
196176 } ) ;
206186 context . with ( trace . setSpan ( context . active ( ) , span ) , ( ) => {
207187 operation . method ( ( err , _result ) => {
208188 assert . ifError ( err ) ;
209- assert . strictEqual ( memoryExporter . getFinishedSpans ( ) . length , 1 ) ;
189+ assert . strictEqual ( getTestSpans ( ) . length , 1 ) ;
210190 span . end ( ) ;
211- const endedSpans = memoryExporter . getFinishedSpans ( ) ;
191+ const endedSpans = getTestSpans ( ) ;
212192 assert . strictEqual ( endedSpans . length , 2 ) ;
213193 assert . strictEqual (
214194 endedSpans [ 0 ] . name ,
239219 } ) ;
240220 } ) ;
241221
242- describe ( 'Removing instrumentation' , ( ) => {
243- before ( ( ) => {
244- instrumentation . disable ( ) ;
245- } ) ;
246-
247- REDIS_OPERATIONS . forEach ( operation => {
248- it ( `should not create a child span for ${ operation . description } ` , done => {
249- const span = tracer . startSpan ( 'test span' ) ;
250- context . with ( trace . setSpan ( context . active ( ) , span ) , ( ) => {
251- operation . method ( ( err , _ ) => {
252- assert . ifError ( err ) ;
253- assert . strictEqual ( memoryExporter . getFinishedSpans ( ) . length , 0 ) ;
254- span . end ( ) ;
255- const endedSpans = memoryExporter . getFinishedSpans ( ) ;
256- assert . strictEqual ( endedSpans . length , 1 ) ;
257- assert . strictEqual ( endedSpans [ 0 ] , span ) ;
258- done ( ) ;
259- } ) ;
260- } ) ;
261- } ) ;
262- } ) ;
263- } ) ;
264-
265222 describe ( 'dbStatementSerializer config' , ( ) => {
266223 const dbStatementSerializer = ( cmdName : string , cmdArgs : Array < string | Buffer > ) => {
267224 return Array . isArray ( cmdArgs ) && cmdArgs . length
268225 ? `${ cmdName } ${ cmdArgs . join ( ' ' ) } `
269226 : cmdName ;
270227 } ;
271228
272- before ( ( ) => {
273- instrumentation . disable ( ) ;
229+ beforeEach ( ( ) => {
274230 instrumentation . setConfig ( { dbStatementSerializer } ) ;
275- instrumentation . enable ( ) ;
276231 } ) ;
277232
278233 REDIS_OPERATIONS . forEach ( operation => {
282237 operation . method ( ( err , _ ) => {
283238 assert . ifError ( err ) ;
284239 span . end ( ) ;
285- const endedSpans = memoryExporter . getFinishedSpans ( ) ;
240+ const endedSpans = getTestSpans ( ) ;
286241 assert . strictEqual ( endedSpans . length , 2 ) ;
287242 const expectedStatement = dbStatementSerializer (
288243 operation . command ,
312267 span . setAttribute ( dataFieldName , new String ( response ) . toString ( ) ) ;
313268 } ;
314269
315- before ( ( ) => {
316- instrumentation . disable ( ) ;
270+ beforeEach ( ( ) => {
317271 instrumentation . setConfig ( { responseHook } ) ;
318- instrumentation . enable ( ) ;
319272 } ) ;
320273
321274 REDIS_OPERATIONS . forEach ( operation => {
322275 it ( `should apply responseHook for operation ${ operation . description } ` , done => {
323276 operation . method ( ( err , reply ) => {
324277 assert . ifError ( err ) ;
325- const endedSpans = memoryExporter . getFinishedSpans ( ) ;
278+ const endedSpans = getTestSpans ( ) ;
326279 assert . strictEqual (
327280 endedSpans [ 0 ] . attributes [ dataFieldName ] ,
328281 new String ( reply ) . toString ( )
343296 throw 'Some kind of error' ;
344297 } ;
345298
346- before ( ( ) => {
347- instrumentation . disable ( ) ;
299+ beforeEach ( ( ) => {
348300 instrumentation . setConfig ( { responseHook : badResponseHook } ) ;
349- instrumentation . enable ( ) ;
350301 } ) ;
351302
352303 REDIS_OPERATIONS . forEach ( operation => {
353304 it ( `should not fail because of responseHook error for operation ${ operation . description } ` , done => {
354305 operation . method ( ( err , _reply ) => {
355306 assert . ifError ( err ) ;
356- const endedSpans = memoryExporter . getFinishedSpans ( ) ;
307+ const endedSpans = getTestSpans ( ) ;
357308 assert . strictEqual ( endedSpans . length , 1 ) ;
358309 done ( ) ;
359310 } ) ;
363314 } ) ;
364315
365316 describe ( 'requireParentSpan config' , ( ) => {
366- before ( ( ) => {
367- instrumentation . disable ( ) ;
317+ beforeEach ( ( ) => {
368318 instrumentation . setConfig ( { requireParentSpan : true } ) ;
369- instrumentation . enable ( ) ;
370319 } ) ;
371320
372321 REDIS_OPERATIONS . forEach ( operation => {
373322 it ( `should not create span without parent span for operation ${ operation . description } ` , done => {
374- operation . method ( ( err , _ ) => {
375- assert . ifError ( err ) ;
376- const endedSpans = memoryExporter . getFinishedSpans ( ) ;
377- assert . strictEqual ( endedSpans . length , 0 ) ;
378- done ( ) ;
323+ context . with ( ROOT_CONTEXT , ( ) => {
324+ operation . method ( ( err , _ ) => {
325+ assert . ifError ( err ) ;
326+ const endedSpans = getTestSpans ( ) ;
327+ assert . strictEqual ( endedSpans . length , 0 ) ;
328+ done ( ) ;
329+ } ) ;
379330 } ) ;
380331 } ) ;
381332
384335 context . with ( trace . setSpan ( context . active ( ) , span ) , ( ) => {
385336 operation . method ( ( err , _ ) => {
386337 assert . ifError ( err ) ;
387- const endedSpans = memoryExporter . getFinishedSpans ( ) ;
338+ const endedSpans = getTestSpans ( ) ;
388339 assert . strictEqual ( endedSpans . length , 1 ) ;
389340 done ( ) ;
390341 } ) ;
391342 } ) ;
392343 } ) ;
393344 } ) ;
394345 } ) ;
395-
396- describe ( 'setTracerProvider' , ( ) => {
397- before ( ( ) => {
398- instrumentation . disable ( ) ;
399- instrumentation . setConfig ( { } ) ;
400- instrumentation . enable ( ) ;
401- } ) ;
402-
403- it ( 'should use new tracer provider after setTracerProvider is called' , done => {
404- const testSpecificMemoryExporter = new InMemorySpanExporter ( ) ;
405- const spanProcessor = new SimpleSpanProcessor (
406- testSpecificMemoryExporter
407- ) ;
408- const tracerProvider = new NodeTracerProvider ( {
409- spanProcessors : [ spanProcessor ] ,
410- } ) ;
411-
412- // key point of this test, setting new tracer provider and making sure
413- // new spans use it.
414- instrumentation . setTracerProvider ( tracerProvider ) ;
415-
416- client . set ( 'test' , 'value-with-new-tracer-provider' , err => {
417- assert . ifError ( err ) ;
418- // assert that the span was exported by the new tracer provider
419- // which is using the test specific span processor
420- const spans = testSpecificMemoryExporter . getFinishedSpans ( ) ;
421- assert . strictEqual ( spans . length , 1 ) ;
422- done ( ) ;
423- } ) ;
424- } ) ;
425- } ) ;
426346 } ) ;
427347} ) ;
0 commit comments