Skip to content

Commit

Permalink
Prefer originalUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
myrotvorets-team committed Oct 5, 2023
1 parent 5a9c4d2 commit 67315ed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
14 changes: 6 additions & 8 deletions lib/callbacks.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import type { HttpCustomAttributeFunction } from '@opentelemetry/instrumentation
import type { EndHook } from '@opentelemetry/instrumentation-fs';

export const http_applyCustomAttributesOnSpan: HttpCustomAttributeFunction = (span, request) => {
if (
'kind' in span &&
span.kind === SpanKind.SERVER &&
'url' in request &&
typeof request.url === 'string' &&
typeof request.method === 'string'
) {
span.updateName(`${request.method} ${request.url}`);
if ('kind' in span && span.kind === SpanKind.SERVER && typeof request.method === 'string') {
if ('originalUrl' in request && typeof request.originalUrl === 'string') {
span.updateName(`${request.method} ${request.originalUrl}`);
} else if ('url' in request && typeof request.url === 'string') {
span.updateName(`${request.method} ${request.url}`);
}
}
};

Expand Down
11 changes: 5 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
"@opentelemetry/sdk-node": "^0.43.0",
"@opentelemetry/sdk-trace-base": "^1.0.1"
},
"overrides": {
"ansi-color": "npm:@myrotvorets/ansi-color@^0.2.2"
},
"repository": {
"type": "git",
"url": "git+https://github.com/myrotvorets/opentelemetry-configurator.git"
Expand Down
1 change: 0 additions & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ sonar.sources=lib
sonar.tests=test
sonar.testExecutionReportPaths=test-report.xml
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.eslint.reportPaths=eslint-report.json
sonar.sourceEncoding=UTF-8
22 changes: 22 additions & 0 deletions test/callbacks.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,27 @@ describe('callbacks', function () {

expect(span).to.have.property('name', expectedSpanName);
});

it('should prefer originalUrl', function () {
const request = new IncomingMessage(new Socket());
const response = new ServerResponse(request);
const url = '/some/url';
const originalUrl = '/some/original/url';
request.url = url;
(request as IncomingMessage & Record<'originalUrl', string>).originalUrl = originalUrl;
request.method = 'GET';
const expectedSpanName = `${request.method} ${originalUrl}`;
const tracer = provider.getTracer('test');
const span = tracer.startActiveSpan('some span', { kind: SpanKind.SERVER }, (span) => {
try {
http_applyCustomAttributesOnSpan(span, request, response);
return span;
} finally {
span.end();
}
});

expect(span).to.have.property('name', expectedSpanName);
});
});
});

0 comments on commit 67315ed

Please sign in to comment.