Skip to content

Commit 3b5eb23

Browse files
HyunnoHpichlermarc
andauthored
feat(sdk-logs): make dropping attribute print message (#4614)
* feat(sdk-logs): make dropping attribute print message * chore: update changelog * chore(sdk-logs): add comment to explain message logic * Update experimental/CHANGELOG.md --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent da02c8d commit 3b5eb23

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to experimental packages in this project will be documented
1616
### :rocket: (Enhancement)
1717

1818
* feat(otlp-transformer): consolidate scope/resource creation in transformer [#4600](https://github.com/open-telemetry/opentelemetry-js/pull/4600)
19+
* 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
1920

2021
### :bug: (Bug Fix)
2122

experimental/packages/sdk-logs/src/LogRecord.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ export class LogRecord implements ReadableLogRecord {
140140
this._logRecordLimits.attributeCountLimit &&
141141
!Object.prototype.hasOwnProperty.call(this.attributes, key)
142142
) {
143+
// This logic is to create drop message at most once per LogRecord to prevent excessive logging.
144+
if (this.droppedAttributesCount === 1) {
145+
api.diag.warn('Dropping extra attributes.');
146+
}
143147
return this;
144148
}
145149
if (isAttributeValue(value)) {

experimental/packages/sdk-logs/test/common/LogRecord.test.ts

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -177,32 +177,31 @@ describe('LogRecord', () => {
177177

178178
describe('when logRecordLimits options set', () => {
179179
describe('when "attributeCountLimit" option defined', () => {
180-
const { logRecord } = setup({ attributeCountLimit: 100 });
181-
for (let i = 0; i < 150; i++) {
182-
let attributeValue;
183-
switch (i % 3) {
184-
case 0: {
185-
attributeValue = `bar${i}`;
186-
break;
187-
}
188-
case 1: {
189-
attributeValue = [`bar${i}`];
190-
break;
191-
}
192-
case 2: {
193-
attributeValue = {
194-
bar: `bar${i}`,
195-
};
196-
break;
197-
}
198-
default: {
199-
attributeValue = `bar${i}`;
180+
it('should remove / drop all remaining values after the number of values exceeds this limit', () => {
181+
const { logRecord } = setup({ attributeCountLimit: 100 });
182+
for (let i = 0; i < 150; i++) {
183+
let attributeValue;
184+
switch (i % 3) {
185+
case 0: {
186+
attributeValue = `bar${i}`;
187+
break;
188+
}
189+
case 1: {
190+
attributeValue = [`bar${i}`];
191+
break;
192+
}
193+
case 2: {
194+
attributeValue = {
195+
bar: `bar${i}`,
196+
};
197+
break;
198+
}
199+
default: {
200+
attributeValue = `bar${i}`;
201+
}
200202
}
203+
logRecord.setAttribute(`foo${i}`, attributeValue);
201204
}
202-
logRecord.setAttribute(`foo${i}`, attributeValue);
203-
}
204-
205-
it('should remove / drop all remaining values after the number of values exceeds this limit', () => {
206205
const { attributes, droppedAttributesCount } = logRecord;
207206
assert.strictEqual(Object.keys(attributes).length, 100);
208207
assert.strictEqual(attributes.foo0, 'bar0');
@@ -212,6 +211,26 @@ describe('LogRecord', () => {
212211
assert.strictEqual(attributes.foo149, undefined);
213212
assert.strictEqual(droppedAttributesCount, 50);
214213
});
214+
215+
it('should not print message when there are no dropped attributes', () => {
216+
const warnStub = sinon.spy(diag, 'warn');
217+
const { logRecord } = setup({ attributeCountLimit: 10 });
218+
for (let i = 0; i < 7; i++) {
219+
logRecord.setAttribute(`foo${i}`, `bar${i}`);
220+
}
221+
sinon.assert.callCount(warnStub, 0);
222+
warnStub.restore();
223+
});
224+
225+
it('should print message only once when attribute(s) are dropped', () => {
226+
const warnStub = sinon.spy(diag, 'warn');
227+
const { logRecord } = setup({ attributeCountLimit: 5 });
228+
for (let i = 0; i < 7; i++) {
229+
logRecord.setAttribute(`foo${i}`, `bar${i}`);
230+
}
231+
sinon.assert.callCount(warnStub, 1);
232+
warnStub.restore();
233+
});
215234
});
216235

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

0 commit comments

Comments
 (0)