Add convenience method `setAttribute(Attribute<Long>, int)` to SpanBuilder (matching the existing convenience method in Span) (#6884)

Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
This commit is contained in:
Xin 2024-11-18 13:41:17 -07:00 committed by GitHub
parent 46b2fcda7d
commit 4c30ec48d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 4 deletions

View File

@ -238,6 +238,18 @@ public interface SpanBuilder {
*/
<T> SpanBuilder setAttribute(AttributeKey<T> key, T value);
/**
* Sets an attribute to the newly created {@code Span}. If {@code SpanBuilder} previously
* contained a mapping for the key, the old value is replaced by the specified value.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default SpanBuilder setAttribute(AttributeKey<Long> key, int value) {
return setAttribute(key, (long) value);
}
/**
* Sets attributes to the {@link SpanBuilder}. If the {@link SpanBuilder} previously contained a
* mapping for any of the keys, the old values are replaced by the specified values.

View File

@ -5,6 +5,7 @@
package io.opentelemetry.api.testing.internal;
import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
@ -155,6 +156,7 @@ public abstract class AbstractDefaultTracerTest {
spanBuilder.setStartTimestamp(12345L, TimeUnit.NANOSECONDS);
spanBuilder.setStartTimestamp(Instant.EPOCH);
spanBuilder.setStartTimestamp(null);
spanBuilder.setAttribute(longKey("MyLongAttributeKey"), 123);
assertThat(spanBuilder.startSpan().getSpanContext().isValid()).isFalse();
})
.doesNotThrowAnyException();

View File

@ -1,2 +1,4 @@
Comparing source compatibility of opentelemetry-api-1.45.0-SNAPSHOT.jar against opentelemetry-api-1.44.1.jar
No changes.
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.trace.SpanBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.trace.SpanBuilder setAttribute(io.opentelemetry.api.common.AttributeKey<java.lang.Long>, int)

View File

@ -246,19 +246,21 @@ class SdkSpanBuilderTest {
.setAttribute("long", 12345L)
.setAttribute("double", .12345)
.setAttribute("boolean", true)
.setAttribute(stringKey("stringAttribute"), "attrvalue");
.setAttribute(stringKey("stringAttribute"), "attrvalue")
.setAttribute(longKey("longAttribute"), 123);
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
try {
SpanData spanData = span.toSpanData();
Attributes attrs = spanData.getAttributes();
assertThat(attrs.size()).isEqualTo(5);
assertThat(attrs.size()).isEqualTo(6);
assertThat(attrs.get(stringKey("string"))).isEqualTo("value");
assertThat(attrs.get(longKey("long"))).isEqualTo(12345L);
assertThat(attrs.get(doubleKey("double"))).isEqualTo(0.12345);
assertThat(attrs.get(booleanKey("boolean"))).isEqualTo(true);
assertThat(attrs.get(stringKey("stringAttribute"))).isEqualTo("attrvalue");
assertThat(spanData.getTotalAttributeCount()).isEqualTo(5);
assertThat(attrs.get(longKey("longAttribute"))).isEqualTo(123);
assertThat(spanData.getTotalAttributeCount()).isEqualTo(6);
} finally {
span.end();
}