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

feat(opentelemetry-web): capture decodedBodySize / http.response_content_length #1262

Merged
merged 11 commits into from
Jul 6, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export enum AttributeNames {
HTTP_URL = 'http.url',
HTTP_TARGET = 'http.target',
HTTP_USER_AGENT = 'http.user_agent',
HTTP_RESPONSE_CONTENT_LENGTH = 'http.response_content_length',
}
6 changes: 5 additions & 1 deletion packages/opentelemetry-plugin-fetch/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,12 @@ describe('fetch', () => {
attributes[keys[7]] !== '',
`attributes ${AttributeNames.HTTP_USER_AGENT} is not defined`
);
assert.ok(
(attributes[keys[8]] as number) > 0,
`attributes ${AttributeNames.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
);

assert.strictEqual(keys.length, 8, 'number of attributes is wrong');
assert.strictEqual(keys.length, 9, 'number of attributes is wrong');
});

it('span should have correct events', () => {
Expand Down
16 changes: 10 additions & 6 deletions packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,31 +290,35 @@ describe('xhr', () => {
url,
`attributes ${HttpAttribute.HTTP_URL} is wrong`
);
assert.ok(
(attributes[keys[2]] as number) > 0,
'attributes ${HttpAttributes.HTTP_RESPONSE_CONTENT_SIZE} <= 0'
);
assert.strictEqual(
attributes[keys[2]],
attributes[keys[3]],
200,
`attributes ${HttpAttribute.HTTP_STATUS_CODE} is wrong`
);
assert.strictEqual(
attributes[keys[3]],
attributes[keys[4]],
'OK',
`attributes ${HttpAttribute.HTTP_STATUS_TEXT} is wrong`
);
assert.strictEqual(
attributes[keys[4]],
attributes[keys[5]],
parseUrl(url).host,
`attributes ${HttpAttribute.HTTP_HOST} is wrong`
);
assert.ok(
attributes[keys[5]] === 'http' || attributes[keys[5]] === 'https',
attributes[keys[6]] === 'http' || attributes[keys[6]] === 'https',
`attributes ${HttpAttribute.HTTP_SCHEME} is wrong`
);
assert.ok(
attributes[keys[6]] !== '',
attributes[keys[7]] !== '',
`attributes ${HttpAttribute.HTTP_USER_AGENT} is not defined`
);

assert.strictEqual(keys.length, 7, 'number of attributes is wrong');
assert.strictEqual(keys.length, 8, 'number of attributes is wrong');
});

it('span should have correct events', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const HttpAttribute = {
HTTP_SERVER_NAME: 'http.server_name',
HTTP_CLIENT_IP: 'http.client_ip',
HTTP_SCHEME: 'http.scheme',
HTTP_RESPONSE_CONTENT_LENGTH: 'http.response_content_length',

// NOT ON OFFICIAL SPEC
HTTP_ERROR_NAME: 'http.error_name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum PerformanceTimingNames {
DOM_INTERACTIVE = 'domInteractive',
DOMAIN_LOOKUP_END = 'domainLookupEnd',
DOMAIN_LOOKUP_START = 'domainLookupStart',
ENCODED_BODY_SIZE = 'encodedBodySize',
FETCH_START = 'fetchStart',
LOAD_EVENT_END = 'loadEventEnd',
LOAD_EVENT_START = 'loadEventStart',
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type PerformanceEntries = {
[PerformanceTimingNames.DOM_INTERACTIVE]?: number;
[PerformanceTimingNames.DOMAIN_LOOKUP_END]?: number;
[PerformanceTimingNames.DOMAIN_LOOKUP_START]?: number;
[PerformanceTimingNames.ENCODED_BODY_SIZE]?: number;
[PerformanceTimingNames.FETCH_START]?: number;
[PerformanceTimingNames.LOAD_EVENT_END]?: number;
[PerformanceTimingNames.LOAD_EVENT_START]?: number;
Expand Down
6 changes: 6 additions & 0 deletions packages/opentelemetry-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export function addSpanNetworkEvents(
addSpanNetworkEvent(span, PTN.REQUEST_START, resource);
addSpanNetworkEvent(span, PTN.RESPONSE_START, resource);
addSpanNetworkEvent(span, PTN.RESPONSE_END, resource);
if (resource[PTN.ENCODED_BODY_SIZE]) {
span.setAttribute(
'http.response_content_length',
johnbley marked this conversation as resolved.
Show resolved Hide resolved
resource[PTN.ENCODED_BODY_SIZE]
);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/opentelemetry-web/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ describe('utils', () => {
describe('addSpanNetworkEvents', () => {
it('should add all network events to span', () => {
const addEventSpy = sinon.spy();
const setAttributeSpy = sinon.spy();
const span = ({
addEvent: addEventSpy,
setAttribute: setAttributeSpy,
} as unknown) as tracing.Span;
const entries = {
[PTN.FETCH_START]: 123,
Expand All @@ -150,13 +152,15 @@ describe('utils', () => {
[PTN.REQUEST_START]: 123,
[PTN.RESPONSE_START]: 123,
[PTN.RESPONSE_END]: 123,
[PTN.ENCODED_BODY_SIZE]: 123,
} as PerformanceEntries;

assert.strictEqual(addEventSpy.callCount, 0);

addSpanNetworkEvents(span, entries);

assert.strictEqual(addEventSpy.callCount, 9);
assert.strictEqual(setAttributeSpy.callCount, 1);
});
});
describe('addSpanNetworkEvent', () => {
Expand Down