Skip to content

Commit 5aabcc7

Browse files
t2t2vmarchaud
andauthoredJul 17, 2021
feat(opentelemetry-web): capture decodedBodySize / http.response_content_length_uncompressed (#2343)
Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
1 parent 2355717 commit 5aabcc7

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed
 

‎packages/opentelemetry-web/src/enums/PerformanceTimingNames.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
export enum PerformanceTimingNames {
1818
CONNECT_END = 'connectEnd',
1919
CONNECT_START = 'connectStart',
20+
DECODED_BODY_SIZE = 'decodedBodySize',
2021
DOM_COMPLETE = 'domComplete',
2122
DOM_CONTENT_LOADED_EVENT_END = 'domContentLoadedEventEnd',
2223
DOM_CONTENT_LOADED_EVENT_START = 'domContentLoadedEventStart',

‎packages/opentelemetry-web/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { PerformanceTimingNames } from './enums/PerformanceTimingNames';
1818
export type PerformanceEntries = {
1919
[PerformanceTimingNames.CONNECT_END]?: number;
2020
[PerformanceTimingNames.CONNECT_START]?: number;
21+
[PerformanceTimingNames.DECODED_BODY_SIZE]?: number;
2122
[PerformanceTimingNames.DOM_COMPLETE]?: number;
2223
[PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_END]?: number;
2324
[PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_START]?: number;

‎packages/opentelemetry-web/src/utils.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,19 @@ export function addSpanNetworkEvents(
8787
addSpanNetworkEvent(span, PTN.REQUEST_START, resource);
8888
addSpanNetworkEvent(span, PTN.RESPONSE_START, resource);
8989
addSpanNetworkEvent(span, PTN.RESPONSE_END, resource);
90-
const contentLength = resource[PTN.ENCODED_BODY_SIZE];
91-
if (contentLength !== undefined) {
90+
const encodedLength = resource[PTN.ENCODED_BODY_SIZE];
91+
if (encodedLength !== undefined) {
9292
span.setAttribute(
9393
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH,
94-
contentLength
94+
encodedLength
95+
);
96+
}
97+
const decodedLength = resource[PTN.DECODED_BODY_SIZE];
98+
// Spec: Not set if transport encoding not used (in which case encoded and decoded sizes match)
99+
if (decodedLength !== undefined && encodedLength !== decodedLength) {
100+
span.setAttribute(
101+
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED,
102+
decodedLength
95103
);
96104
}
97105
}

‎packages/opentelemetry-web/test/utils.test.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,34 @@ describe('utils', () => {
146146
[PTN.REQUEST_START]: 123,
147147
[PTN.RESPONSE_START]: 123,
148148
[PTN.RESPONSE_END]: 123,
149-
[PTN.ENCODED_BODY_SIZE]: 123,
149+
[PTN.DECODED_BODY_SIZE]: 123,
150+
[PTN.ENCODED_BODY_SIZE]: 61,
150151
} as PerformanceEntries;
151152

152153
assert.strictEqual(addEventSpy.callCount, 0);
153154

154155
addSpanNetworkEvents(span, entries);
155156

156157
assert.strictEqual(addEventSpy.callCount, 9);
158+
assert.strictEqual(setAttributeSpy.callCount, 2);
159+
});
160+
it('should only include encoded size when content encoding is being used', () => {
161+
const addEventSpy = sinon.spy();
162+
const setAttributeSpy = sinon.spy();
163+
const span = ({
164+
addEvent: addEventSpy,
165+
setAttribute: setAttributeSpy,
166+
} as unknown) as tracing.Span;
167+
const entries = {
168+
[PTN.DECODED_BODY_SIZE]: 123,
169+
[PTN.ENCODED_BODY_SIZE]: 123,
170+
} as PerformanceEntries;
171+
172+
assert.strictEqual(setAttributeSpy.callCount, 0);
173+
174+
addSpanNetworkEvents(span, entries);
175+
176+
assert.strictEqual(addEventSpy.callCount, 0);
157177
assert.strictEqual(setAttributeSpy.callCount, 1);
158178
});
159179
});

0 commit comments

Comments
 (0)
Please sign in to comment.