From da78075575c2df8c799c8909c7a51920f98736a2 Mon Sep 17 00:00:00 2001 From: John Bley Date: Thu, 22 Feb 2024 10:46:52 -0500 Subject: [PATCH 1/4] Fault in SdkSpan.events --- .../main/java/io/opentelemetry/sdk/trace/SdkSpan.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java index fec835b17ff..086b41895d5 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java @@ -71,7 +71,8 @@ final class SdkSpan implements ReadWriteSpan, ExtendedSpan { // List of recorded events. @GuardedBy("lock") - private final List events; + @Nullable + private List events; // faulted in // Number of events recorded. @GuardedBy("lock") @@ -126,7 +127,6 @@ private SdkSpan( this.clock = clock; this.startEpochNanos = startEpochNanos; this.attributes = attributes; - this.events = new ArrayList<>(); this.spanLimits = spanLimits; } @@ -378,6 +378,9 @@ private void addTimedEvent(EventData timedEvent) { logger.log(Level.FINE, "Calling addEvent() on an ended Span."); return; } + if (events == null) { + events = new ArrayList<>(); + } if (events.size() < spanLimits.getMaxNumberOfEvents()) { events.add(timedEvent); } @@ -518,7 +521,7 @@ long getStartEpochNanos() { @GuardedBy("lock") private List getImmutableTimedEvents() { - if (events.isEmpty()) { + if (events == null || events.isEmpty()) { return Collections.emptyList(); } From 2ccec20744bb22823324cf63e5f46ec634785f56 Mon Sep 17 00:00:00 2001 From: John Bley Date: Thu, 22 Feb 2024 11:43:52 -0500 Subject: [PATCH 2/4] Add test for proper handling of un-faulted-in events (which should be empty) --- .../src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java index df7a6ba1c02..d18970bc9ab 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java @@ -655,6 +655,7 @@ void setAllAttributes_emptyAttributes() { @Test void addEvent() { SdkSpan span = createTestRootSpan(); + assertThat(span.toSpanData().getEvents()).isEmpty(); try { span.addEvent("event1"); span.addEvent("event2", Attributes.of(stringKey("e1key"), "e1Value")); From 42363dda8ec81cf6c71886449fc4efe5bbc6c036 Mon Sep 17 00:00:00 2001 From: John Bley Date: Thu, 22 Feb 2024 12:24:51 -0500 Subject: [PATCH 3/4] Simplify logic for faulted in events --- sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java | 2 +- .../src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java index 086b41895d5..8a34f2f7ce0 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java @@ -521,7 +521,7 @@ long getStartEpochNanos() { @GuardedBy("lock") private List getImmutableTimedEvents() { - if (events == null || events.isEmpty()) { + if (events == null) { return Collections.emptyList(); } diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java index d18970bc9ab..df7a6ba1c02 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java @@ -655,7 +655,6 @@ void setAllAttributes_emptyAttributes() { @Test void addEvent() { SdkSpan span = createTestRootSpan(); - assertThat(span.toSpanData().getEvents()).isEmpty(); try { span.addEvent("event1"); span.addEvent("event2", Attributes.of(stringKey("e1key"), "e1Value")); From 572a3cb8f176547589a2df6932e06c93070bd39f Mon Sep 17 00:00:00 2001 From: John Watson Date: Fri, 23 Feb 2024 18:49:15 -0800 Subject: [PATCH 4/4] Update sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java --- sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java index 8a34f2f7ce0..a6b28043d29 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java @@ -72,7 +72,7 @@ final class SdkSpan implements ReadWriteSpan, ExtendedSpan { // List of recorded events. @GuardedBy("lock") @Nullable - private List events; // faulted in + private List events; // Number of events recorded. @GuardedBy("lock")