From 41bf22f86c3ff8efb3207f8924dacacfdbd11269 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Tue, 26 Jan 2021 10:13:32 -0300 Subject: [PATCH 1/9] 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; From d732e3d8ffb401e53b3bac71fff5619ab8cc69ae Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Tue, 26 Jan 2021 10:33:22 -0300 Subject: [PATCH 2/9] tests --- tests/plugins/axios/expected.data.yaml | 8 ++++++++ tests/plugins/express/expected.data.yaml | 8 ++++++++ tests/plugins/http/expected.data.yaml | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/tests/plugins/axios/expected.data.yaml b/tests/plugins/axios/expected.data.yaml index 3ecd711..15638bf 100644 --- a/tests/plugins/axios/expected.data.yaml +++ b/tests/plugins/axios/expected.data.yaml @@ -35,6 +35,8 @@ segmentItems: tags: - key: http.url value: httpbin.org:80/json + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg @@ -53,6 +55,8 @@ segmentItems: tags: - key: http.url value: server:5000/axios + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg @@ -79,6 +83,8 @@ segmentItems: tags: - key: http.url value: server:5000/axios + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg @@ -97,6 +103,8 @@ segmentItems: tags: - key: http.url value: localhost:5001/axios + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg diff --git a/tests/plugins/express/expected.data.yaml b/tests/plugins/express/expected.data.yaml index 009c233..3a628f4 100644 --- a/tests/plugins/express/expected.data.yaml +++ b/tests/plugins/express/expected.data.yaml @@ -35,6 +35,8 @@ segmentItems: tags: - key: http.url value: httpbin.org/json + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg @@ -47,6 +49,8 @@ segmentItems: tags: - key: http.url value: server:5000/express + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg @@ -79,6 +83,8 @@ segmentItems: tags: - key: http.url value: server:5000/express + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg @@ -97,6 +103,8 @@ segmentItems: tags: - key: http.url value: localhost:5001/express + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg diff --git a/tests/plugins/http/expected.data.yaml b/tests/plugins/http/expected.data.yaml index b498ff7..b7e241d 100644 --- a/tests/plugins/http/expected.data.yaml +++ b/tests/plugins/http/expected.data.yaml @@ -35,6 +35,8 @@ segmentItems: tags: - key: http.url value: server:5000/test + - key: http.method + value: GET - key: http.status.code value: '200' refs: @@ -60,6 +62,8 @@ segmentItems: tags: - key: http.url value: httpbin.org/json + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg @@ -83,6 +87,8 @@ segmentItems: tags: - key: http.url value: localhost:5001/test + - key: http.method + value: GET - key: http.status.code value: '200' - operationName: /test @@ -99,6 +105,8 @@ segmentItems: tags: - key: http.url value: server:5000/test + - key: http.method + value: GET - key: http.status.code value: '200' - key: http.status.msg From 6222d9d69cd0e96c8bdf3a6f6c36a6dcb61faa0b Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Tue, 26 Jan 2021 13:47:03 -0300 Subject: [PATCH 3/9] fixed peer for http and express --- src/plugins/ExpressPlugin.ts | 8 ++++++-- src/plugins/HttpPlugin.ts | 11 ++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/plugins/ExpressPlugin.ts b/src/plugins/ExpressPlugin.ts index 387ec06..cd94ca3 100644 --- a/src/plugins/ExpressPlugin.ts +++ b/src/plugins/ExpressPlugin.ts @@ -71,8 +71,12 @@ class ExpressPlugin implements SwPlugin { try { span.layer = SpanLayer.HTTP; span.component = Component.EXPRESS; - span.peer = req.headers.host || ''; - span.tag(Tag.httpURL(span.peer + req.url)); + span.peer = + (typeof req.headers['x-forwarded-for'] === 'string' && req.headers['x-forwarded-for'].split(',').shift()) + || (req.connection.remoteFamily === 'IPv6' + ? `[${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)); const ret = _handle.call(this, req, res, (err: Error) => { diff --git a/src/plugins/HttpPlugin.ts b/src/plugins/HttpPlugin.ts index 85c6b63..4dd66cf 100644 --- a/src/plugins/HttpPlugin.ts +++ b/src/plugins/HttpPlugin.ts @@ -154,9 +154,14 @@ class HttpPlugin implements SwPlugin { try { span.component = Component.HTTP_SERVER; span.layer = SpanLayer.HTTP; - span.peer = req.connection.remoteFamily === 'IPv6' - ? `[${req.connection.remoteAddress}]:${req.connection.remotePort}` - : `${req.connection.remoteAddress}:${req.connection.remotePort}`; + // span.peer = req.connection.remoteFamily === 'IPv6' + // ? `[${req.connection.remoteAddress}]:${req.connection.remotePort}` + // : `${req.connection.remoteAddress}:${req.connection.remotePort}`; + span.peer = + (typeof req.headers['x-forwarded-for'] === 'string' && req.headers['x-forwarded-for'].split(',').shift()) + || (req.connection.remoteFamily === 'IPv6' + ? `[${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)); From b3b39d386487bd43e9b5a22fa43d234e9278f721 Mon Sep 17 00:00:00 2001 From: Zhenxu Ke Date: Wed, 27 Jan 2021 20:14:17 +0800 Subject: [PATCH 4/9] Update expected.data.yaml --- tests/plugins/express/expected.data.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugins/express/expected.data.yaml b/tests/plugins/express/expected.data.yaml index 3a628f4..23ba437 100644 --- a/tests/plugins/express/expected.data.yaml +++ b/tests/plugins/express/expected.data.yaml @@ -68,7 +68,7 @@ segmentItems: endTime: gt 0 componentId: 4002 spanType: Entry - peer: server:5000 + peer: not null skipAnalysis: false - serviceName: client segmentSize: 1 From 7982b339a3eeac79d97b2469a22470c4c91bfe53 Mon Sep 17 00:00:00 2001 From: Zhenxu Ke Date: Wed, 27 Jan 2021 20:14:31 +0800 Subject: [PATCH 5/9] Update tests/plugins/axios/expected.data.yaml --- tests/plugins/axios/expected.data.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugins/axios/expected.data.yaml b/tests/plugins/axios/expected.data.yaml index 15638bf..f5c66d3 100644 --- a/tests/plugins/axios/expected.data.yaml +++ b/tests/plugins/axios/expected.data.yaml @@ -34,7 +34,7 @@ segmentItems: skipAnalysis: false tags: - key: http.url - value: httpbin.org:80/json + value: httpbin.org/json - key: http.method value: GET - key: http.status.code From 56e58c581cdbae4695f32654299d276d615ee8b7 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Wed, 27 Jan 2021 09:36:17 -0300 Subject: [PATCH 6/9] removed commented-out code --- src/plugins/HttpPlugin.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/plugins/HttpPlugin.ts b/src/plugins/HttpPlugin.ts index 4dd66cf..8c1e090 100644 --- a/src/plugins/HttpPlugin.ts +++ b/src/plugins/HttpPlugin.ts @@ -154,9 +154,6 @@ class HttpPlugin implements SwPlugin { try { span.component = Component.HTTP_SERVER; span.layer = SpanLayer.HTTP; - // span.peer = req.connection.remoteFamily === 'IPv6' - // ? `[${req.connection.remoteAddress}]:${req.connection.remotePort}` - // : `${req.connection.remoteAddress}:${req.connection.remotePort}`; span.peer = (typeof req.headers['x-forwarded-for'] === 'string' && req.headers['x-forwarded-for'].split(',').shift()) || (req.connection.remoteFamily === 'IPv6' From 648cdc42a8528d008b1eb3cd63aa5b06a4ecf234 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Wed, 27 Jan 2021 10:47:53 -0300 Subject: [PATCH 7/9] added protocol to server http.url --- src/plugins/ExpressPlugin.ts | 3 ++- src/plugins/HttpPlugin.ts | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/plugins/ExpressPlugin.ts b/src/plugins/ExpressPlugin.ts index cd94ca3..5a550e3 100644 --- a/src/plugins/ExpressPlugin.ts +++ b/src/plugins/ExpressPlugin.ts @@ -76,7 +76,6 @@ class ExpressPlugin implements SwPlugin { || (req.connection.remoteFamily === 'IPv6' ? `[${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)); const ret = _handle.call(this, req, res, (err: Error) => { @@ -95,6 +94,8 @@ class ExpressPlugin implements SwPlugin { } catch (e) { stopIfNotStopped(e); throw e; + } finally { // req.protocol is only possibly available after call to _handle() + span.tag(Tag.httpURL(((req as any).protocol ? (req as any).protocol + '://' : '') + (req.headers.host || '') + req.url)); } }; } diff --git a/src/plugins/HttpPlugin.ts b/src/plugins/HttpPlugin.ts index 8c1e090..e324b00 100644 --- a/src/plugins/HttpPlugin.ts +++ b/src/plugins/HttpPlugin.ts @@ -38,9 +38,9 @@ class HttpPlugin implements SwPlugin { const https = require('https'); this.interceptClientRequest(http); - this.interceptServerRequest(http); + this.interceptServerRequest(http, 'http'); this.interceptClientRequest(https); - this.interceptServerRequest(https); + this.interceptServerRequest(https, 'https'); } private interceptClientRequest(module: any) { @@ -120,7 +120,7 @@ class HttpPlugin implements SwPlugin { }; } - private interceptServerRequest(module: any) { + private interceptServerRequest(module: any, protocol: string) { /// TODO? full event protocol support not currently implemented (prependListener(), removeListener(), etc...) const _addListener = module.Server.prototype.addListener; @@ -159,7 +159,7 @@ class HttpPlugin implements SwPlugin { || (req.connection.remoteFamily === 'IPv6' ? `[${req.connection.remoteAddress}]:${req.connection.remotePort}` : `${req.connection.remoteAddress}:${req.connection.remotePort}`); - span.tag(Tag.httpURL((req.headers.host || '') + req.url)); + span.tag(Tag.httpURL(protocol + '://' + (req.headers.host || '') + req.url)); span.tag(Tag.httpMethod(req.method)); let ret = handler.call(this, req, res, ...reqArgs); From c50f7fc81a0baca4ee8b7a3355dbb0a7fbb51fc6 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Wed, 27 Jan 2021 10:58:38 -0300 Subject: [PATCH 8/9] forgot tests again --- tests/plugins/axios/expected.data.yaml | 4 ++-- tests/plugins/express/expected.data.yaml | 4 ++-- tests/plugins/http/expected.data.yaml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/plugins/axios/expected.data.yaml b/tests/plugins/axios/expected.data.yaml index f5c66d3..ddb2fc9 100644 --- a/tests/plugins/axios/expected.data.yaml +++ b/tests/plugins/axios/expected.data.yaml @@ -54,7 +54,7 @@ segmentItems: skipAnalysis: false tags: - key: http.url - value: server:5000/axios + value: http://server:5000/axios - key: http.method value: GET - key: http.status.code @@ -102,7 +102,7 @@ segmentItems: spanLayer: Http tags: - key: http.url - value: localhost:5001/axios + value: http://localhost:5001/axios - key: http.method value: GET - key: http.status.code diff --git a/tests/plugins/express/expected.data.yaml b/tests/plugins/express/expected.data.yaml index 23ba437..f892683 100644 --- a/tests/plugins/express/expected.data.yaml +++ b/tests/plugins/express/expected.data.yaml @@ -48,7 +48,7 @@ segmentItems: spanLayer: Http tags: - key: http.url - value: server:5000/express + value: http://server:5000/express - key: http.method value: GET - key: http.status.code @@ -102,7 +102,7 @@ segmentItems: spanLayer: Http tags: - key: http.url - value: localhost:5001/express + value: http://localhost:5001/express - key: http.method value: GET - key: http.status.code diff --git a/tests/plugins/http/expected.data.yaml b/tests/plugins/http/expected.data.yaml index b7e241d..486b6ce 100644 --- a/tests/plugins/http/expected.data.yaml +++ b/tests/plugins/http/expected.data.yaml @@ -34,7 +34,7 @@ segmentItems: skipAnalysis: false tags: - key: http.url - value: server:5000/test + value: http://server:5000/test - key: http.method value: GET - key: http.status.code @@ -86,7 +86,7 @@ segmentItems: skipAnalysis: false tags: - key: http.url - value: localhost:5001/test + value: http://localhost:5001/test - key: http.method value: GET - key: http.status.code From 6553106fc9cbf217e49158644baed1cb1e4c727d Mon Sep 17 00:00:00 2001 From: Zhenxu Ke Date: Wed, 27 Jan 2021 23:42:07 +0800 Subject: [PATCH 9/9] Update expected.data.yaml --- tests/plugins/express/expected.data.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/plugins/express/expected.data.yaml b/tests/plugins/express/expected.data.yaml index f892683..240a481 100644 --- a/tests/plugins/express/expected.data.yaml +++ b/tests/plugins/express/expected.data.yaml @@ -47,10 +47,10 @@ segmentItems: spanId: 0 spanLayer: Http tags: - - key: http.url - value: http://server:5000/express - key: http.method value: GET + - key: http.url + value: http://server:5000/express - key: http.status.code value: '200' - key: http.status.msg @@ -101,10 +101,10 @@ segmentItems: spanId: 0 spanLayer: Http tags: - - key: http.url - value: http://localhost:5001/express - key: http.method value: GET + - key: http.url + value: http://localhost:5001/express - key: http.status.code value: '200' - key: http.status.msg