Skip to content

Commit

Permalink
fix(sdk-logs): avoid map attribute set when count limit exceeded (#4195)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
HyunnoH and pichlermarc committed Nov 9, 2023
1 parent 40fde0f commit f5ef8de
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix(sdk-logs): avoid map attribute set when count limit exceeded

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
22 changes: 13 additions & 9 deletions experimental/packages/sdk-logs/src/LogRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ export class LogRecord implements ReadableLogRecord {
if (value === null) {
return this;
}
if (
typeof value === 'object' &&
!Array.isArray(value) &&
Object.keys(value).length > 0
) {
this.attributes[key] = value;
}
if (key.length === 0) {
api.diag.warn(`Invalid attribute key: ${key}`);
return this;
}
if (!isAttributeValue(value)) {
if (
!isAttributeValue(value) &&
!(
typeof value === 'object' &&
!Array.isArray(value) &&
Object.keys(value).length > 0
)
) {
api.diag.warn(`Invalid attribute value set for key: ${key}`);
return this;
}
Expand All @@ -136,7 +136,11 @@ export class LogRecord implements ReadableLogRecord {
) {
return this;
}
this.attributes[key] = this._truncateToSize(value);
if (isAttributeValue(value)) {
this.attributes[key] = this._truncateToSize(value);
} else {
this.attributes[key] = value;
}
return this;
}

Expand Down
26 changes: 24 additions & 2 deletions experimental/packages/sdk-logs/test/common/LogRecord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,36 @@ describe('LogRecord', () => {
describe('when "attributeCountLimit" option defined', () => {
const { logRecord } = setup({ attributeCountLimit: 100 });
for (let i = 0; i < 150; i++) {
logRecord.setAttribute(`foo${i}`, `bar${i}`);
let attributeValue;
switch (i % 3) {
case 0: {
attributeValue = `bar${i}`;
break;
}
case 1: {
attributeValue = [`bar${i}`];
break;
}
case 2: {
attributeValue = {
bar: `bar${i}`,
};
break;
}
default: {
attributeValue = `bar${i}`;
}
}
logRecord.setAttribute(`foo${i}`, attributeValue);
}

it('should remove / drop all remaining values after the number of values exceeds this limit', () => {
const { attributes } = logRecord;
assert.strictEqual(Object.keys(attributes).length, 100);
assert.strictEqual(attributes.foo0, 'bar0');
assert.strictEqual(attributes.foo99, 'bar99');
assert.deepStrictEqual(attributes.foo98, { bar: 'bar98' });
assert.strictEqual(attributes.foo147, undefined);
assert.strictEqual(attributes.foo148, undefined);
assert.strictEqual(attributes.foo149, undefined);
});
});
Expand Down

0 comments on commit f5ef8de

Please sign in to comment.