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(sdk-logs): make dropping attribute print message #4614

1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All notable changes to experimental packages in this project will be documented
### :rocket: (Enhancement)

* feat(otlp-transformer): consolidate scope/resource creation in transformer [#4600](https://github.com/open-telemetry/opentelemetry-js/pull/4600)
* feat(sdk-logs): print message when attributes are dropped due to attribute count limit [#4614](https://github.com/open-telemetry/opentelemetry-js/pull/4614) @HyunnoH

### :bug: (Bug Fix)

Expand Down
4 changes: 4 additions & 0 deletions experimental/packages/sdk-logs/src/LogRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export class LogRecord implements ReadableLogRecord {
this._logRecordLimits.attributeCountLimit &&
!Object.prototype.hasOwnProperty.call(this.attributes, key)
) {
// This logic is to create drop message at most once per LogRecord to prevent excessive logging.
if (this.droppedAttributesCount === 1) {
HyunnoH marked this conversation as resolved.
Show resolved Hide resolved
api.diag.warn('Dropping extra attributes.');
}
return this;
}
if (isAttributeValue(value)) {
Expand Down
67 changes: 43 additions & 24 deletions experimental/packages/sdk-logs/test/common/LogRecord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,32 +177,31 @@ describe('LogRecord', () => {

describe('when logRecordLimits options set', () => {
describe('when "attributeCountLimit" option defined', () => {
const { logRecord } = setup({ attributeCountLimit: 100 });
for (let i = 0; i < 150; 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}`;
it('should remove / drop all remaining values after the number of values exceeds this limit', () => {
const { logRecord } = setup({ attributeCountLimit: 100 });
for (let i = 0; i < 150; 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);
}
logRecord.setAttribute(`foo${i}`, attributeValue);
}

it('should remove / drop all remaining values after the number of values exceeds this limit', () => {
const { attributes, droppedAttributesCount } = logRecord;
assert.strictEqual(Object.keys(attributes).length, 100);
assert.strictEqual(attributes.foo0, 'bar0');
Expand All @@ -212,6 +211,26 @@ describe('LogRecord', () => {
assert.strictEqual(attributes.foo149, undefined);
assert.strictEqual(droppedAttributesCount, 50);
});

it('should not print message when there are no dropped attributes', () => {
const warnStub = sinon.spy(diag, 'warn');
const { logRecord } = setup({ attributeCountLimit: 10 });
for (let i = 0; i < 7; i++) {
logRecord.setAttribute(`foo${i}`, `bar${i}`);
}
sinon.assert.callCount(warnStub, 0);
warnStub.restore();
});

it('should print message only once when attribute(s) are dropped', () => {
const warnStub = sinon.spy(diag, 'warn');
const { logRecord } = setup({ attributeCountLimit: 5 });
for (let i = 0; i < 7; i++) {
logRecord.setAttribute(`foo${i}`, `bar${i}`);
}
sinon.assert.callCount(warnStub, 1);
warnStub.restore();
});
});

describe('when "attributeValueLengthLimit" option defined', () => {
Expand Down