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

chore: require value in setAttribute #1851

Merged
merged 8 commits into from
Jan 30, 2021
2 changes: 1 addition & 1 deletion packages/opentelemetry-api/src/trace/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface Span {
* @param value the value for this attribute. Setting a value null or
* undefined is invalid and will result in undefined behavior.
*/
setAttribute(key: string, value?: AttributeValue): this;
setAttribute(key: string, value: AttributeValue): this;

/**
* Sets attributes to the span.
Expand Down
4 changes: 3 additions & 1 deletion packages/opentelemetry-instrumentation-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ export class FetchInstrumentation extends InstrumentationBase<
): void {
const parsedUrl = web.parseUrl(response.url);
span.setAttribute(HttpAttribute.HTTP_STATUS_CODE, response.status);
span.setAttribute(HttpAttribute.HTTP_STATUS_TEXT, response.statusText);
if (response.statusText != undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: !== undefined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may end up stuck in a loop #1851 (comment)

span.setAttribute(HttpAttribute.HTTP_STATUS_TEXT, response.statusText);
}
span.setAttribute(HttpAttribute.HTTP_HOST, parsedUrl.host);
span.setAttribute(
HttpAttribute.HTTP_SCHEME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
_addFinalSpanAttributes(span: api.Span, xhrMem: XhrMem, spanUrl?: string) {
if (typeof spanUrl === 'string') {
const parsedUrl = parseUrl(spanUrl);

span.setAttribute(HttpAttribute.HTTP_STATUS_CODE, xhrMem.status);
span.setAttribute(HttpAttribute.HTTP_STATUS_TEXT, xhrMem.statusText);
if (xhrMem.status !== undefined) {
span.setAttribute(HttpAttribute.HTTP_STATUS_CODE, xhrMem.status);
}
if (xhrMem.statusText !== undefined) {
span.setAttribute(HttpAttribute.HTTP_STATUS_TEXT, xhrMem.statusText);
}
span.setAttribute(HttpAttribute.HTTP_HOST, parsedUrl.host);
span.setAttribute(
HttpAttribute.HTTP_SCHEME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,29 +789,34 @@ describe('xhr', () => {
);
assert.strictEqual(
attributes[keys[2]],
0,
`attributes ${HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH} is wrong`
);
assert.strictEqual(
attributes[keys[3]],
400,
`attributes ${HttpAttribute.HTTP_STATUS_CODE} is wrong`
);
assert.strictEqual(
attributes[keys[3]],
attributes[keys[4]],
'Bad Request',
`attributes ${HttpAttribute.HTTP_STATUS_TEXT} is wrong`
);
assert.strictEqual(
attributes[keys[4]],
attributes[keys[5]],
'raw.githubusercontent.com',
`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
5 changes: 3 additions & 2 deletions packages/opentelemetry-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ 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]) {
const contentLength = resource[PTN.ENCODED_BODY_SIZE];
if (contentLength !== undefined) {
span.setAttribute(
HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH,
resource[PTN.ENCODED_BODY_SIZE]
contentLength
);
}
}
Expand Down