Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metric Bug Fix: Strip out Query and Fragment in extractAPIPathValue Logic #36

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,21 @@ export class AwsSpanProcessingUtil {
if (httpTarget == null || httpTarget === '') {
return '/';
}
const paths: string[] = httpTarget.split('/');
// Divergence from Java/Python
// https://github.com/open-telemetry/semantic-conventions/blob/4e7c42ee8e4c3a39a899c4c85c64df28cd543f78/docs/attributes-registry/http.md#deprecated-http-attributes
// According to OTel Spec, httpTarget may include query and fragment:
// - `/search?q=OpenTelemetry#SemConv`
// We do NOT want the `?` or `#` parts, so let us strip it out,
// because HTTP (ingress) instrumentation was observed to include the query (`?`) part
// - https://github.com/open-telemetry/opentelemetry-js/blob/b418d36609c371d1fcae46898e9ede6278aca917/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts#L502-L504
// According to RFC Specification, "The path is terminated by the first question mark ("?") or number sign ("#") character, or by the end of the URI."
// - https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
//
// This is a fix that can be applied here since this is the central location for generating API Path Value
// TODO: Possibly contribute fix to upstream for this diff between langauges. However, the current attribute value in JS is according to spec.
//
// Interestingly, according to Spec, Java/Python should be affected, but they are not.
const paths: string[] = httpTarget.split(/[/?#]/);
if (paths.length > 1) {
return '/' + paths[1];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ describe('AwsSpanProcessingUtilTest', () => {
expect(pathValue).toEqual('/users');
});

it('testExtractAPIPathValidPathSingleSlash', () => {
jj22ee marked this conversation as resolved.
Show resolved Hide resolved
let validTarget: string = '/users?query#fragment';
let pathValue: string = AwsSpanProcessingUtil.extractAPIPathValue(validTarget);
expect(pathValue).toEqual('/users');

validTarget = '/users#fragment?fragment_part_2';
pathValue = AwsSpanProcessingUtil.extractAPIPathValue(validTarget);
expect(pathValue).toEqual('/users');

validTarget = '/users?query';
pathValue = AwsSpanProcessingUtil.extractAPIPathValue(validTarget);
expect(pathValue).toEqual('/users');

validTarget = '/users#fragment';
pathValue = AwsSpanProcessingUtil.extractAPIPathValue(validTarget);
expect(pathValue).toEqual('/users');
});

it('testIsKeyPresentKeyPresent', () => {
attributesMock[SEMATTRS_HTTP_TARGET] = 'target';
expect(AwsSpanProcessingUtil.isKeyPresent(spanDataMock, SEMATTRS_HTTP_TARGET)).toBeTruthy();
Expand Down
Loading