1414 * limitations under the License.
1515 */
1616
17- import { SpanStatusCode , context , trace } from '@opentelemetry/api' ;
17+ import { SpanStatusCode , context , SpanKind , trace } from '@opentelemetry/api' ;
1818import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node' ;
1919import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks' ;
2020import {
@@ -26,6 +26,7 @@ import { AttributeNames } from '../src/enums/AttributeNames';
2626import { ExpressInstrumentation } from '../src' ;
2727import { createServer , httpRequest , serverWithMiddleware } from './utils' ;
2828import { SemanticAttributes } from '@opentelemetry/semantic-conventions' ;
29+ import * as testUtils from '@opentelemetry/contrib-test-utils' ;
2930
3031const instrumentation = new ExpressInstrumentation ( ) ;
3132instrumentation . enable ( ) ;
@@ -493,4 +494,43 @@ describe('ExpressInstrumentation', () => {
493494 ) ;
494495 } ) ;
495496 } ) ;
497+ it ( 'should work with ESM usage' , async ( ) => {
498+ await testUtils . runTestFixture ( {
499+ cwd : __dirname ,
500+ argv : [ 'fixtures/use-express.mjs' ] ,
501+ env : {
502+ NODE_OPTIONS :
503+ '--experimental-loader=@opentelemetry/instrumentation/hook.mjs' ,
504+ NODE_NO_WARNINGS : '1' ,
505+ } ,
506+ checkResult : ( err , stdout , stderr ) => {
507+ assert . ifError ( err ) ;
508+ } ,
509+ checkCollector : ( collector : testUtils . TestCollector ) => {
510+ // use-express.mjs creates an express app with a 'GET /post/:id' endpoint and
511+ // a `simpleMiddleware`, then makes a single 'GET /post/0' request. We
512+ // expect to see spans like this:
513+ // span 'GET /post/:id'
514+ // `- span 'middleware - query'
515+ // `- span 'middleware - expressInit'
516+ // `- span 'middleware - simpleMiddleware'
517+ // `- span 'request handler - /post/:id'
518+ const spans = collector . sortedSpans ;
519+ assert . strictEqual ( spans [ 0 ] . name , 'GET /post/:id' ) ;
520+ assert . strictEqual ( spans [ 0 ] . kind , SpanKind . CLIENT ) ;
521+ assert . strictEqual ( spans [ 1 ] . name , 'middleware - query' ) ;
522+ assert . strictEqual ( spans [ 1 ] . kind , SpanKind . SERVER ) ;
523+ assert . strictEqual ( spans [ 1 ] . parentSpanId , spans [ 0 ] . spanId ) ;
524+ assert . strictEqual ( spans [ 2 ] . name , 'middleware - expressInit' ) ;
525+ assert . strictEqual ( spans [ 2 ] . kind , SpanKind . SERVER ) ;
526+ assert . strictEqual ( spans [ 2 ] . parentSpanId , spans [ 0 ] . spanId ) ;
527+ assert . strictEqual ( spans [ 3 ] . name , 'middleware - simpleMiddleware' ) ;
528+ assert . strictEqual ( spans [ 3 ] . kind , SpanKind . SERVER ) ;
529+ assert . strictEqual ( spans [ 3 ] . parentSpanId , spans [ 0 ] . spanId ) ;
530+ assert . strictEqual ( spans [ 4 ] . name , 'request handler - /post/:id' ) ;
531+ assert . strictEqual ( spans [ 4 ] . kind , SpanKind . SERVER ) ;
532+ assert . strictEqual ( spans [ 4 ] . parentSpanId , spans [ 0 ] . spanId ) ;
533+ } ,
534+ } ) ;
535+ } ) ;
496536} ) ;
0 commit comments