Skip to content

Commit d13f04d

Browse files
authored
Clarify that AttributesBuilder.put allows nulls (#7271)
1 parent 14e2fef commit d13f04d

File tree

14 files changed

+44
-25
lines changed

14 files changed

+44
-25
lines changed

api/all/src/main/java/io/opentelemetry/api/common/ArrayBackedAttributesBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Arrays;
1010
import java.util.List;
1111
import java.util.function.Predicate;
12+
import javax.annotation.Nullable;
1213

1314
class ArrayBackedAttributesBuilder implements AttributesBuilder {
1415
private final List<Object> data;
@@ -37,7 +38,7 @@ public <T> AttributesBuilder put(AttributeKey<Long> key, int value) {
3738
}
3839

3940
@Override
40-
public <T> AttributesBuilder put(AttributeKey<T> key, T value) {
41+
public <T> AttributesBuilder put(AttributeKey<T> key, @Nullable T value) {
4142
if (key == null || key.getKey().isEmpty() || value == null) {
4243
return this;
4344
}

api/all/src/main/java/io/opentelemetry/api/common/AttributesBuilder.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Arrays;
1919
import java.util.List;
2020
import java.util.function.Predicate;
21+
import javax.annotation.Nullable;
2122

2223
/** A builder of {@link Attributes} supporting an arbitrary number of key-value pairs. */
2324
public interface AttributesBuilder {
@@ -35,18 +36,22 @@ public interface AttributesBuilder {
3536
// version.
3637
<T> AttributesBuilder put(AttributeKey<Long> key, int value);
3738

38-
/** Puts a {@link AttributeKey} with associated value into this. */
39-
<T> AttributesBuilder put(AttributeKey<T> key, T value);
39+
/**
40+
* Puts an {@link AttributeKey} with an associated value into this if the value is non-null.
41+
* Providing a null value does not remove or unset previously set values.
42+
*/
43+
<T> AttributesBuilder put(AttributeKey<T> key, @Nullable T value);
4044

4145
/**
42-
* Puts a String attribute into this.
46+
* Puts a String attribute into this if the value is non-null. Providing a null value does not
47+
* remove or unset previously set values.
4348
*
4449
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
4550
* your keys, if possible.
4651
*
4752
* @return this Builder
4853
*/
49-
default AttributesBuilder put(String key, String value) {
54+
default AttributesBuilder put(String key, @Nullable String value) {
5055
return put(stringKey(key), value);
5156
}
5257

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.opentelemetry.context.Context;
1111
import java.time.Instant;
1212
import java.util.concurrent.TimeUnit;
13+
import javax.annotation.Nullable;
1314

1415
class DefaultLogger implements Logger {
1516

@@ -77,7 +78,7 @@ public LogRecordBuilder setBody(Value<?> body) {
7778
}
7879

7980
@Override
80-
public <T> LogRecordBuilder setAttribute(AttributeKey<T> key, T value) {
81+
public <T> LogRecordBuilder setAttribute(AttributeKey<T> key, @Nullable T value) {
8182
return this;
8283
}
8384

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.opentelemetry.context.Context;
1717
import java.time.Instant;
1818
import java.util.concurrent.TimeUnit;
19+
import javax.annotation.Nullable;
1920

2021
/**
2122
* Used to construct and emit log records from a {@link Logger}.
@@ -107,16 +108,20 @@ default LogRecordBuilder setAllAttributes(Attributes attributes) {
107108
* Sets an attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained a
108109
* mapping for the key, the old value is replaced by the specified value.
109110
*
111+
* <p>Note: Providing a null value is a no-op and will not remove previously set values.
112+
*
110113
* @param key the key for this attribute.
111114
* @param value the value for this attribute.
112115
* @return this.
113116
*/
114-
<T> LogRecordBuilder setAttribute(AttributeKey<T> key, T value);
117+
<T> LogRecordBuilder setAttribute(AttributeKey<T> key, @Nullable T value);
115118

116119
/**
117120
* Sets a String attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained
118121
* a mapping for the key, the old value is replaced by the specified value.
119122
*
123+
* <p>Note: Providing a null value is a no-op and will not remove previously set values.
124+
*
120125
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
121126
* pre-allocate your keys, if possible.
122127
*
@@ -125,7 +130,7 @@ default LogRecordBuilder setAllAttributes(Attributes attributes) {
125130
* @return this.
126131
* @since 1.48.0
127132
*/
128-
default LogRecordBuilder setAttribute(String key, String value) {
133+
default LogRecordBuilder setAttribute(String key, @Nullable String value) {
129134
return setAttribute(stringKey(key), value);
130135
}
131136

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.opentelemetry.api.common.AttributeKey;
99
import io.opentelemetry.api.common.Attributes;
1010
import java.util.concurrent.TimeUnit;
11+
import javax.annotation.Nullable;
1112
import javax.annotation.concurrent.Immutable;
1213

1314
/**
@@ -42,7 +43,7 @@ private PropagatedSpan(SpanContext spanContext) {
4243
}
4344

4445
@Override
45-
public Span setAttribute(String key, String value) {
46+
public Span setAttribute(String key, @Nullable String value) {
4647
return this;
4748
}
4849

@@ -62,7 +63,7 @@ public Span setAttribute(String key, boolean value) {
6263
}
6364

6465
@Override
65-
public <T> Span setAttribute(AttributeKey<T> key, T value) {
66+
public <T> Span setAttribute(AttributeKey<T> key, @Nullable T value) {
6667
return this;
6768
}
6869

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ static Span wrap(SpanContext spanContext) {
8888
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
8989
* the key, the old value is replaced by the specified value.
9090
*
91-
* <p>Empty String "" and null are valid attribute {@code value}, but not valid keys.
91+
* <p>Empty String "" and null are valid attribute {@code value}s, but not valid keys.
92+
*
93+
* <p>Note: Providing a null value is a no-op and will not remove previously set values.
9294
*
9395
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
9496
* pre-allocate your keys, if possible.
@@ -97,7 +99,7 @@ static Span wrap(SpanContext spanContext) {
9799
* @param value the value for this attribute.
98100
* @return this.
99101
*/
100-
default Span setAttribute(String key, String value) {
102+
default Span setAttribute(String key, @Nullable String value) {
101103
return setAttribute(AttributeKey.stringKey(key), value);
102104
}
103105

@@ -150,13 +152,13 @@ default Span setAttribute(String key, boolean value) {
150152
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
151153
* the key, the old value is replaced by the specified value.
152154
*
153-
* <p>Note: the behavior of null values is undefined, and hence strongly discouraged.
155+
* <p>Note: Providing a null value is a no-op.
154156
*
155157
* @param key the key for this attribute.
156158
* @param value the value for this attribute.
157159
* @return this.
158160
*/
159-
<T> Span setAttribute(AttributeKey<T> key, T value);
161+
<T> Span setAttribute(AttributeKey<T> key, @Nullable T value);
160162

161163
/**
162164
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for

api/all/src/test/java/io/opentelemetry/api/common/AttributesTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Map;
2727
import java.util.NoSuchElementException;
2828
import java.util.concurrent.atomic.AtomicBoolean;
29+
import javax.annotation.Nullable;
2930
import org.junit.jupiter.api.Test;
3031

3132
/** Unit tests for {@link Attributes}s. */
@@ -565,7 +566,7 @@ public <T> AttributesBuilder put(AttributeKey<Long> key, int value) {
565566
}
566567

567568
@Override
568-
public <T> AttributesBuilder put(AttributeKey<T> key, T value) {
569+
public <T> AttributesBuilder put(AttributeKey<T> key, @Nullable T value) {
569570
return null;
570571
}
571572

api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.opentelemetry.context.Context;
1414
import java.time.Instant;
1515
import java.util.concurrent.TimeUnit;
16+
import javax.annotation.Nullable;
1617

1718
class ExtendedDefaultLogger implements ExtendedLogger {
1819

@@ -51,7 +52,7 @@ public <T> ExtendedLogRecordBuilder setAttribute(ExtendedAttributeKey<T> key, T
5152
}
5253

5354
@Override
54-
public <T> ExtendedLogRecordBuilder setAttribute(AttributeKey<T> key, T value) {
55+
public <T> ExtendedLogRecordBuilder setAttribute(AttributeKey<T> key, @Nullable T value) {
5556
return this;
5657
}
5758

api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedLogRecordBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.opentelemetry.context.Context;
1616
import java.time.Instant;
1717
import java.util.concurrent.TimeUnit;
18+
import javax.annotation.Nullable;
1819

1920
/** Extended {@link LogRecordBuilder} with experimental APIs. */
2021
public interface ExtendedLogRecordBuilder extends LogRecordBuilder {
@@ -110,7 +111,7 @@ default ExtendedLogRecordBuilder setAllAttributes(ExtendedAttributes attributes)
110111
* attribute APIs.
111112
*/
112113
@Override
113-
<T> ExtendedLogRecordBuilder setAttribute(AttributeKey<T> key, T value);
114+
<T> ExtendedLogRecordBuilder setAttribute(AttributeKey<T> key, @Nullable T value);
114115

115116
/**
116117
* Set an attribute.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.opentelemetry.context.Scope;
1616
import java.time.Instant;
1717
import java.util.concurrent.TimeUnit;
18+
import javax.annotation.Nullable;
1819

1920
/**
2021
* Delegates <i>all</i> {@link Span} methods to some underlying Span via {@link
@@ -52,12 +53,12 @@ default boolean isRecording() {
5253
}
5354

5455
@Override
55-
default <T> Span setAttribute(AttributeKey<T> key, T value) {
56+
default <T> Span setAttribute(AttributeKey<T> key, @Nullable T value) {
5657
return getDelegate().setAttribute(key, value);
5758
}
5859

5960
@Override
60-
default Span setAttribute(String key, String value) {
61+
default Span setAttribute(String key, @Nullable String value) {
6162
return getDelegate().setAttribute(key, value);
6263
}
6364

0 commit comments

Comments
 (0)