From 154915f051f7acb26db500aae38a61b6bd0ebbc2 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Fri, 13 Aug 2021 11:57:35 +0900 Subject: [PATCH] Fix nullability in opentracing-shim. (#3476) --- .../opentelemetry/opentracingshim/Propagation.java | 13 +++++++++---- .../opentracingshim/ScopeManagerShim.java | 3 ++- .../opentracingshim/SpanBuilderShim.java | 9 +++++---- .../opentracingshim/SpanContextShim.java | 3 ++- .../io/opentelemetry/opentracingshim/SpanShim.java | 1 + .../opentelemetry/opentracingshim/TracerShim.java | 3 ++- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index 94969dd78c4..3f27bbeb171 100644 --- a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -60,8 +60,10 @@ static final class TextMapSetter private TextMapSetter() {} @Override - public void set(TextMapInject carrier, String key, String value) { - carrier.put(key, value); + public void set(@Nullable TextMapInject carrier, String key, String value) { + if (carrier != null) { + carrier.put(key, value); + } } } @@ -71,14 +73,17 @@ static final class TextMapGetter implements io.opentelemetry.context.propagation.TextMapGetter> { private TextMapGetter() {} - @Nullable @Override public Iterable keys(Map carrier) { return carrier.keySet(); } @Override - public String get(Map carrier, String key) { + @Nullable + public String get(@Nullable Map carrier, String key) { + if (carrier == null) { + return null; + } for (Map.Entry entry : carrier.entrySet()) { if (key.equalsIgnoreCase(entry.getKey())) { return entry.getValue(); diff --git a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java index adc11188ddf..fa2aed6db2e 100644 --- a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java +++ b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java @@ -9,6 +9,7 @@ import io.opentracing.Scope; import io.opentracing.ScopeManager; import io.opentracing.Span; +import javax.annotation.Nullable; final class ScopeManagerShim extends BaseShimObject implements ScopeManager { public ScopeManagerShim(TelemetryInfo telemetryInfo) { @@ -16,7 +17,7 @@ public ScopeManagerShim(TelemetryInfo telemetryInfo) { } @Override - @SuppressWarnings("ReturnMissingNullable") + @Nullable public Span activeSpan() { SpanShim spanShim = SpanShim.current(); io.opentelemetry.api.trace.Span span = null; diff --git a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java index 011707ae0b0..fe1b90405ac 100644 --- a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java +++ b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java @@ -23,14 +23,15 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; +import javax.annotation.Nullable; final class SpanBuilderShim extends BaseShimObject implements SpanBuilder { private final String spanName; // The parent will be either a Span or a SpanContext. // Inherited baggage is supported only for the main parent. - private SpanShim parentSpan; - private SpanContextShim parentSpanContext; + @Nullable private SpanShim parentSpan; + @Nullable private SpanContextShim parentSpanContext; private boolean ignoreActiveSpan; private final List parentLinks = new ArrayList<>(); @@ -39,7 +40,7 @@ final class SpanBuilderShim extends BaseShimObject implements SpanBuilder { private final List spanBuilderAttributeKeys = new ArrayList<>(); private final List spanBuilderAttributeValues = new ArrayList<>(); - private SpanKind spanKind; + @Nullable private SpanKind spanKind; private boolean error; private long startTimestampMicros; @@ -72,7 +73,7 @@ public SpanBuilder asChildOf(SpanContext parent) { } @Override - public SpanBuilder addReference(String referenceType, SpanContext referencedContext) { + public SpanBuilder addReference(@Nullable String referenceType, SpanContext referencedContext) { if (referencedContext == null) { return this; } diff --git a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java index 62d31fe45a4..c0d9000f514 100644 --- a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java +++ b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; final class SpanContextShim extends BaseShimObject implements SpanContext { @@ -74,7 +75,7 @@ public Iterable> baggageItems() { return items; } - @SuppressWarnings("ReturnMissingNullable") + @Nullable String getBaggageItem(String key) { return baggage.getEntryValue(key); } diff --git a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java index 53a206ee38a..8853737b0bf 100644 --- a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java +++ b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java @@ -54,6 +54,7 @@ io.opentelemetry.api.trace.Span getSpan() { return span; } + @Nullable public static SpanShim current() { return Context.current().get(SPAN_SHIM_KEY); } diff --git a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/TracerShim.java b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/TracerShim.java index 66080728517..27d61129976 100644 --- a/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/TracerShim.java +++ b/opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/TracerShim.java @@ -15,6 +15,7 @@ import io.opentracing.propagation.TextMapInject; import java.util.logging.Level; import java.util.logging.Logger; +import javax.annotation.Nullable; final class TracerShim extends BaseShimObject implements Tracer { private static final Logger logger = Logger.getLogger(TracerShim.class.getName()); @@ -69,8 +70,8 @@ public void inject(SpanContext context, Format format, C carrier) { } } - @SuppressWarnings("ReturnMissingNullable") @Override + @Nullable public SpanContext extract(Format format, C carrier) { try { if (format == Format.Builtin.TEXT_MAP