Skip to content

Commit 19f8996

Browse files
committed
Add missing set value attribute shortcuts
1 parent 207c861 commit 19f8996

6 files changed

Lines changed: 47 additions & 8 deletions

File tree

api/all/src/main/java/io/opentelemetry/api/logs/LogRecordBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,21 @@ default LogRecordBuilder setAttribute(String key, int value) {
196196
return setAttribute(key, (long) value);
197197
}
198198

199+
/**
200+
* Sets a {@link Value} attribute on the {@code LogRecord}. If the {@code LogRecord} previously
201+
* contained a mapping for the key, the old value is replaced by the specified value.
202+
*
203+
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
204+
* pre-allocate your keys, if possible.
205+
*
206+
* @param key the key for this attribute.
207+
* @param value the value for this attribute.
208+
* @return this.
209+
*/
210+
default LogRecordBuilder setAttribute(String key, Value<?> value) {
211+
return setAttribute(AttributeKey.valueKey(key), value);
212+
}
213+
199214
/**
200215
* Sets the event name, which identifies the class / type of the Event.
201216
*

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import io.opentelemetry.api.common.AttributeKey;
1212
import io.opentelemetry.api.common.Attributes;
13+
import io.opentelemetry.api.common.Value;
1314
import io.opentelemetry.api.internal.ApiUsageLogger;
1415
import io.opentelemetry.context.Context;
1516
import io.opentelemetry.context.ImplicitContextKeyed;
@@ -148,6 +149,21 @@ default Span setAttribute(String key, boolean value) {
148149
return setAttribute(AttributeKey.booleanKey(key), value);
149150
}
150151

152+
/**
153+
* Sets a {@link Value} attribute on the {@code Span}. If the {@code Span} previously contained a
154+
* mapping for the key, the old value is replaced by the specified value.
155+
*
156+
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
157+
* pre-allocate your keys, if possible.
158+
*
159+
* @param key the key for this attribute.
160+
* @param value the value for this attribute.
161+
* @return this.
162+
*/
163+
default Span setAttribute(String key, Value<?> value) {
164+
return setAttribute(AttributeKey.valueKey(key), value);
165+
}
166+
151167
/**
152168
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
153169
* the key, the old value is replaced by the specified value.

api/all/src/test/java/io/opentelemetry/api/trace/PropagatedSpanTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static org.assertj.core.api.Assertions.assertThat;
1616

1717
import io.opentelemetry.api.common.Attributes;
18+
import io.opentelemetry.api.common.Value;
1819
import java.time.Instant;
1920
import java.util.concurrent.TimeUnit;
2021
import org.junit.jupiter.api.Test;
@@ -40,16 +41,17 @@ void doNotCrash() {
4041
span.setAttribute(booleanKey("MyBooleanAttributeKey"), true);
4142
span.setAttribute(longKey("MyLongAttributeKey"), 123L);
4243
span.setAttribute(longKey("MyLongAttributeKey"), 123);
43-
span.setAttribute("NullString", null);
44+
span.setAttribute("NullString", (String) null);
4445
span.setAttribute("EmptyString", "");
4546
span.setAttribute("long", 1);
4647
span.setAttribute("double", 1.0);
4748
span.setAttribute("boolean", true);
49+
span.setAttribute("value", Value.of("val"));
4850
span.setAttribute(stringArrayKey("NullArrayString"), null);
4951
span.setAttribute(booleanArrayKey("NullArrayBoolean"), null);
5052
span.setAttribute(longArrayKey("NullArrayLong"), null);
5153
span.setAttribute(doubleArrayKey("NullArrayDouble"), null);
52-
span.setAttribute((String) null, null);
54+
span.setAttribute((String) null, (String) null);
5355
span.setAllAttributes(null);
5456
span.setAllAttributes(Attributes.empty());
5557
span.setAllAttributes(

sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilderTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static io.opentelemetry.api.common.AttributeKey.doubleKey;
1010
import static io.opentelemetry.api.common.AttributeKey.longKey;
1111
import static io.opentelemetry.api.common.AttributeKey.stringKey;
12+
import static io.opentelemetry.api.common.AttributeKey.valueKey;
1213
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
1314
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
1415
import static org.mockito.ArgumentMatchers.eq;
@@ -160,14 +161,16 @@ void testConvenienceAttributeMethods() {
160161
.setAttribute("dk", 12.123)
161162
.setAttribute("bk", true)
162163
.setAttribute("ik", 13)
164+
.setAttribute("vk", Value.of(new byte[] {1, 2, 3}))
163165
.emit();
164166
assertThat(emittedLog.get().toLogRecordData())
165167
.hasAttributesSatisfyingExactly(
166168
equalTo(stringKey("foo"), "bar"),
167169
equalTo(longKey("lk"), 12L),
168170
equalTo(doubleKey("dk"), 12.123),
169171
equalTo(booleanKey("bk"), true),
170-
equalTo(longKey("ik"), 13L));
172+
equalTo(longKey("ik"), 13L),
173+
equalTo(valueKey("vk"), Value.of(new byte[] {1, 2, 3})));
171174
}
172175

173176
@Test

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ void setAttribute_nullAttributeValue_afterEnd() {
391391
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
392392
assertThat(span.toSpanData().getAttributes().size()).isEqualTo(10);
393393
span.end();
394-
span.setAttribute("emptyString", null);
394+
span.setAttribute("emptyString", (String) null);
395395
span.setAttribute(stringKey("emptyStringAttributeValue"), null);
396396
span.setAttribute(longKey("longAttribute"), null);
397397
span.setAttribute(booleanKey("boolAttribute"), null);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ void setAttribute() {
548548
SdkSpan span = createTestRootSpan();
549549
try {
550550
span.setAttribute("StringKey", "StringVal");
551-
span.setAttribute("NullStringKey", null);
551+
span.setAttribute("NullStringKey", (String) null);
552552
span.setAttribute("EmptyStringKey", "");
553553
span.setAttribute(stringKey("NullStringAttributeValue"), null);
554554
span.setAttribute(stringKey("EmptyStringAttributeValue"), "");
@@ -557,6 +557,7 @@ void setAttribute() {
557557
span.setAttribute(longKey("LongKey3"), 6L);
558558
span.setAttribute("DoubleKey", 10.0);
559559
span.setAttribute("BooleanKey", false);
560+
span.setAttribute("BytesValueKey", Value.of(new byte[] {1, 2, 3}));
560561
span.setAttribute(
561562
stringArrayKey("ArrayStringKey"), Arrays.asList("StringVal", null, "", "StringVal2"));
562563
span.setAttribute(longArrayKey("ArrayLongKey"), Arrays.asList(1L, 2L, 3L, 4L, 5L));
@@ -579,7 +580,7 @@ void setAttribute() {
579580
span.end();
580581
}
581582
SpanData spanData = span.toSpanData();
582-
assertThat(spanData.getAttributes().size()).isEqualTo(17);
583+
assertThat(spanData.getAttributes().size()).isEqualTo(18);
583584
assertThat(spanData.getAttributes().get(stringKey("StringKey"))).isNotNull();
584585
assertThat(spanData.getAttributes().get(stringKey("EmptyStringKey"))).isNotNull();
585586
assertThat(spanData.getAttributes().get(stringKey("EmptyStringAttributeValue"))).isNotNull();
@@ -588,6 +589,8 @@ void setAttribute() {
588589
assertThat(spanData.getAttributes().get(longKey("LongKey3"))).isEqualTo(6L);
589590
assertThat(spanData.getAttributes().get(doubleKey("DoubleKey"))).isNotNull();
590591
assertThat(spanData.getAttributes().get(booleanKey("BooleanKey"))).isNotNull();
592+
assertThat(spanData.getAttributes().get(valueKey("BytesValueKey")))
593+
.isEqualTo(Value.of(new byte[] {1, 2, 3}));
591594
assertThat(spanData.getAttributes().get(stringArrayKey("ArrayStringKey"))).isNotNull();
592595
assertThat(spanData.getAttributes().get(longArrayKey("ArrayLongKey"))).isNotNull();
593596
assertThat(spanData.getAttributes().get(doubleArrayKey("ArrayDoubleKey"))).isNotNull();
@@ -630,7 +633,7 @@ void setAttribute_nullKeys() {
630633
span.setAttribute(null, Collections.emptyList());
631634
span.setAttribute(null, Collections.emptyList());
632635
span.setAttribute(null, Collections.emptyList());
633-
span.setAttribute(null, Value.empty());
636+
span.setAttribute((AttributeKey<Value<?>>) null, Value.empty());
634637
assertThat(span.toSpanData().getAttributes().size()).isZero();
635638
}
636639

@@ -658,7 +661,7 @@ void setAttribute_emptyArrayAttributeValue() {
658661
@Test
659662
void setAttribute_nullStringValue() {
660663
SdkSpan span = createTestRootSpan();
661-
span.setAttribute("nullString", null);
664+
span.setAttribute("nullString", (String) null);
662665
span.setAttribute("emptyString", "");
663666
span.setAttribute(stringKey("nullStringAttributeValue"), null);
664667
span.setAttribute(stringKey("emptyStringAttributeValue"), "");

0 commit comments

Comments
 (0)