Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReadableSpan#getAttributes #6382

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
*/
@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();

Check warning on line 129 in sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ReadableSpan.java

View check run for this annotation

Codecov / codecov/patch

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ReadableSpan.java#L129

Added line #L129 was not covered by tests
}
}
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
Loading