Skip to content

Commit cee42d8

Browse files
OlivierAlbertinimayurkale22
authored andcommitted
fix(plugin-http): improve formatting for url attribute (open-telemetry#317)
closes open-telemetry#316 Signed-off-by: Olivier Albertini <[email protected]>
1 parent cc8a27d commit cee42d8

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

packages/opentelemetry-plugin-http/src/http.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,17 @@ export class HttpPlugin extends BasePlugin<Http> {
179179
response.req && response.req.method
180180
? response.req.method.toUpperCase()
181181
: 'GET';
182-
const headers = options.headers;
183-
const userAgent = headers ? headers['user-agent'] : null;
182+
const headers = options.headers || {};
183+
const userAgent = headers['user-agent'];
184184

185185
const host = options.hostname || options.host || 'localhost';
186186

187187
const attributes: Attributes = {
188-
[AttributeNames.HTTP_URL]: `${options.protocol}//${options.hostname}${options.path}`,
188+
[AttributeNames.HTTP_URL]: Utils.getAbsoluteUrl(
189+
options,
190+
headers,
191+
`${HttpPlugin.component}:`
192+
),
189193
[AttributeNames.HTTP_HOSTNAME]: host,
190194
[AttributeNames.HTTP_METHOD]: method,
191195
[AttributeNames.HTTP_PATH]: options.path || '/',
@@ -286,9 +290,10 @@ export class HttpPlugin extends BasePlugin<Http> {
286290
const userAgent = headers['user-agent'];
287291

288292
const attributes: Attributes = {
289-
[AttributeNames.HTTP_URL]: Utils.getUrlFromIncomingRequest(
293+
[AttributeNames.HTTP_URL]: Utils.getAbsoluteUrl(
290294
requestUrl,
291-
headers
295+
headers,
296+
`${HttpPlugin.component}:`
292297
),
293298
[AttributeNames.HTTP_HOSTNAME]: hostname,
294299
[AttributeNames.HTTP_METHOD]: method,

packages/opentelemetry-plugin-http/src/utils.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import {
2020
IncomingMessage,
2121
ClientRequest,
2222
IncomingHttpHeaders,
23+
OutgoingHttpHeaders,
2324
} from 'http';
24-
import { IgnoreMatcher, Err } from './types';
25+
import { IgnoreMatcher, Err, ParsedRequestOptions } from './types';
2526
import { AttributeNames } from './enums/AttributeNames';
2627
import * as url from 'url';
2728

@@ -30,20 +31,30 @@ import * as url from 'url';
3031
*/
3132
export class Utils {
3233
/**
33-
* return an absolute url
34+
* Get an absolute url
3435
*/
35-
static getUrlFromIncomingRequest(
36-
requestUrl: url.UrlWithStringQuery | null,
37-
headers: IncomingHttpHeaders
36+
static getAbsoluteUrl(
37+
requestUrl: ParsedRequestOptions | null,
38+
headers: IncomingHttpHeaders | OutgoingHttpHeaders,
39+
fallbackProtocol = 'http:'
3840
): string {
39-
if (!requestUrl) {
40-
return `http://${headers.host || 'localhost'}/`;
41+
const reqUrlObject = requestUrl || {};
42+
const protocol = reqUrlObject.protocol || fallbackProtocol;
43+
const port = (reqUrlObject.port || '').toString();
44+
const path = reqUrlObject.path || '/';
45+
let host =
46+
headers.host || reqUrlObject.hostname || headers.host || 'localhost';
47+
48+
// if there is no port in host and there is a port
49+
// it should be displayed if it's not 80 and 443 (default ports)
50+
if (
51+
(host as string).indexOf(':') === -1 &&
52+
(port && port !== '80' && port !== '443')
53+
) {
54+
host += `:${port}`;
4155
}
4256

43-
return requestUrl.href && requestUrl.href.startsWith('http')
44-
? `${requestUrl.protocol}//${requestUrl.hostname}${requestUrl.path}`
45-
: `${requestUrl.protocol || 'http:'}//${headers.host ||
46-
'localhost'}${requestUrl.path || '/'}`;
57+
return `${protocol}//${host}${path}`;
4758
}
4859
/**
4960
* Parse status code from HTTP response.

packages/opentelemetry-plugin-http/test/functionals/utils.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,28 @@ describe('Utils', () => {
165165
});
166166
});
167167

168-
describe('getUrlFromIncomingRequest()', () => {
168+
describe('getAbsoluteUrl()', () => {
169169
it('should return absolute url with localhost', () => {
170170
const path = '/test/1';
171-
const result = Utils.getUrlFromIncomingRequest(url.parse(path), {});
171+
const result = Utils.getAbsoluteUrl(url.parse(path), {});
172172
assert.strictEqual(result, `http://localhost${path}`);
173173
});
174174
it('should return absolute url', () => {
175175
const absUrl = 'http://www.google/test/1?query=1';
176-
const result = Utils.getUrlFromIncomingRequest(url.parse(absUrl), {});
176+
const result = Utils.getAbsoluteUrl(url.parse(absUrl), {});
177177
assert.strictEqual(result, absUrl);
178178
});
179179
it('should return default url', () => {
180-
const result = Utils.getUrlFromIncomingRequest(null, {});
180+
const result = Utils.getAbsoluteUrl(null, {});
181181
assert.strictEqual(result, 'http://localhost/');
182182
});
183+
it("{ path: '/helloworld', port: 8080 } should return http://localhost:8080/helloworld", () => {
184+
const result = Utils.getAbsoluteUrl(
185+
{ path: '/helloworld', port: 8080 },
186+
{}
187+
);
188+
assert.strictEqual(result, 'http://localhost:8080/helloworld');
189+
});
183190
});
184191
describe('setSpanOnError()', () => {
185192
it('should call span methods when we get an error event', done => {

0 commit comments

Comments
 (0)