From 5196f424c450d3507e264bb25fdcfb46da436882 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 15 Jan 2024 15:53:52 +0100 Subject: [PATCH] fix(sdk-trace-base): ensure attribute value length limit is enforced on span creation (#4417) * fix(sdk-trace-base): ensure attribute value length limit is enforced on span creation * fix(changelog): add changelog entry --- CHANGELOG.md | 2 ++ .../opentelemetry-sdk-trace-base/src/Span.ts | 4 ++-- .../test/common/Span.test.ts | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72b6731991..fc065fe434 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) +* fix(sdk-trace-base): ensure attribute value length limit is enforced on span creation [#4417](https://github.com/open-telemetry/opentelemetry-js/pull/4417) @pichlermarc + ### :books: (Refine Doc) ### :house: (Internal) diff --git a/packages/opentelemetry-sdk-trace-base/src/Span.ts b/packages/opentelemetry-sdk-trace-base/src/Span.ts index 29af9fc651..2a00be5d8a 100644 --- a/packages/opentelemetry-sdk-trace-base/src/Span.ts +++ b/packages/opentelemetry-sdk-trace-base/src/Span.ts @@ -120,6 +120,8 @@ export class Span implements APISpan, ReadableSpan { this.resource = parentTracer.resource; this.instrumentationLibrary = parentTracer.instrumentationLibrary; this._spanLimits = parentTracer.getSpanLimits(); + this._attributeValueLengthLimit = + this._spanLimits.attributeValueLengthLimit || 0; if (attributes != null) { this.setAttributes(attributes); @@ -127,8 +129,6 @@ export class Span implements APISpan, ReadableSpan { this._spanProcessor = parentTracer.getActiveSpanProcessor(); this._spanProcessor.onStart(this, context); - this._attributeValueLengthLimit = - this._spanLimits.attributeValueLengthLimit || 0; } spanContext(): SpanContext { diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts index 0067f85c89..5dd7ec79e5 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts @@ -369,6 +369,22 @@ describe('Span', () => { span.setAttribute('attr-non-string', true); assert.strictEqual(span.attributes['attr-non-string'], true); }); + + it('should truncate value when attributes are passed to the constructor', () => { + const span = new Span( + tracer, + ROOT_CONTEXT, + name, + spanContext, + SpanKind.CLIENT, + undefined, + undefined, + undefined, + undefined, + { 'attr-with-more-length': 'abcdefgh' } + ); + assert.strictEqual(span.attributes['attr-with-more-length'], 'abcde'); + }); }); describe('when "attributeValueLengthLimit" option is invalid', () => {