fix(plugin-http): improve formatting for url attribute (#317)
closes #316 Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca>
This commit is contained in:
parent
cc8a27d307
commit
cee42d8a97
|
@ -179,13 +179,17 @@ export class HttpPlugin extends BasePlugin<Http> {
|
||||||
response.req && response.req.method
|
response.req && response.req.method
|
||||||
? response.req.method.toUpperCase()
|
? response.req.method.toUpperCase()
|
||||||
: 'GET';
|
: 'GET';
|
||||||
const headers = options.headers;
|
const headers = options.headers || {};
|
||||||
const userAgent = headers ? headers['user-agent'] : null;
|
const userAgent = headers['user-agent'];
|
||||||
|
|
||||||
const host = options.hostname || options.host || 'localhost';
|
const host = options.hostname || options.host || 'localhost';
|
||||||
|
|
||||||
const attributes: Attributes = {
|
const attributes: Attributes = {
|
||||||
[AttributeNames.HTTP_URL]: `${options.protocol}//${options.hostname}${options.path}`,
|
[AttributeNames.HTTP_URL]: Utils.getAbsoluteUrl(
|
||||||
|
options,
|
||||||
|
headers,
|
||||||
|
`${HttpPlugin.component}:`
|
||||||
|
),
|
||||||
[AttributeNames.HTTP_HOSTNAME]: host,
|
[AttributeNames.HTTP_HOSTNAME]: host,
|
||||||
[AttributeNames.HTTP_METHOD]: method,
|
[AttributeNames.HTTP_METHOD]: method,
|
||||||
[AttributeNames.HTTP_PATH]: options.path || '/',
|
[AttributeNames.HTTP_PATH]: options.path || '/',
|
||||||
|
@ -286,9 +290,10 @@ export class HttpPlugin extends BasePlugin<Http> {
|
||||||
const userAgent = headers['user-agent'];
|
const userAgent = headers['user-agent'];
|
||||||
|
|
||||||
const attributes: Attributes = {
|
const attributes: Attributes = {
|
||||||
[AttributeNames.HTTP_URL]: Utils.getUrlFromIncomingRequest(
|
[AttributeNames.HTTP_URL]: Utils.getAbsoluteUrl(
|
||||||
requestUrl,
|
requestUrl,
|
||||||
headers
|
headers,
|
||||||
|
`${HttpPlugin.component}:`
|
||||||
),
|
),
|
||||||
[AttributeNames.HTTP_HOSTNAME]: hostname,
|
[AttributeNames.HTTP_HOSTNAME]: hostname,
|
||||||
[AttributeNames.HTTP_METHOD]: method,
|
[AttributeNames.HTTP_METHOD]: method,
|
||||||
|
|
|
@ -20,8 +20,9 @@ import {
|
||||||
IncomingMessage,
|
IncomingMessage,
|
||||||
ClientRequest,
|
ClientRequest,
|
||||||
IncomingHttpHeaders,
|
IncomingHttpHeaders,
|
||||||
|
OutgoingHttpHeaders,
|
||||||
} from 'http';
|
} from 'http';
|
||||||
import { IgnoreMatcher, Err } from './types';
|
import { IgnoreMatcher, Err, ParsedRequestOptions } from './types';
|
||||||
import { AttributeNames } from './enums/AttributeNames';
|
import { AttributeNames } from './enums/AttributeNames';
|
||||||
import * as url from 'url';
|
import * as url from 'url';
|
||||||
|
|
||||||
|
@ -30,20 +31,30 @@ import * as url from 'url';
|
||||||
*/
|
*/
|
||||||
export class Utils {
|
export class Utils {
|
||||||
/**
|
/**
|
||||||
* return an absolute url
|
* Get an absolute url
|
||||||
*/
|
*/
|
||||||
static getUrlFromIncomingRequest(
|
static getAbsoluteUrl(
|
||||||
requestUrl: url.UrlWithStringQuery | null,
|
requestUrl: ParsedRequestOptions | null,
|
||||||
headers: IncomingHttpHeaders
|
headers: IncomingHttpHeaders | OutgoingHttpHeaders,
|
||||||
|
fallbackProtocol = 'http:'
|
||||||
): string {
|
): string {
|
||||||
if (!requestUrl) {
|
const reqUrlObject = requestUrl || {};
|
||||||
return `http://${headers.host || 'localhost'}/`;
|
const protocol = reqUrlObject.protocol || fallbackProtocol;
|
||||||
|
const port = (reqUrlObject.port || '').toString();
|
||||||
|
const path = reqUrlObject.path || '/';
|
||||||
|
let host =
|
||||||
|
headers.host || reqUrlObject.hostname || headers.host || 'localhost';
|
||||||
|
|
||||||
|
// if there is no port in host and there is a port
|
||||||
|
// it should be displayed if it's not 80 and 443 (default ports)
|
||||||
|
if (
|
||||||
|
(host as string).indexOf(':') === -1 &&
|
||||||
|
(port && port !== '80' && port !== '443')
|
||||||
|
) {
|
||||||
|
host += `:${port}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return requestUrl.href && requestUrl.href.startsWith('http')
|
return `${protocol}//${host}${path}`;
|
||||||
? `${requestUrl.protocol}//${requestUrl.hostname}${requestUrl.path}`
|
|
||||||
: `${requestUrl.protocol || 'http:'}//${headers.host ||
|
|
||||||
'localhost'}${requestUrl.path || '/'}`;
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Parse status code from HTTP response.
|
* Parse status code from HTTP response.
|
||||||
|
|
|
@ -165,21 +165,28 @@ describe('Utils', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getUrlFromIncomingRequest()', () => {
|
describe('getAbsoluteUrl()', () => {
|
||||||
it('should return absolute url with localhost', () => {
|
it('should return absolute url with localhost', () => {
|
||||||
const path = '/test/1';
|
const path = '/test/1';
|
||||||
const result = Utils.getUrlFromIncomingRequest(url.parse(path), {});
|
const result = Utils.getAbsoluteUrl(url.parse(path), {});
|
||||||
assert.strictEqual(result, `http://localhost${path}`);
|
assert.strictEqual(result, `http://localhost${path}`);
|
||||||
});
|
});
|
||||||
it('should return absolute url', () => {
|
it('should return absolute url', () => {
|
||||||
const absUrl = 'http://www.google/test/1?query=1';
|
const absUrl = 'http://www.google/test/1?query=1';
|
||||||
const result = Utils.getUrlFromIncomingRequest(url.parse(absUrl), {});
|
const result = Utils.getAbsoluteUrl(url.parse(absUrl), {});
|
||||||
assert.strictEqual(result, absUrl);
|
assert.strictEqual(result, absUrl);
|
||||||
});
|
});
|
||||||
it('should return default url', () => {
|
it('should return default url', () => {
|
||||||
const result = Utils.getUrlFromIncomingRequest(null, {});
|
const result = Utils.getAbsoluteUrl(null, {});
|
||||||
assert.strictEqual(result, 'http://localhost/');
|
assert.strictEqual(result, 'http://localhost/');
|
||||||
});
|
});
|
||||||
|
it("{ path: '/helloworld', port: 8080 } should return http://localhost:8080/helloworld", () => {
|
||||||
|
const result = Utils.getAbsoluteUrl(
|
||||||
|
{ path: '/helloworld', port: 8080 },
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
assert.strictEqual(result, 'http://localhost:8080/helloworld');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
describe('setSpanOnError()', () => {
|
describe('setSpanOnError()', () => {
|
||||||
it('should call span methods when we get an error event', done => {
|
it('should call span methods when we get an error event', done => {
|
||||||
|
|
Loading…
Reference in New Issue