diff --git a/api/all/src/main/java/io/opentelemetry/api/trace/SpanBuilder.java b/api/all/src/main/java/io/opentelemetry/api/trace/SpanBuilder.java index 3b1726933a8..9f9e5c0ce5e 100644 --- a/api/all/src/main/java/io/opentelemetry/api/trace/SpanBuilder.java +++ b/api/all/src/main/java/io/opentelemetry/api/trace/SpanBuilder.java @@ -238,6 +238,18 @@ public interface SpanBuilder { */ SpanBuilder setAttribute(AttributeKey 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 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. diff --git a/api/testing-internal/src/main/java/io/opentelemetry/api/testing/internal/AbstractDefaultTracerTest.java b/api/testing-internal/src/main/java/io/opentelemetry/api/testing/internal/AbstractDefaultTracerTest.java index c90b7da21d9..c03f877f8a6 100644 --- a/api/testing-internal/src/main/java/io/opentelemetry/api/testing/internal/AbstractDefaultTracerTest.java +++ b/api/testing-internal/src/main/java/io/opentelemetry/api/testing/internal/AbstractDefaultTracerTest.java @@ -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 @@ void doNotCrash_NoopImplementation() { spanBuilder.setStartTimestamp(12345L, TimeUnit.NANOSECONDS); spanBuilder.setStartTimestamp(Instant.EPOCH); spanBuilder.setStartTimestamp(null); + spanBuilder.setAttribute(longKey("MyLongAttributeKey"), 123); assertThat(spanBuilder.startSpan().getSpanContext().isValid()).isFalse(); }) .doesNotThrowAnyException(); diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-api.txt b/docs/apidiffs/current_vs_latest/opentelemetry-api.txt index 351726df140..4bc9a878f22 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-api.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-api.txt @@ -1,2 +1,4 @@ Comparing source compatibility of opentelemetry-api-1.45.0-SNAPSHOT.jar against opentelemetry-api-1.44.1.jar -No changes. \ No newline at end of file +*** 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, int) diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanBuilderTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanBuilderTest.java index 461ed774cca..9c9ecdc7e9c 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanBuilderTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanBuilderTest.java @@ -246,19 +246,21 @@ void setAttribute() { .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(); }