Skip to content

Commit 41bf22f

Browse files
committed
added http.method to plugins
1 parent aa7013f commit 41bf22f

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/Tag.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,33 @@ export interface Tag {
2424
}
2525

2626
export default {
27-
httpStatusCode: (val: string | number | undefined): Tag => {
27+
httpURLKey: 'http.url',
28+
httpMethodKey: 'http.method', // TODO: maybe find a better place to put these?
29+
30+
httpStatusCode(val: string | number | undefined): Tag {
2831
return {
2932
key: 'http.status.code',
3033
overridable: true,
3134
val: `${val}`,
3235
} as Tag;
3336
},
34-
httpStatusMsg: (val: string | undefined): Tag => {
37+
httpStatusMsg(val: string | undefined): Tag {
3538
return {
3639
key: 'http.status.msg',
3740
overridable: true,
3841
val: `${val}`,
3942
} as Tag;
4043
},
41-
httpURL: (val: string | undefined): Tag => {
44+
httpURL(val: string | undefined): Tag {
45+
return {
46+
key: this.httpURLKey,
47+
overridable: true,
48+
val: `${val}`,
49+
} as Tag;
50+
},
51+
httpMethod(val: string | undefined): Tag {
4252
return {
43-
key: 'http.url',
53+
key: this.httpMethodKey,
4454
overridable: true,
4555
val: `${val}`,
4656
} as Tag;

src/plugins/ExpressPlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class ExpressPlugin implements SwPlugin {
7373
span.component = Component.EXPRESS;
7474
span.peer = req.headers.host || '';
7575
span.tag(Tag.httpURL(span.peer + req.url));
76+
span.tag(Tag.httpMethod(req.method));
7677

7778
const ret = _handle.call(this, req, res, (err: Error) => {
7879
if (err) {

src/plugins/HttpPlugin.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ class HttpPlugin implements SwPlugin {
5858
host: (url.host || url.hostname || 'unknown') + ':' + (url.port || 80),
5959
pathname: url.path || '/',
6060
};
61-
const operation = pathname.replace(/\?.*$/g, '');
61+
const httpMethod = arguments[url instanceof URL || typeof url === 'string' ? 1 : 0]?.method || 'GET';
62+
const httpURL = host + pathname;
63+
const operation = pathname.replace(/\?.*$/g, '');
6264

6365
let stopped = 0; // compensating if request aborted right after creation 'close' is not emitted
6466
const stopIfNotStopped = () => !stopped++ ? span.stop() : null; // make sure we stop only once
@@ -72,10 +74,12 @@ class HttpPlugin implements SwPlugin {
7274
if (!span.peer) {
7375
span.peer = host;
7476
}
75-
const httpURL = host + pathname;
76-
if (!span.hasTag(httpURL)) {
77+
if (!span.hasTag(Tag.httpURLKey)) { // only set if a higher level plugin with more info did not already set
7778
span.tag(Tag.httpURL(httpURL));
7879
}
80+
if (!span.hasTag(Tag.httpMethodKey)) {
81+
span.tag(Tag.httpMethod(httpMethod));
82+
}
7983

8084
const req: ClientRequest = _request.apply(this, arguments);
8185

@@ -154,6 +158,7 @@ class HttpPlugin implements SwPlugin {
154158
? `[${req.connection.remoteAddress}]:${req.connection.remotePort}`
155159
: `${req.connection.remoteAddress}:${req.connection.remotePort}`;
156160
span.tag(Tag.httpURL((req.headers.host || '') + req.url));
161+
span.tag(Tag.httpMethod(req.method));
157162

158163
let ret = handler.call(this, req, res, ...reqArgs);
159164
const type = ret?.constructor;

0 commit comments

Comments
 (0)