17
17
import { BasePlugin } from '@opentelemetry/core' ;
18
18
import { Attributes } from '@opentelemetry/types' ;
19
19
import * as express from 'express' ;
20
- import * as core from " express-serve-static-core" ;
20
+ import * as core from ' express-serve-static-core' ;
21
21
import * as shimmer from 'shimmer' ;
22
22
import {
23
23
ExpressLayer ,
@@ -26,13 +26,9 @@ import {
26
26
PatchedRequest ,
27
27
Parameters ,
28
28
PathParams ,
29
- _MIDDLEWARES_STORE_PROPERTY
29
+ _MIDDLEWARES_STORE_PROPERTY ,
30
30
} from './types' ;
31
- import {
32
- getLayerMetadata ,
33
- storeLayerPath ,
34
- patchEnd ,
35
- } from './utils'
31
+ import { getLayerMetadata , storeLayerPath , patchEnd } from './utils' ;
36
32
import { VERSION } from './version' ;
37
33
38
34
/**
@@ -59,7 +55,8 @@ export class ExpressPlugin extends BasePlugin<typeof express> {
59
55
if ( this . _moduleExports === undefined || this . _moduleExports === null ) {
60
56
return this . _moduleExports ;
61
57
}
62
- const routerProto = ( this . _moduleExports . Router as unknown ) as express . Router ;
58
+ const routerProto = ( this . _moduleExports
59
+ . Router as unknown ) as express . Router ;
63
60
64
61
this . _logger . debug ( 'patching express.Router.prototype.route' ) ;
65
62
shimmer . wrap ( routerProto , 'route' , this . _getRoutePatch . bind ( this ) ) ;
@@ -68,52 +65,67 @@ export class ExpressPlugin extends BasePlugin<typeof express> {
68
65
shimmer . wrap ( routerProto , 'use' , this . _getRouterUsePatch . bind ( this ) ) ;
69
66
70
67
this . _logger . debug ( 'patching express.Application.use' ) ;
71
- shimmer . wrap ( this . _moduleExports . application , 'use' , this . _getAppUsePatch . bind ( this ) ) ;
68
+ shimmer . wrap (
69
+ this . _moduleExports . application ,
70
+ 'use' ,
71
+ this . _getAppUsePatch . bind ( this )
72
+ ) ;
72
73
73
74
return this . _moduleExports ;
74
75
}
75
76
76
77
/**
77
78
* Get the patch for Router.route function
78
- * @param original
79
+ * @param original
79
80
*/
80
- private _getRoutePatch ( original : ( path : PathParams ) => express . IRoute ) {
81
- const plugin = this
81
+ private _getRoutePatch ( original : ( path : PathParams ) => express . IRoute ) {
82
+ const plugin = this ;
82
83
return function route_trace (
83
84
this : ExpressRouter ,
84
85
...args : Parameters < typeof original >
85
86
) {
86
87
const route = original . apply ( this , args ) ;
87
88
const layer = this . stack [ this . stack . length - 1 ] as ExpressLayer ;
88
- plugin . _applyPatch ( layer , typeof args [ 0 ] === 'string' ? args [ 0 ] : undefined ) ;
89
+ plugin . _applyPatch (
90
+ layer ,
91
+ typeof args [ 0 ] === 'string' ? args [ 0 ] : undefined
92
+ ) ;
89
93
return route ;
90
94
} ;
91
95
}
92
96
93
97
/**
94
98
* Get the patch for Router.use function
95
- * @param original
99
+ * @param original
96
100
*/
97
- private _getRouterUsePatch ( original : express . IRouterHandler < express . Router > & express . IRouterMatcher < express . Router > ) {
98
- const plugin = this
101
+ private _getRouterUsePatch (
102
+ original : express . IRouterHandler < express . Router > &
103
+ express . IRouterMatcher < express . Router >
104
+ ) {
105
+ const plugin = this ;
99
106
return function use (
100
107
this : express . Application ,
101
108
...args : Parameters < typeof original >
102
109
) {
103
110
const route = original . apply ( this , args ) ;
104
111
const layer = this . stack [ this . stack . length - 1 ] as ExpressLayer ;
105
- plugin . _applyPatch ( layer , typeof args [ 0 ] === 'string' ? args [ 0 ] : undefined ) ;
112
+ plugin . _applyPatch (
113
+ layer ,
114
+ typeof args [ 0 ] === 'string' ? args [ 0 ] : undefined
115
+ ) ;
106
116
return route ;
107
117
// tslint:disable-next-line:no-any
108
- } as any
118
+ } as any ;
109
119
}
110
120
111
121
/**
112
122
* Get the patch for Application.use function
113
- * @param original
123
+ * @param original
114
124
*/
115
- private _getAppUsePatch ( original : core . ApplicationRequestHandler < express . Application > ) {
116
- const plugin = this
125
+ private _getAppUsePatch (
126
+ original : core . ApplicationRequestHandler < express . Application >
127
+ ) {
128
+ const plugin = this ;
117
129
return function use (
118
130
this : { _router : ExpressRouter } ,
119
131
...args : Parameters < typeof original >
@@ -157,8 +169,8 @@ export class ExpressPlugin extends BasePlugin<typeof express> {
157
169
[ AttributeNames . COMPONENT ] : plugin . _COMPONENT ,
158
170
[ AttributeNames . HTTP_ROUTE ] : route . length > 0 ? route : undefined ,
159
171
} ;
160
- const metadata = getLayerMetadata ( layer , layerPath )
161
-
172
+ const metadata = getLayerMetadata ( layer , layerPath ) ;
173
+
162
174
const span = plugin . _tracer . startSpan ( metadata . name , {
163
175
parent : plugin . _tracer . getCurrentSpan ( ) ,
164
176
attributes : Object . assign ( attributes , metadata . attributes ) ,
0 commit comments