Skip to content

Commit

Permalink
feat(opentelemetry-js): linting
Browse files Browse the repository at this point in the history
  • Loading branch information
nijotz committed Oct 26, 2020
1 parent 97c8799 commit baf1b68
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 70 deletions.
35 changes: 20 additions & 15 deletions packages/opentelemetry-plugin-http/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,26 +230,32 @@ export const setContentLengthAttributes = (
) => {
let isCompressed = false;

if (headers['content-length'] === undefined)
return;
if (headers['content-length'] === undefined) return;

if (headers['content-encoding'] && headers['content-encoding'] !== 'identity') {
isCompressed = true
};
if (
headers['content-encoding'] &&
headers['content-encoding'] !== 'identity'
) {
isCompressed = true;
}

if (isCompressed) {
if (isRequest) {
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH] = headers['content-length']
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH] =
headers['content-length'];
} else {
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH] = headers['content-length']
};
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH] =
headers['content-length'];
}
} else {
if (isRequest) {
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] = headers['content-length']
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] =
headers['content-length'];
} else {
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] = headers['content-length']
};
};
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] =
headers['content-length'];
}
}
};

/**
Expand Down Expand Up @@ -394,14 +400,13 @@ export const getOutgoingRequestAttributesOnResponse = (
const { statusCode, statusMessage, httpVersion, headers, socket } = response;
const { remoteAddress, remotePort } = socket;


const attributes: Attributes = {
[GeneralAttribute.NET_PEER_IP]: remoteAddress,
[GeneralAttribute.NET_PEER_PORT]: remotePort,
[HttpAttribute.HTTP_HOST]: `${options.hostname}:${remotePort}`,
};

setContentLengthAttributes(headers, attributes, false)
setContentLengthAttributes(headers, attributes, false);

if (statusCode) {
attributes[HttpAttribute.HTTP_STATUS_CODE] = statusCode;
Expand Down Expand Up @@ -463,7 +468,7 @@ export const getIncomingRequestAttributes = (
attributes[HttpAttribute.HTTP_USER_AGENT] = userAgent;
}

setContentLengthAttributes(headers, attributes, true)
setContentLengthAttributes(headers, attributes, true);

const httpKindAttributes = getAttributesFromHttpKind(httpVersion);
return Object.assign(attributes, httpKindAttributes);
Expand Down
183 changes: 136 additions & 47 deletions packages/opentelemetry-plugin-http/test/functionals/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Attributes, CanonicalCode, SpanKind, TraceFlags } from '@opentelemetry/api';
import {
Attributes,
CanonicalCode,
SpanKind,
TraceFlags,
} from '@opentelemetry/api';
import { NoopLogger } from '@opentelemetry/core';
import { BasicTracerProvider, Span } from '@opentelemetry/tracing';
import { HttpAttribute } from '@opentelemetry/semantic-conventions';
Expand Down Expand Up @@ -314,110 +319,194 @@ describe('Utility', () => {
it('should set response content-length uncompressed attribute with no content-encoding header', () => {
const attributes: Attributes = {};

let headers: http.IncomingHttpHeaders = {};
const headers: http.IncomingHttpHeaders = {};

headers["content-length"] = '1200';
headers['content-length'] = '1200';

utils.setContentLengthAttributes(headers, attributes, false);

assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED], '1200');
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH], undefined);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED],
'1200'
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH],
undefined
);
});

it('should set response content-length uncompressed attribute with "identity" content-encoding header', () => {
const attributes: Attributes = {};

let headers: http.IncomingHttpHeaders = {};
const headers: http.IncomingHttpHeaders = {};

headers["content-length"] = '1200';
headers["content-encoding"] = 'identity';
headers['content-length'] = '1200';
headers['content-encoding'] = 'identity';

utils.setContentLengthAttributes(headers, attributes, false);

assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED], '1200');
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH], undefined);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED],
'1200'
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH],
undefined
);
});

it('should set response content-length compressed attribute with "gzip" content-encoding header', () => {
const attributes: Attributes = {};

let headers: http.IncomingHttpHeaders = {};
const headers: http.IncomingHttpHeaders = {};

headers["content-length"] = '1200';
headers["content-encoding"] = 'gzip';
headers['content-length'] = '1200';
headers['content-encoding'] = 'gzip';

utils.setContentLengthAttributes(headers, attributes, false);

assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH], '1200');
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH], undefined);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH],
'1200'
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH],
undefined
);
});

it('should set request content-length uncompressed attribute with no content-encoding header', () => {
const attributes: Attributes = {};

let headers: http.OutgoingHttpHeaders = {};
const headers: http.OutgoingHttpHeaders = {};

headers["content-length"] = '1200';
headers['content-length'] = '1200';

utils.setContentLengthAttributes(headers, attributes, true);

assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED], '1200');
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH], undefined);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
'1200'
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH],
undefined
);
});

it('should set request content-length uncompressed attribute with "identity" content-encoding header', () => {
const attributes: Attributes = {};

let headers: http.OutgoingHttpHeaders = {};
const headers: http.OutgoingHttpHeaders = {};

headers["content-length"] = '1200';
headers["content-encoding"] = 'identity';
headers['content-length'] = '1200';
headers['content-encoding'] = 'identity';

utils.setContentLengthAttributes(headers, attributes, true);

assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED], '1200');
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH], undefined);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
'1200'
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH],
undefined
);
});

it('should set request content-length compressed attribute with "gzip" content-encoding header', () => {
const attributes: Attributes = {};

let headers: http.OutgoingHttpHeaders = {};
const headers: http.OutgoingHttpHeaders = {};

headers["content-length"] = '1200';
headers["content-encoding"] = 'gzip';
headers['content-length'] = '1200';
headers['content-encoding'] = 'gzip';

utils.setContentLengthAttributes(headers, attributes, true);

assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH], '1200');
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH], undefined);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH],
'1200'
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH],
undefined
);
});

it('should set no attributes with no content-length header', () => {
const attributes: Attributes = {};

let headers: http.OutgoingHttpHeaders = {};
const headers: http.OutgoingHttpHeaders = {};

headers["content-encoding"] = 'gzip';
headers['content-encoding'] = 'gzip';

utils.setContentLengthAttributes(headers, attributes, true);

assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED], undefined);
assert.strictEqual(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH], undefined);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
assert.strictEqual(
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH],
undefined
);
});
});
});
22 changes: 16 additions & 6 deletions packages/opentelemetry-plugin-http/test/utils/assertSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,21 @@ export const assertSpan = (

if (span.kind === SpanKind.CLIENT) {
if (validations.resHeaders['content-length']) {
const contentLength = validations.resHeaders['content-length']
const contentLength = validations.resHeaders['content-length'];

if (validations.resHeaders['content-encoding'] && validations.resHeaders['content-encoding'] != 'identity') {
if (
validations.resHeaders['content-encoding'] &&
validations.resHeaders['content-encoding'] != 'identity'
) {
assert.strictEqual(
span.attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH],
contentLength
);
} else {
assert.strictEqual(
span.attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED],
span.attributes[
HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED
],
contentLength
);
}
Expand Down Expand Up @@ -123,16 +128,21 @@ export const assertSpan = (
}
if (span.kind === SpanKind.SERVER) {
if (validations.reqHeaders && validations.reqHeaders['content-length']) {
const contentLength = validations.reqHeaders['content-length']
const contentLength = validations.reqHeaders['content-length'];

if (validations.reqHeaders['content-encoding'] && validations.reqHeaders['content-encoding'] != 'identity') {
if (
validations.reqHeaders['content-encoding'] &&
validations.reqHeaders['content-encoding'] != 'identity'
) {
assert.strictEqual(
span.attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH],
contentLength
);
} else {
assert.strictEqual(
span.attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
span.attributes[
HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED
],
contentLength
);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/opentelemetry-semantic-conventions/src/trace/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ export const HttpAttribute = {
HTTP_CLIENT_IP: 'http.client_ip',
HTTP_SCHEME: 'http.scheme',
HTTP_RESPONSE_CONTENT_LENGTH: 'http.response_content_length',
HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED: 'http.response_content_length_uncompressed',
HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED:
'http.response_content_length_uncompressed',
HTTP_REQUEST_CONTENT_LENGTH: 'http.request_content_length',
HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED: 'http.request_content_length_uncompressed',
HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED:
'http.request_content_length_uncompressed',

// NOT ON OFFICIAL SPEC
HTTP_ERROR_NAME: 'http.error_name',
Expand Down

0 comments on commit baf1b68

Please sign in to comment.