Skip to content

Commit bfb2d2a

Browse files
add Nullaway to instrumentation API (#14458)
Co-authored-by: otelbot <[email protected]>
1 parent 9281486 commit bfb2d2a

File tree

24 files changed

+101
-43
lines changed

24 files changed

+101
-43
lines changed

conventions/src/main/kotlin/otel.nullaway-conventions.gradle.kts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,19 @@ nullaway {
1818

1919
tasks {
2020
withType<JavaCompile>().configureEach {
21-
if (!name.contains("test", ignoreCase = true)) {
21+
if (name.contains("test", ignoreCase = true)) {
22+
options.errorprone.nullaway {
23+
enabled = false
24+
}
25+
} else {
2226
options.errorprone.nullaway {
2327
severity.set(CheckSeverity.ERROR)
2428
}
2529
}
30+
options.errorprone.nullaway {
31+
customInitializerAnnotations.add("org.openjdk.jmh.annotations.Setup")
32+
excludedFieldAnnotations.add("org.mockito.Mock")
33+
excludedFieldAnnotations.add("org.mockito.InjectMocks")
34+
}
2635
}
2736
}

instrumentation-api/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
id("otel.japicmp-conventions")
88
id("otel.publish-conventions")
99
id("otel.jmh-conventions")
10+
id("otel.nullaway-conventions")
1011
}
1112

1213
group = "io.opentelemetry.instrumentation"

instrumentation-api/src/jmh/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBenchmark.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
@State(Scope.Thread)
3434
public class InstrumenterBenchmark {
3535

36-
private static final Instrumenter<Void, Void> INSTRUMENTER =
37-
Instrumenter.<Void, Void>builder(
36+
private static final Object REQUEST = new Object();
37+
38+
private static final Instrumenter<Object, Void> INSTRUMENTER =
39+
Instrumenter.<Object, Void>builder(
3840
OpenTelemetry.noop(),
3941
"benchmark",
4042
HttpSpanNameExtractor.create(ConstantHttpAttributesGetter.INSTANCE))
@@ -44,75 +46,76 @@ public class InstrumenterBenchmark {
4446

4547
@Benchmark
4648
public Context start() {
47-
return INSTRUMENTER.start(Context.root(), null);
49+
return INSTRUMENTER.start(Context.root(), REQUEST);
4850
}
4951

5052
@Benchmark
5153
public Context startEnd() {
52-
Context context = INSTRUMENTER.start(Context.root(), null);
53-
INSTRUMENTER.end(context, null, null, null);
54+
Context context = INSTRUMENTER.start(Context.root(), REQUEST);
55+
INSTRUMENTER.end(context, REQUEST, null, null);
5456
return context;
5557
}
5658

57-
enum ConstantHttpAttributesGetter implements HttpClientAttributesGetter<Void, Void> {
59+
enum ConstantHttpAttributesGetter implements HttpClientAttributesGetter<Object, Void> {
5860
INSTANCE;
5961

6062
private static final InetSocketAddress PEER_ADDRESS =
6163
InetSocketAddress.createUnresolved("localhost", 8080);
6264

6365
@Override
64-
public String getUrlFull(Void unused) {
66+
public String getUrlFull(Object unused) {
6567
return "https://opentelemetry.io/benchmark";
6668
}
6769

6870
@Override
69-
public String getHttpRequestMethod(Void unused) {
71+
public String getHttpRequestMethod(Object unused) {
7072
return "GET";
7173
}
7274

7375
@Override
74-
public List<String> getHttpRequestHeader(Void unused, String name) {
76+
public List<String> getHttpRequestHeader(Object unused, String name) {
7577
if (name.equalsIgnoreCase("user-agent")) {
7678
return Collections.singletonList("OpenTelemetryBot");
7779
}
7880
return Collections.emptyList();
7981
}
8082

8183
@Override
82-
public Integer getHttpResponseStatusCode(Void unused, Void unused2, @Nullable Throwable error) {
84+
public Integer getHttpResponseStatusCode(
85+
Object unused, Void unused2, @Nullable Throwable error) {
8386
return 200;
8487
}
8588

8689
@Override
87-
public List<String> getHttpResponseHeader(Void unused, Void unused2, String name) {
90+
public List<String> getHttpResponseHeader(Object unused, Void unused2, String name) {
8891
return Collections.emptyList();
8992
}
9093

9194
@Override
92-
public String getNetworkProtocolName(Void unused, @Nullable Void unused2) {
95+
public String getNetworkProtocolName(Object unused, @Nullable Void unused2) {
9396
return "http";
9497
}
9598

9699
@Override
97-
public String getNetworkProtocolVersion(Void unused, @Nullable Void unused2) {
100+
public String getNetworkProtocolVersion(Object unused, @Nullable Void unused2) {
98101
return "2.0";
99102
}
100103

101104
@Nullable
102105
@Override
103-
public String getServerAddress(Void request) {
106+
public String getServerAddress(Object request) {
104107
return null;
105108
}
106109

107110
@Nullable
108111
@Override
109-
public Integer getServerPort(Void request) {
112+
public Integer getServerPort(Object request) {
110113
return null;
111114
}
112115

113116
@Override
114117
public InetSocketAddress getNetworkPeerInetSocketAddress(
115-
Void request, @Nullable Void response) {
118+
Object request, @Nullable Void response) {
116119
return PEER_ADDRESS;
117120
}
118121
}

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/UnsafeAttributes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.HashMap;
1313
import java.util.Map;
1414
import java.util.function.BiConsumer;
15+
import javax.annotation.Nullable;
1516

1617
/**
1718
* The {@link AttributesBuilder} and {@link Attributes} used by the instrumentation API. We are able
@@ -29,6 +30,7 @@ final class UnsafeAttributes extends HashMap<AttributeKey<?>, Object>
2930

3031
@SuppressWarnings("unchecked")
3132
@Override
33+
@Nullable
3234
public <T> T get(AttributeKey<T> key) {
3335
return (T) super.get(key);
3436
}
@@ -62,7 +64,7 @@ public <T> AttributesBuilder put(AttributeKey<Long> key, int value) {
6264

6365
@Override
6466
@CanIgnoreReturnValue
65-
public <T> AttributesBuilder put(AttributeKey<T> key, T value) {
67+
public <T> AttributesBuilder put(AttributeKey<T> key, @Nullable T value) {
6668
super.put(key, value);
6769
return this;
6870
}

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/ContextPropagationDebug.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public final class ContextPropagationDebug {
4545
private final Context sourceContext;
4646
private final List<Propagation> locations;
4747
// context after adding debug locations
48-
private Context wrappedContext;
48+
@Nullable private Context wrappedContext;
4949

5050
private ContextPropagationDebug(Context sourceContext) {
5151
this.sourceContext = sourceContext;

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/HttpProtocolUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
*/
1414
public final class HttpProtocolUtil {
1515

16+
@Nullable
1617
public static String getProtocol(@Nullable String protocol) {
1718
if (protocol != null && protocol.startsWith("HTTP/")) {
1819
return "http";
1920
}
2021
return null;
2122
}
2223

24+
@Nullable
2325
public static String getVersion(@Nullable String protocol) {
2426
if (protocol != null && protocol.startsWith("HTTP/")) {
2527
return normalizeHttpVersion(protocol.substring("HTTP/".length()));

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/HttpRouteState.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public static HttpRouteState create(
4040

4141
// this method is used reflectively from InstrumentationApiContextBridging
4242
public static HttpRouteState create(
43-
@Nullable String method, @Nullable String route, int updatedBySourceOrder, Span span) {
43+
@Nullable String method,
44+
@Nullable String route,
45+
int updatedBySourceOrder,
46+
@Nullable Span span) {
4447
return new HttpRouteState(method, route, updatedBySourceOrder, span);
4548
}
4649

@@ -50,7 +53,10 @@ public static HttpRouteState create(
5053
@Nullable private volatile Span span;
5154

5255
private HttpRouteState(
53-
@Nullable String method, @Nullable String route, int updatedBySourceOrder, Span span) {
56+
@Nullable String method,
57+
@Nullable String route,
58+
int updatedBySourceOrder,
59+
@Nullable Span span) {
5460
this.method = method;
5561
this.updatedBySourceOrder = updatedBySourceOrder;
5662
this.route = route;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.api.internal;
7+
8+
/**
9+
* See <a href="https://github.com/uber/NullAway/wiki/Supported-Annotations#initialization">NullAway
10+
* spec</a>
11+
*
12+
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
13+
* at any time.
14+
*/
15+
public @interface Initializer {}

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/InstrumenterUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ public final class InstrumenterUtil {
2424
private static InstrumenterAccess instrumenterAccess;
2525
private static InstrumenterBuilderAccess instrumenterBuilderAccess;
2626

27+
@Initializer
2728
public static void setInstrumenterAccess(InstrumenterAccess instrumenterAccess) {
2829
InstrumenterUtil.instrumenterAccess = instrumenterAccess;
2930
}
3031

32+
@Initializer
3133
public static void setInstrumenterBuilderAccess(
3234
InstrumenterBuilderAccess instrumenterBuilderAccess) {
3335
InstrumenterUtil.instrumenterBuilderAccess = instrumenterBuilderAccess;

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/cache/WeakLockFreeCache.java

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

88
import io.opentelemetry.instrumentation.api.internal.cache.weaklockfree.WeakConcurrentMap;
99
import java.util.function.Function;
10+
import javax.annotation.Nullable;
1011

1112
final class WeakLockFreeCache<K, V> implements Cache<K, V> {
1213

@@ -22,6 +23,7 @@ public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction
2223
}
2324

2425
@Override
26+
@Nullable
2527
public V get(K key) {
2628
return delegate.getIfPresent(key);
2729
}

0 commit comments

Comments
 (0)