From 41bf22f86c3ff8efb3207f8924dacacfdbd11269 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Tue, 26 Jan 2021 10:13:32 -0300 Subject: [PATCH] added http.method to plugins --- src/Tag.ts | 18 ++++++++++++++---- src/plugins/ExpressPlugin.ts | 1 + src/plugins/HttpPlugin.ts | 11 ++++++++--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Tag.ts b/src/Tag.ts index 229fea4..95e8ab3 100644 --- a/src/Tag.ts +++ b/src/Tag.ts @@ -24,23 +24,33 @@ export interface Tag { } export default { - httpStatusCode: (val: string | number | undefined): Tag => { + httpURLKey: 'http.url', + httpMethodKey: 'http.method', // TODO: maybe find a better place to put these? + + httpStatusCode(val: string | number | undefined): Tag { return { key: 'http.status.code', overridable: true, val: `${val}`, } as Tag; }, - httpStatusMsg: (val: string | undefined): Tag => { + httpStatusMsg(val: string | undefined): Tag { return { key: 'http.status.msg', overridable: true, val: `${val}`, } as Tag; }, - httpURL: (val: string | undefined): Tag => { + httpURL(val: string | undefined): Tag { + return { + key: this.httpURLKey, + overridable: true, + val: `${val}`, + } as Tag; + }, + httpMethod(val: string | undefined): Tag { return { - key: 'http.url', + key: this.httpMethodKey, overridable: true, val: `${val}`, } as Tag; diff --git a/src/plugins/ExpressPlugin.ts b/src/plugins/ExpressPlugin.ts index 9d9a3e8..387ec06 100644 --- a/src/plugins/ExpressPlugin.ts +++ b/src/plugins/ExpressPlugin.ts @@ -73,6 +73,7 @@ class ExpressPlugin implements SwPlugin { span.component = Component.EXPRESS; span.peer = req.headers.host || ''; span.tag(Tag.httpURL(span.peer + req.url)); + span.tag(Tag.httpMethod(req.method)); const ret = _handle.call(this, req, res, (err: Error) => { if (err) { diff --git a/src/plugins/HttpPlugin.ts b/src/plugins/HttpPlugin.ts index ca01811..85c6b63 100644 --- a/src/plugins/HttpPlugin.ts +++ b/src/plugins/HttpPlugin.ts @@ -58,7 +58,9 @@ class HttpPlugin implements SwPlugin { host: (url.host || url.hostname || 'unknown') + ':' + (url.port || 80), pathname: url.path || '/', }; - const operation = pathname.replace(/\?.*$/g, ''); + const httpMethod = arguments[url instanceof URL || typeof url === 'string' ? 1 : 0]?.method || 'GET'; + const httpURL = host + pathname; + const operation = pathname.replace(/\?.*$/g, ''); let stopped = 0; // compensating if request aborted right after creation 'close' is not emitted const stopIfNotStopped = () => !stopped++ ? span.stop() : null; // make sure we stop only once @@ -72,10 +74,12 @@ class HttpPlugin implements SwPlugin { if (!span.peer) { span.peer = host; } - const httpURL = host + pathname; - if (!span.hasTag(httpURL)) { + if (!span.hasTag(Tag.httpURLKey)) { // only set if a higher level plugin with more info did not already set span.tag(Tag.httpURL(httpURL)); } + if (!span.hasTag(Tag.httpMethodKey)) { + span.tag(Tag.httpMethod(httpMethod)); + } const req: ClientRequest = _request.apply(this, arguments); @@ -154,6 +158,7 @@ class HttpPlugin implements SwPlugin { ? `[${req.connection.remoteAddress}]:${req.connection.remotePort}` : `${req.connection.remoteAddress}:${req.connection.remotePort}`; span.tag(Tag.httpURL((req.headers.host || '') + req.url)); + span.tag(Tag.httpMethod(req.method)); let ret = handler.call(this, req, res, ...reqArgs); const type = ret?.constructor;