Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit aeb5fd4

Browse files
authored
Instrumentation/HTTP: Handle incoming requests with long request url … (#515)
* Instrumentation/HTTP: Handle incoming requests with long request url path * Fix review comment
1 parent 727b59c commit aeb5fd4

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/opencensus-instrumentation-http/src/http.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const UNLIMITED_PROPAGATION_MD = {
3434
tagTtl: TagTtl.UNLIMITED_PROPAGATION
3535
};
3636

37+
const TAG_VALUE_MAX_LENGTH = 255;
38+
3739
/** Http instrumentation plugin for Opencensus */
3840
export class HttpPlugin extends BasePlugin {
3941
/**
@@ -228,7 +230,10 @@ export class HttpPlugin extends BasePlugin {
228230
rootSpan.addAttribute(
229231
HttpPlugin.ATTRIBUTE_HTTP_ROUTE, requestUrl.path || '');
230232
tags.set(
231-
stats.HTTP_SERVER_ROUTE, {value: requestUrl.path || ''},
233+
stats.HTTP_SERVER_ROUTE, {
234+
value: (requestUrl.path ||
235+
'').substring(0, TAG_VALUE_MAX_LENGTH)
236+
},
232237
UNLIMITED_PROPAGATION_MD);
233238
}
234239
if (userAgent) {

packages/opencensus-instrumentation-http/test/test-http.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,35 @@ describe('HttpPlugin', () => {
421421
});
422422
});
423423

424+
it('should handle incoming requests with long request url path',
425+
async () => {
426+
const testPath = '/test&code=' +
427+
'a'.repeat(300);
428+
const options = {
429+
host: 'localhost',
430+
path: testPath,
431+
port: serverPort,
432+
headers: {'User-Agent': 'Android'}
433+
};
434+
shimmer.unwrap(http, 'get');
435+
shimmer.unwrap(http, 'request');
436+
nock.enableNetConnect();
437+
438+
assert.strictEqual(spanVerifier.endedSpans.length, 0);
439+
440+
await httpRequest.get(options).then((result) => {
441+
assert.strictEqual(spanVerifier.endedSpans.length, 1);
442+
assert.ok(spanVerifier.endedSpans[0].name.indexOf(testPath) >= 0);
443+
const [span] = spanVerifier.endedSpans;
444+
assertSpanAttributes(
445+
span, 200, 'GET', 'localhost', testPath, 'Android');
446+
assertServerStats(
447+
testExporter, 200, 'GET',
448+
'/test&code=' +
449+
'a'.repeat(244));
450+
});
451+
});
452+
424453
for (const ignored of ['string', 'function', 'regexp']) {
425454
it(`should not trace ignored requests with type ${ignored}`, async () => {
426455
const testPath = `/ignored/${ignored}`;

0 commit comments

Comments
 (0)