Skip to content

Commit

Permalink
added http.method to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-pytel committed Jan 26, 2021
1 parent aa7013f commit 41bf22f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
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
1 change: 1 addition & 0 deletions src/plugins/ExpressPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 8 additions & 3 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 @@ -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;
Expand Down

0 comments on commit 41bf22f

Please sign in to comment.