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-sdk-trace-base): implemented option to limit length of values of attributes #2418

Conversation

banothurameshnaik
Copy link
Contributor

@banothurameshnaik banothurameshnaik commented Aug 18, 2021

Which problem is this PR solving?

Short description of the changes

…th of values of attributes

Signed-off-by: Banothu Ramesh Naik <[email protected]>
@banothurameshnaik banothurameshnaik requested a review from a team August 18, 2021 04:31
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Aug 18, 2021

CLA Signed

The committers are authorized under a signed CLA.

@codecov
Copy link

codecov bot commented Aug 18, 2021

Codecov Report

Merging #2418 (835e9c5) into main (5bb9fcc) will increase coverage by 0.53%.
The diff coverage is 100.00%.

❗ Current head 835e9c5 differs from pull request most recent head 97e619b. Consider uploading reports for the commit 97e619b to get more accurate results

@@            Coverage Diff             @@
##             main    #2418      +/-   ##
==========================================
+ Coverage   92.14%   92.68%   +0.53%     
==========================================
  Files         120      137      +17     
  Lines        3998     4990     +992     
  Branches      844     1054     +210     
==========================================
+ Hits         3684     4625     +941     
- Misses        314      365      +51     
Impacted Files Coverage Δ
...ckages/opentelemetry-core/src/utils/environment.ts 95.83% <ø> (ø)
...ackages/opentelemetry-sdk-trace-base/src/config.ts 86.84% <ø> (ø)
packages/opentelemetry-sdk-trace-base/src/Span.ts 100.00% <100.00%> (+1.92%) ⬆️
...emetry-api-metrics/src/platform/node/globalThis.ts 0.00% <0.00%> (-100.00%) ⬇️
...entelemetry-sdk-trace-web/src/WebTracerProvider.ts 100.00% <0.00%> (ø)
...ry-exporter-collector/src/CollectorExporterBase.ts 92.15% <0.00%> (ø)
...lemetry-exporter-collector/src/transformMetrics.ts 95.71% <0.00%> (ø)
...emetry-instrumentation-xml-http-request/src/xhr.ts 97.58% <0.00%> (ø)
...mentation-xml-http-request/src/enums/EventNames.ts 100.00% <0.00%> (ø)
...s/opentelemetry-instrumentation-fetch/src/fetch.ts 96.91% <0.00%> (ø)
... and 12 more

packages/opentelemetry-sdk-trace-base/src/Span.ts Outdated Show resolved Hide resolved
* @returns truncated attribute value if required, otherwise same value
*/
private _truncateToSize(value?: SpanAttributeValue): SpanAttributeValue | undefined {
const limit = this._spanLimits.attributeValueLengthLimit!
Copy link
Member

Choose a reason for hiding this comment

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

Why is the ! assertion needed here?

packages/opentelemetry-sdk-trace-base/src/Span.ts Outdated Show resolved Hide resolved

// Check type of values
// Allowed types are string, array of strings
if (value == null || (typeof value != 'string' && !Array.isArray(value))) {
Copy link
Member

Choose a reason for hiding this comment

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

This will already fall through to the return at the end

Suggested change
if (value == null || (typeof value != 'string' && !Array.isArray(value))) {
if (value == null) {

@dyladan dyladan changed the title chore(opentelemetry-sdk-trace-base): implemented option to limit length of values of attributes feat(opentelemetry-sdk-trace-base): implemented option to limit length of values of attributes Aug 18, 2021
}

// Array of strings
if (this._isArrayOfStrings(value)) {
Copy link
Member

@obecny obecny Aug 18, 2021

Choose a reason for hiding this comment

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

spec doesn't allow to have array of different type. Imagine the situation when you have 1k of attributes and only one is a number and the rest will still be string. From a user perspective I would assume that all 999 attributes should be truncated and the rest not. Having this both things in mind I would get rid of function _isArrayOfStrings and would simply do.

return (value as []).map(val => {
  typeof val  === 'string' ? this._truncateToLimitUtil(val, limit)) : val;
}

This way code will not break and the attributes that can be limited will be limited.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I implemented the same way at the first glance.
but later added array of strings check since spec here specially says apply these truncate only for strings and array of strings otherwise ignore.

Let me change back as you mentioned.

const limit = this._spanLimits.attributeValueLengthLimit;
// Check limit
// undefined limit means do not truncate
if (typeof limit != 'number' || limit <= 0) {
Copy link
Member

Choose a reason for hiding this comment

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

please use strict typing

Suggested change
if (typeof limit != 'number' || limit <= 0) {
if (typeof limit !== 'number' || limit <= 0) {

or even do

if (!(limit > 0))

?

Copy link
Member

Choose a reason for hiding this comment

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

OT: Why is lint not complaining about this?

Copy link
Member

Choose a reason for hiding this comment

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

that's a good question, maybe some rule is missing, as some time ago we updated lint, wondering if that was on purpose then or it was missed.

attributeCountLimit: 100,
eventCountLimit: 128,
linkCountLimit: 128,
});
});

it('should construct an instance with customized attributeValueLengthLimit span limits', () => {
Copy link
Member

@obecny obecny Aug 18, 2021

Choose a reason for hiding this comment

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

I'm having difficulties with understanding this description and what is happening here, consider this

Suggested change
it('should construct an instance with customized attributeValueLengthLimit span limits', () => {
// earlier
describe('"spanLimits"', ()=> {
...
describe('when "attributeValueLengthLimit" is defined', ()=> {
it('should truncate value which length exceeds this limit', () => {
});
});
describe('when "attributeCountLimit" is defined', ()=> {
it('should remove / drop all remaining values after the number of values exceeds this limit, () => {
});
}
// etc. etc. just use more natural language to describe it with 'when' -> 'then' approach
....

WDYT ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree. It's adds better readability also 👍

I followed existing code test cases pattern since its my first contribution to open source

I will make changes here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@obecny I made test cases as you mentioned.

Copy link
Member

@obecny obecny left a comment

Choose a reason for hiding this comment

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

lgtm, thx for changing tests it is much readable now :)

span.setAttribute('array<string>', ['str1', 'str2']);
span.setAttribute('array<number>', [1, 2]);
span.setAttribute('array<bool>', [true, false]);
it('should able to overwrite attributes', () => {
Copy link
Member

Choose a reason for hiding this comment

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

?

Suggested change
it('should able to overwrite attributes', () => {
it('should be able to overwrite attributes', () => {

SpanKind.CLIENT
);
describe('setAttributes', () => {
it('should able to set multiple attributes', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
it('should able to set multiple attributes', () => {
it('should be able to set multiple attributes', () => {

@banothurameshnaik
Copy link
Contributor Author

@dyladan @Flarna
I made changes as you mentioned in comments.

Can you please have a look.

* @param value Attribute value
* @returns truncated attribute value if required, otherwise same value
*/
private _truncateToSize(value?: SpanAttributeValue): SpanAttributeValue | undefined {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private _truncateToSize(value?: SpanAttributeValue): SpanAttributeValue | undefined {
private _truncateToSize(value: SpanAttributeValue): SpanAttributeValue {

@obecny obecny added the enhancement New feature or request label Aug 23, 2021
@obecny obecny merged commit 78a78c0 into open-telemetry:main Aug 23, 2021
@dyladan dyladan mentioned this pull request Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Spec: implement an option to limit length of values of attributes and metric values
5 participants