Skip to content

Commit

Permalink
feat(opentelemetry-web): capture decodedBodySize / http.response_cont…
Browse files Browse the repository at this point in the history
…ent_length_uncompressed (#2343)

Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
  • Loading branch information
t2t2 and vmarchaud committed Jul 17, 2021
1 parent 2355717 commit 5aabcc7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
Expand Up @@ -17,6 +17,7 @@
export enum PerformanceTimingNames {
CONNECT_END = 'connectEnd',
CONNECT_START = 'connectStart',
DECODED_BODY_SIZE = 'decodedBodySize',
DOM_COMPLETE = 'domComplete',
DOM_CONTENT_LOADED_EVENT_END = 'domContentLoadedEventEnd',
DOM_CONTENT_LOADED_EVENT_START = 'domContentLoadedEventStart',
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-web/src/types.ts
Expand Up @@ -18,6 +18,7 @@ import { PerformanceTimingNames } from './enums/PerformanceTimingNames';
export type PerformanceEntries = {
[PerformanceTimingNames.CONNECT_END]?: number;
[PerformanceTimingNames.CONNECT_START]?: number;
[PerformanceTimingNames.DECODED_BODY_SIZE]?: number;
[PerformanceTimingNames.DOM_COMPLETE]?: number;
[PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_END]?: number;
[PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_START]?: number;
Expand Down
14 changes: 11 additions & 3 deletions packages/opentelemetry-web/src/utils.ts
Expand Up @@ -87,11 +87,19 @@ export function addSpanNetworkEvents(
addSpanNetworkEvent(span, PTN.REQUEST_START, resource);
addSpanNetworkEvent(span, PTN.RESPONSE_START, resource);
addSpanNetworkEvent(span, PTN.RESPONSE_END, resource);
const contentLength = resource[PTN.ENCODED_BODY_SIZE];
if (contentLength !== undefined) {
const encodedLength = resource[PTN.ENCODED_BODY_SIZE];
if (encodedLength !== undefined) {
span.setAttribute(
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH,
contentLength
encodedLength
);
}
const decodedLength = resource[PTN.DECODED_BODY_SIZE];
// Spec: Not set if transport encoding not used (in which case encoded and decoded sizes match)
if (decodedLength !== undefined && encodedLength !== decodedLength) {
span.setAttribute(
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED,
decodedLength
);
}
}
Expand Down
22 changes: 21 additions & 1 deletion packages/opentelemetry-web/test/utils.test.ts
Expand Up @@ -146,14 +146,34 @@ describe('utils', () => {
[PTN.REQUEST_START]: 123,
[PTN.RESPONSE_START]: 123,
[PTN.RESPONSE_END]: 123,
[PTN.ENCODED_BODY_SIZE]: 123,
[PTN.DECODED_BODY_SIZE]: 123,
[PTN.ENCODED_BODY_SIZE]: 61,
} as PerformanceEntries;

assert.strictEqual(addEventSpy.callCount, 0);

addSpanNetworkEvents(span, entries);

assert.strictEqual(addEventSpy.callCount, 9);
assert.strictEqual(setAttributeSpy.callCount, 2);
});
it('should only include encoded size when content encoding is being used', () => {
const addEventSpy = sinon.spy();
const setAttributeSpy = sinon.spy();
const span = ({
addEvent: addEventSpy,
setAttribute: setAttributeSpy,
} as unknown) as tracing.Span;
const entries = {
[PTN.DECODED_BODY_SIZE]: 123,
[PTN.ENCODED_BODY_SIZE]: 123,
} as PerformanceEntries;

assert.strictEqual(setAttributeSpy.callCount, 0);

addSpanNetworkEvents(span, entries);

assert.strictEqual(addEventSpy.callCount, 0);
assert.strictEqual(setAttributeSpy.callCount, 1);
});
});
Expand Down

0 comments on commit 5aabcc7

Please sign in to comment.