Skip to content

Commit 4c30ec4

Browse files
Stoyastrask
andauthored
Add convenience method setAttribute(Attribute<Long>, int) to SpanBuilder (matching the existing convenience method in Span) (#6884)
Co-authored-by: Trask Stalnaker <[email protected]>
1 parent 46b2fcd commit 4c30ec4

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

api/all/src/main/java/io/opentelemetry/api/trace/SpanBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,18 @@ public interface SpanBuilder {
238238
*/
239239
<T> SpanBuilder setAttribute(AttributeKey<T> key, T value);
240240

241+
/**
242+
* Sets an attribute to the newly created {@code Span}. If {@code SpanBuilder} previously
243+
* contained a mapping for the key, the old value is replaced by the specified value.
244+
*
245+
* @param key the key for this attribute.
246+
* @param value the value for this attribute.
247+
* @return this.
248+
*/
249+
default SpanBuilder setAttribute(AttributeKey<Long> key, int value) {
250+
return setAttribute(key, (long) value);
251+
}
252+
241253
/**
242254
* Sets attributes to the {@link SpanBuilder}. If the {@link SpanBuilder} previously contained a
243255
* mapping for any of the keys, the old values are replaced by the specified values.

api/testing-internal/src/main/java/io/opentelemetry/api/testing/internal/AbstractDefaultTracerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.api.testing.internal;
77

8+
import static io.opentelemetry.api.common.AttributeKey.longKey;
89
import static io.opentelemetry.api.common.AttributeKey.stringKey;
910
import static org.assertj.core.api.Assertions.assertThat;
1011
import static org.assertj.core.api.Assertions.assertThatCode;
@@ -155,6 +156,7 @@ void doNotCrash_NoopImplementation() {
155156
spanBuilder.setStartTimestamp(12345L, TimeUnit.NANOSECONDS);
156157
spanBuilder.setStartTimestamp(Instant.EPOCH);
157158
spanBuilder.setStartTimestamp(null);
159+
spanBuilder.setAttribute(longKey("MyLongAttributeKey"), 123);
158160
assertThat(spanBuilder.startSpan().getSpanContext().isValid()).isFalse();
159161
})
160162
.doesNotThrowAnyException();
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Comparing source compatibility of opentelemetry-api-1.45.0-SNAPSHOT.jar against opentelemetry-api-1.44.1.jar
2-
No changes.
2+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.trace.SpanBuilder (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.trace.SpanBuilder setAttribute(io.opentelemetry.api.common.AttributeKey<java.lang.Long>, int)

sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanBuilderTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,21 @@ void setAttribute() {
246246
.setAttribute("long", 12345L)
247247
.setAttribute("double", .12345)
248248
.setAttribute("boolean", true)
249-
.setAttribute(stringKey("stringAttribute"), "attrvalue");
249+
.setAttribute(stringKey("stringAttribute"), "attrvalue")
250+
.setAttribute(longKey("longAttribute"), 123);
250251

251252
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
252253
try {
253254
SpanData spanData = span.toSpanData();
254255
Attributes attrs = spanData.getAttributes();
255-
assertThat(attrs.size()).isEqualTo(5);
256+
assertThat(attrs.size()).isEqualTo(6);
256257
assertThat(attrs.get(stringKey("string"))).isEqualTo("value");
257258
assertThat(attrs.get(longKey("long"))).isEqualTo(12345L);
258259
assertThat(attrs.get(doubleKey("double"))).isEqualTo(0.12345);
259260
assertThat(attrs.get(booleanKey("boolean"))).isEqualTo(true);
260261
assertThat(attrs.get(stringKey("stringAttribute"))).isEqualTo("attrvalue");
261-
assertThat(spanData.getTotalAttributeCount()).isEqualTo(5);
262+
assertThat(attrs.get(longKey("longAttribute"))).isEqualTo(123);
263+
assertThat(spanData.getTotalAttributeCount()).isEqualTo(6);
262264
} finally {
263265
span.end();
264266
}

0 commit comments

Comments
 (0)