Skip to content

Commit

Permalink
Add convenience method setAttribute(Attribute<Long>, int) to SpanBu…
Browse files Browse the repository at this point in the history
…ilder (matching the existing convenience method in Span) (#6884)

Co-authored-by: Trask Stalnaker <[email protected]>
  • Loading branch information
Stoyas and trask authored Nov 18, 2024
1 parent 46b2fcd commit 4c30ec4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
12 changes: 12 additions & 0 deletions api/all/src/main/java/io/opentelemetry/api/trace/SpanBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-api.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 4c30ec4

Please sign in to comment.