From 41bf22f86c3ff8efb3207f8924dacacfdbd11269 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Tue, 26 Jan 2021 10:13:32 -0300 Subject: [PATCH 1/6] 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/6] 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/6] 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/6] 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/6] 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/6] 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'