Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/ExpressPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ 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) => {
if (err) {
Expand Down
19 changes: 13 additions & 6 deletions src/plugins/HttpPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -150,10 +154,13 @@ 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'
? `[${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;
Expand Down
10 changes: 9 additions & 1 deletion tests/plugins/axios/expected.data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ 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
value: '200'
- key: http.status.msg
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion tests/plugins/express/expected.data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -64,7 +68,7 @@ segmentItems:
endTime: gt 0
componentId: 4002
spanType: Entry
peer: server:5000
peer: not null
skipAnalysis: false
- serviceName: client
segmentSize: 1
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/plugins/http/expected.data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down