Skip to content

Commit 9da4bdf

Browse files
authored
Metric Bug Fix: Strip out Query and Fragment in extractAPIPathValue Logic (#36)
*Issue #, if available:* - `httpTarget` can be of the form `/pathname?q=query#fragment`. - extractAPIPathValue(httpTarget) will include the query or the fragment parts, which we do not want. - This isn't caught with the existing unit test (`/users/1/pet?query#fragment`) because the query+fragment is removed after the extractAPIPathValue Logic splits the slashes. *Description of changes:* - In this PR, we remove the query and fragment. Testing: - Unit tests - Observed removal of query and fragment from Console Span Exporter. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 98d5f83 commit 9da4bdf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/aws-span-processing-util.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,21 @@ export class AwsSpanProcessingUtil {
7676
if (httpTarget == null || httpTarget === '') {
7777
return '/';
7878
}
79-
const paths: string[] = httpTarget.split('/');
79+
// Divergence from Java/Python
80+
// https://github.com/open-telemetry/semantic-conventions/blob/4e7c42ee8e4c3a39a899c4c85c64df28cd543f78/docs/attributes-registry/http.md#deprecated-http-attributes
81+
// According to OTel Spec, httpTarget may include query and fragment:
82+
// - `/search?q=OpenTelemetry#SemConv`
83+
// We do NOT want the `?` or `#` parts, so let us strip it out,
84+
// because HTTP (ingress) instrumentation was observed to include the query (`?`) part
85+
// - https://github.com/open-telemetry/opentelemetry-js/blob/b418d36609c371d1fcae46898e9ede6278aca917/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts#L502-L504
86+
// According to RFC Specification, "The path is terminated by the first question mark ("?") or number sign ("#") character, or by the end of the URI."
87+
// - https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
88+
//
89+
// This is a fix that can be applied here since this is the central location for generating API Path Value
90+
// TODO: Possibly contribute fix to upstream for this diff between langauges. However, the current attribute value in JS is according to spec.
91+
//
92+
// Interestingly, according to Spec, Java/Python should be affected, but they are not.
93+
const paths: string[] = httpTarget.split(/[/?#]/);
8094
if (paths.length > 1) {
8195
return '/' + paths[1];
8296
}

aws-distro-opentelemetry-node-autoinstrumentation/test/aws-span-processing-util.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,24 @@ describe('AwsSpanProcessingUtilTest', () => {
171171
expect(pathValue).toEqual('/users');
172172
});
173173

174+
it('testExtractAPIPathValidPathSingleSlash', () => {
175+
let validTarget: string = '/users?query#fragment';
176+
let pathValue: string = AwsSpanProcessingUtil.extractAPIPathValue(validTarget);
177+
expect(pathValue).toEqual('/users');
178+
179+
validTarget = '/users#fragment?fragment_part_2';
180+
pathValue = AwsSpanProcessingUtil.extractAPIPathValue(validTarget);
181+
expect(pathValue).toEqual('/users');
182+
183+
validTarget = '/users?query';
184+
pathValue = AwsSpanProcessingUtil.extractAPIPathValue(validTarget);
185+
expect(pathValue).toEqual('/users');
186+
187+
validTarget = '/users#fragment';
188+
pathValue = AwsSpanProcessingUtil.extractAPIPathValue(validTarget);
189+
expect(pathValue).toEqual('/users');
190+
});
191+
174192
it('testIsKeyPresentKeyPresent', () => {
175193
attributesMock[SEMATTRS_HTTP_TARGET] = 'target';
176194
expect(AwsSpanProcessingUtil.isKeyPresent(spanDataMock, SEMATTRS_HTTP_TARGET)).toBeTruthy();

0 commit comments

Comments
 (0)