Skip to content

Commit

Permalink
fix(sdk-logs): avoid map attribute set when count limit exceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunnoH committed Oct 9, 2023
1 parent c320c98 commit 93a4e2c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
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
24 changes: 22 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,34 @@ 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.foo149, undefined);
});
});
Expand Down

0 comments on commit 93a4e2c

Please sign in to comment.