Skip to content

Commit

Permalink
Add ReadableSpan#getAttributes (#6382)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Apr 17, 2024
1 parent 8937a10 commit 7b4bb8f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-trace.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of against
No changes.
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.trace.ReadableSpan (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.common.Attributes getAttributes()
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.sdk.trace;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
Expand Down Expand Up @@ -111,4 +112,20 @@ default InstrumentationScopeInfo getInstrumentationScopeInfo() {
*/
@Nullable
<T> T getAttribute(AttributeKey<T> key);

/**
* Returns the Span attributes.
*
* <p>Attributes can be changed during the lifetime of the Span by using {@link
* Span#setAttribute}} so this value cannot be cached.
*
* <p>Note: the implementation of this method performs locking and returns an immutable copy to
* ensure thread-safe behavior. If you only need a single attribute it is better to call {@link
* #getAttribute(AttributeKey)}.
*
* @return the Span attributes, or {@link Attributes#empty()} if the span has no attributes.
*/
default Attributes getAttributes() {
return Attributes.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ public <T> T getAttribute(AttributeKey<T> key) {
}
}

@Override
public Attributes getAttributes() {
synchronized (lock) {
return attributes == null ? Attributes.empty() : attributes.immutableCopy();
}
}

@Override
public boolean hasEnded() {
synchronized (lock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,32 @@ void getAttribute() {
}
}

@Test
void getAttributes() {
SdkSpan span = createTestSpanWithAttributes(attributes);
try {
assertThat(span.getAttributes())
.isEqualTo(
Attributes.builder()
.put("MyBooleanAttributeKey", false)
.put("MyStringAttributeKey", "MyStringAttributeValue")
.put("MyLongAttributeKey", 123L)
.build());
} finally {
span.end();
}
}

@Test
void getAttributes_Empty() {
SdkSpan span = createTestSpan(SpanKind.INTERNAL);
try {
assertThat(span.getAttributes()).isEqualTo(Attributes.empty());
} finally {
span.end();
}
}

@Test
@SuppressWarnings("deprecation") // Testing deprecated code
void getInstrumentationLibraryInfo() {
Expand Down

0 comments on commit 7b4bb8f

Please sign in to comment.