From f5ef8de1cc92ad22d7d95df6a5f585f9d64ddef1 Mon Sep 17 00:00:00 2001 From: Hyun Oh Date: Fri, 10 Nov 2023 01:19:46 +0900 Subject: [PATCH] fix(sdk-logs): avoid map attribute set when count limit exceeded (#4195) Co-authored-by: Marc Pichler --- experimental/CHANGELOG.md | 2 ++ .../packages/sdk-logs/src/LogRecord.ts | 22 +++++++++------- .../sdk-logs/test/common/LogRecord.test.ts | 26 +++++++++++++++++-- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 7a5a363e44..bb1fd6aca0 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -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) diff --git a/experimental/packages/sdk-logs/src/LogRecord.ts b/experimental/packages/sdk-logs/src/LogRecord.ts index ed76d5eb07..8d8fae6168 100644 --- a/experimental/packages/sdk-logs/src/LogRecord.ts +++ b/experimental/packages/sdk-logs/src/LogRecord.ts @@ -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; } @@ -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; } diff --git a/experimental/packages/sdk-logs/test/common/LogRecord.test.ts b/experimental/packages/sdk-logs/test/common/LogRecord.test.ts index e32ab39838..678b4c2e72 100644 --- a/experimental/packages/sdk-logs/test/common/LogRecord.test.ts +++ b/experimental/packages/sdk-logs/test/common/LogRecord.test.ts @@ -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); }); });