From fb6d8dc8d0d89df0fa399d132bffb975daa7bf1b Mon Sep 17 00:00:00 2001 From: Martin Kubliniak Date: Fri, 15 Dec 2023 08:36:29 +0100 Subject: [PATCH] feat(sdk-trace-base): improve log messages when dropping span events (#4223) --- CHANGELOG.md | 2 ++ packages/opentelemetry-sdk-trace-base/src/Span.ts | 10 +++++++++- .../test/common/Span.test.ts | 11 +++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d81af36519..28759606768 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :rocket: (Enhancement) +* feat(sdk-trace-base): improve log messages when dropping span events [#4223](https://github.com/open-telemetry/opentelemetry-js/pull/4223) @mkubliniak + ### :bug: (Bug Fix) ### :books: (Refine Doc) diff --git a/packages/opentelemetry-sdk-trace-base/src/Span.ts b/packages/opentelemetry-sdk-trace-base/src/Span.ts index e3e37d82136..29af9fc6513 100644 --- a/packages/opentelemetry-sdk-trace-base/src/Span.ts +++ b/packages/opentelemetry-sdk-trace-base/src/Span.ts @@ -185,7 +185,9 @@ export class Span implements APISpan, ReadableSpan { return this; } if (this.events.length >= this._spanLimits.eventCountLimit!) { - diag.warn('Dropping extra events.'); + if (this._droppedEventsCount === 0) { + diag.debug('Dropping extra events.'); + } this.events.shift(); this._droppedEventsCount++; } @@ -242,6 +244,12 @@ export class Span implements APISpan, ReadableSpan { this._duration = [0, 0]; } + if (this._droppedEventsCount > 0) { + diag.warn( + `Dropped ${this._droppedEventsCount} events because eventCountLimit reached` + ); + } + this._spanProcessor.onEnd(this); } 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 11b0f3f318e..0067f85c897 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts @@ -15,6 +15,7 @@ */ import { + diag, SpanStatusCode, Exception, ROOT_CONTEXT, @@ -786,6 +787,10 @@ describe('Span', () => { spanContext, SpanKind.CLIENT ); + + const debugStub = sinon.spy(diag, 'debug'); + const warnStub = sinon.spy(diag, 'warn'); + for (let i = 0; i < 150; i++) { span.addEvent('sent' + i); } @@ -793,6 +798,12 @@ describe('Span', () => { assert.strictEqual(span.events.length, 100); assert.strictEqual(span.events[span.events.length - 1].name, 'sent149'); + + sinon.assert.calledOnceWithExactly(debugStub, 'Dropping extra events.'); + sinon.assert.calledOnceWithExactly( + warnStub, + 'Dropped 50 events because eventCountLimit reached' + ); }); it('should store the count of dropped events in droppedEventsCount', () => {