From 8bb16dc4005265f303a2d25980ae7f243b9e3fba Mon Sep 17 00:00:00 2001 From: Hyun Oh Date: Wed, 13 Dec 2023 21:22:11 +0900 Subject: [PATCH 1/4] feat(sdk-logs): make dropping attribute print message --- .../packages/sdk-logs/src/LogRecord.ts | 3 + .../sdk-logs/test/common/LogRecord.test.ts | 67 ++++++++++++------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/experimental/packages/sdk-logs/src/LogRecord.ts b/experimental/packages/sdk-logs/src/LogRecord.ts index abc4f452855..2df76fe05df 100644 --- a/experimental/packages/sdk-logs/src/LogRecord.ts +++ b/experimental/packages/sdk-logs/src/LogRecord.ts @@ -140,6 +140,9 @@ export class LogRecord implements ReadableLogRecord { this._logRecordLimits.attributeCountLimit && !Object.prototype.hasOwnProperty.call(this.attributes, key) ) { + if (this.droppedAttributesCount === 1) { + api.diag.warn('Dropping extra attributes.'); + } return this; } if (isAttributeValue(value)) { diff --git a/experimental/packages/sdk-logs/test/common/LogRecord.test.ts b/experimental/packages/sdk-logs/test/common/LogRecord.test.ts index 5a0acaa6db2..3518e99fd18 100644 --- a/experimental/packages/sdk-logs/test/common/LogRecord.test.ts +++ b/experimental/packages/sdk-logs/test/common/LogRecord.test.ts @@ -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'); @@ -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', () => { From f702bffe81ceeaa9af90b33c08c66adbd427a0c6 Mon Sep 17 00:00:00 2001 From: Hyun Oh Date: Wed, 10 Apr 2024 23:58:45 +0900 Subject: [PATCH 2/4] chore: update changelog --- experimental/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index c8d631e6c17..74eb6efca91 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -9,6 +9,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): make dropping attribute print message ### :bug: (Bug Fix) From 7369607220d8c5cfd60c7206b6b0e5d9a745c5ca Mon Sep 17 00:00:00 2001 From: Hyun Oh Date: Thu, 11 Apr 2024 00:11:25 +0900 Subject: [PATCH 3/4] chore(sdk-logs): add comment to explain message logic --- experimental/packages/sdk-logs/src/LogRecord.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/packages/sdk-logs/src/LogRecord.ts b/experimental/packages/sdk-logs/src/LogRecord.ts index 2df76fe05df..e7704ec3892 100644 --- a/experimental/packages/sdk-logs/src/LogRecord.ts +++ b/experimental/packages/sdk-logs/src/LogRecord.ts @@ -140,6 +140,7 @@ 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) { api.diag.warn('Dropping extra attributes.'); } From 6fc5d64e9a9826dfccc8a6f18b97fb4b12f735df Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 15 Apr 2024 13:01:40 +0200 Subject: [PATCH 4/4] Update experimental/CHANGELOG.md --- experimental/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index bfc04ee45cd..f7733df890b 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -16,7 +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): make dropping attribute print message +* 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)