Skip to content

Commit 7ddb3b2

Browse files
authored
Add missing set value attribute shortcuts (#8255)
1 parent f2ed2ed commit 7ddb3b2

11 files changed

Lines changed: 77 additions & 9 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ 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+
* <p>Instrumentations should assume that backends do not index individual properties of complex
207+
* attributes, that querying or aggregating on such properties is inefficient and complicated, and
208+
* that reporting complex attributes carries higher performance overhead.
209+
*
210+
* @param key the key for this attribute.
211+
* @param value the value for this attribute.
212+
* @return this.
213+
*/
214+
default LogRecordBuilder setAttribute(String key, Value<?> value) {
215+
return setAttribute(AttributeKey.valueKey(key), value);
216+
}
217+
199218
/**
200219
* Sets the event name, which identifies the class / type of the Event.
201220
*

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

Lines changed: 20 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.common.impl.ApiUsageLogger;
1415
import io.opentelemetry.context.Context;
1516
import io.opentelemetry.context.ImplicitContextKeyed;
@@ -148,6 +149,25 @@ 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+
* <p>Instrumentations should assume that backends do not index individual properties of complex
160+
* attributes, that querying or aggregating on such properties is inefficient and complicated, and
161+
* that reporting complex attributes carries higher performance overhead.
162+
*
163+
* @param key the key for this attribute.
164+
* @param value the value for this attribute.
165+
* @return this.
166+
*/
167+
default Span setAttribute(String key, Value<?> value) {
168+
return setAttribute(AttributeKey.valueKey(key), value);
169+
}
170+
151171
/**
152172
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
153173
* 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(
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
Comparing source compatibility of opentelemetry-api-1.63.0-SNAPSHOT.jar against opentelemetry-api-1.62.0.jar
2-
No changes.
2+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.logs.LogRecordBuilder (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, io.opentelemetry.api.common.Value<?>)
5+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.trace.Span (not serializable)
6+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
7+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.trace.Span setAttribute(java.lang.String, io.opentelemetry.api.common.Value<?>)

opencensus-shim/src/main/java/io/opentelemetry/opencensusshim/DelegatingSpan.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.google.errorprone.annotations.MustBeClosed;
99
import io.opentelemetry.api.common.AttributeKey;
1010
import io.opentelemetry.api.common.Attributes;
11+
import io.opentelemetry.api.common.Value;
1112
import io.opentelemetry.api.trace.Span;
1213
import io.opentelemetry.api.trace.SpanContext;
1314
import io.opentelemetry.api.trace.StatusCode;
@@ -77,6 +78,11 @@ default Span setAttribute(String key, boolean value) {
7778
return getDelegate().setAttribute(key, value);
7879
}
7980

81+
@Override
82+
default Span setAttribute(String key, Value<?> value) {
83+
return getDelegate().setAttribute(key, value);
84+
}
85+
8086
@Override
8187
default Span setAttribute(AttributeKey<Long> key, int value) {
8288
return getDelegate().setAttribute(key, value);

opencensus-shim/src/main/java/io/opentelemetry/opencensusshim/OpenTelemetryNoRecordEventsSpanImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.opencensus.trace.Status;
3434
import io.opentelemetry.api.common.AttributeKey;
3535
import io.opentelemetry.api.common.Attributes;
36+
import io.opentelemetry.api.common.Value;
3637
import io.opentelemetry.api.trace.StatusCode;
3738
import java.util.EnumSet;
3839
import java.util.Map;
@@ -129,6 +130,11 @@ public io.opentelemetry.api.trace.Span setAttribute(String key, boolean value) {
129130
return this;
130131
}
131132

133+
@Override
134+
public io.opentelemetry.api.trace.Span setAttribute(String key, Value<?> value) {
135+
return this;
136+
}
137+
132138
@Override
133139
public <T> io.opentelemetry.api.trace.Span setAttribute(AttributeKey<T> key, @Nullable T value) {
134140
return this;

opencensus-shim/src/test/java/io/opentelemetry/opencensusshim/DelegatingSpanTest.java

Lines changed: 2 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.trace.Span;
1415
import io.opentelemetry.api.trace.SpanContext;
1516
import io.opentelemetry.api.trace.StatusCode;
@@ -95,6 +96,7 @@ static Stream<Arguments> delegateMethodsProvider() {
9596
Arguments.of("setAttribute", new Class<?>[] {String.class, long.class}, times(1)),
9697
Arguments.of("setAttribute", new Class<?>[] {String.class, double.class}, times(1)),
9798
Arguments.of("setAttribute", new Class<?>[] {String.class, boolean.class}, times(1)),
99+
Arguments.of("setAttribute", new Class<?>[] {String.class, Value.class}, times(1)),
98100
Arguments.of(
99101
"recordException", new Class<?>[] {Throwable.class, Attributes.class}, times(1)),
100102
Arguments.of("recordException", new Class<?>[] {Throwable.class}, times(1)),

opencensus-shim/src/test/java/io/opentelemetry/opencensusshim/OpenTelemetryNoRecordEventsSpanImplTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import io.opencensus.trace.Tracestate;
3939
import io.opentelemetry.api.common.AttributeKey;
4040
import io.opentelemetry.api.common.Attributes;
41+
import io.opentelemetry.api.common.Value;
4142
import java.util.HashMap;
4243
import java.util.Map;
4344
import java.util.Random;
@@ -93,6 +94,7 @@ public void doNotCrash() {
9394
noRecordEventsSpan.setAttribute("OTelAttributeKeyLong", 123);
9495
noRecordEventsSpan.setAttribute("OTelAttributeKeyDouble", 123.45);
9596
noRecordEventsSpan.setAttribute("OTelAttributeKeyBoolean", true);
97+
noRecordEventsSpan.setAttribute("OTelAttributeKeyValue", Value.empty());
9698
noRecordEventsSpan.addEvent("OTel event 1");
9799
noRecordEventsSpan.addEvent("OTel event 2", 29922310, TimeUnit.HOURS);
98100
noRecordEventsSpan.addEvent(

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
@@ -393,7 +393,7 @@ void setAttribute_nullAttributeValue_afterEnd() {
393393
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
394394
assertThat(span.toSpanData().getAttributes().size()).isEqualTo(10);
395395
span.end();
396-
span.setAttribute("emptyString", null);
396+
span.setAttribute("emptyString", (String) null);
397397
span.setAttribute(stringKey("emptyStringAttributeValue"), null);
398398
span.setAttribute(longKey("longAttribute"), null);
399399
span.setAttribute(booleanKey("boolAttribute"), null);

0 commit comments

Comments
 (0)