Add nullability annotations to module/spring-boot-tracing

See gh-46587
This commit is contained in:
Moritz Halbritter
2025-08-06 10:55:00 +02:00
parent 598ee985bf
commit 89a5bfd607
16 changed files with 60 additions and 25 deletions
@@ -31,6 +31,7 @@ import brave.propagation.CurrentTraceContext.ScopeDecorator;
import brave.propagation.Propagation;
import brave.propagation.Propagation.Factory;
import io.micrometer.tracing.brave.bridge.BraveBaggageManager;
import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProperty;
@@ -103,7 +104,7 @@ class BravePropagationConfigurations {
return new Factory() {
@Override
public Propagation<String> get() {
public @Nullable Propagation<String> get() {
return null;
}
@@ -29,6 +29,7 @@ import brave.propagation.TraceContext;
import brave.propagation.TraceContextOrSamplingFlags;
import io.micrometer.tracing.BaggageManager;
import io.micrometer.tracing.brave.bridge.W3CPropagation;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.tracing.autoconfigure.TracingProperties.Propagation.PropagationType;
@@ -107,8 +108,8 @@ class CompositePropagationFactory extends Propagation.Factory {
* @param localFields the local fields, or {@code null}
* @return the {@link CompositePropagationFactory}
*/
static CompositePropagationFactory create(TracingProperties.Propagation properties, BaggageManager baggageManager,
LocalBaggageFields localFields) {
static CompositePropagationFactory create(TracingProperties.Propagation properties,
@Nullable BaggageManager baggageManager, @Nullable LocalBaggageFields localFields) {
PropagationFactoryMapper mapper = new PropagationFactoryMapper(baggageManager, localFields);
List<Factory> injectors = properties.getEffectiveProducedTypes().stream().map(mapper::map).toList();
List<Factory> extractors = properties.getEffectiveConsumedTypes().stream().map(mapper::map).toList();
@@ -121,11 +122,11 @@ class CompositePropagationFactory extends Propagation.Factory {
*/
private static class PropagationFactoryMapper {
private final BaggageManager baggageManager;
private final @Nullable BaggageManager baggageManager;
private final LocalBaggageFields localFields;
PropagationFactoryMapper(BaggageManager baggageManager, LocalBaggageFields localFields) {
PropagationFactoryMapper(@Nullable BaggageManager baggageManager, @Nullable LocalBaggageFields localFields) {
this.baggageManager = baggageManager;
this.localFields = (localFields != null) ? localFields : LocalBaggageFields.empty();
}
@@ -32,6 +32,7 @@ import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.tracing.autoconfigure.TracingProperties.Propagation.PropagationType;
@@ -48,7 +49,7 @@ class CompositeTextMapPropagator implements TextMapPropagator {
private final Collection<TextMapPropagator> extractors;
private final TextMapPropagator baggagePropagator;
private final @Nullable TextMapPropagator baggagePropagator;
private final Set<String> fields;
@@ -61,7 +62,7 @@ class CompositeTextMapPropagator implements TextMapPropagator {
* @param baggagePropagator the baggage propagator to use, or {@code null}
*/
CompositeTextMapPropagator(Collection<TextMapPropagator> injectors,
Collection<TextMapPropagator> mutuallyExclusiveExtractors, TextMapPropagator baggagePropagator) {
Collection<TextMapPropagator> mutuallyExclusiveExtractors, @Nullable TextMapPropagator baggagePropagator) {
this.injectors = injectors;
this.extractors = mutuallyExclusiveExtractors;
this.baggagePropagator = baggagePropagator;
@@ -92,14 +93,14 @@ class CompositeTextMapPropagator implements TextMapPropagator {
}
@Override
public <C> void inject(Context context, C carrier, TextMapSetter<C> setter) {
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
if (context != null && setter != null) {
this.injectors.forEach((injector) -> injector.inject(context, carrier, setter));
}
}
@Override
public <C> Context extract(Context context, C carrier, TextMapGetter<C> getter) {
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
if (context == null) {
return Context.root();
}
@@ -123,7 +124,8 @@ class CompositeTextMapPropagator implements TextMapPropagator {
* @param baggagePropagator the baggage propagator to use, or {@code null}
* @return the {@link CompositeTextMapPropagator}
*/
static TextMapPropagator create(TracingProperties.Propagation properties, TextMapPropagator baggagePropagator) {
static TextMapPropagator create(TracingProperties.Propagation properties,
@Nullable TextMapPropagator baggagePropagator) {
TextMapPropagatorMapper mapper = new TextMapPropagatorMapper(baggagePropagator != null);
List<TextMapPropagator> injectors = properties.getEffectiveProducedTypes()
.stream()
@@ -16,6 +16,8 @@
package org.springframework.boot.tracing.autoconfigure;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.logging.LoggingSystem;
@@ -63,7 +65,7 @@ class LogCorrelationEnvironmentPostProcessor implements EnvironmentPostProcessor
}
@Override
public Object getProperty(String name) {
public @Nullable Object getProperty(String name) {
if (name.equals(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY)) {
return this.environment.getProperty("management.tracing.enabled", Boolean.class, Boolean.TRUE);
}
@@ -18,6 +18,8 @@ package org.springframework.boot.tracing.autoconfigure;
import java.util.Map;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
@@ -59,8 +61,9 @@ class OnEnabledTracingCondition extends SpringBootCondition {
.because("tracing is enabled by default"));
}
private static String getExporterName(AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnEnabledTracing.class.getName());
private static @Nullable String getExporterName(AnnotatedTypeMetadata metadata) {
Map<String, @Nullable Object> attributes = metadata
.getAnnotationAttributes(ConditionalOnEnabledTracing.class.getName());
if (attributes == null) {
return null;
}
@@ -24,6 +24,7 @@ import io.micrometer.tracing.otel.bridge.OtelTracer.EventPublisher;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextStorage;
import io.opentelemetry.context.Scope;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationContext;
@@ -122,7 +123,7 @@ public class OpenTelemetryEventPublisherBeansApplicationListener implements Gene
private final MultiValueMap<ApplicationContext, EventPublishingContextWrapper> beans = new LinkedMultiValueMap<>();
private volatile ContextStorage storageDelegate;
private volatile @Nullable ContextStorage storageDelegate;
private Wrapper() {
}
@@ -181,7 +182,7 @@ public class OpenTelemetryEventPublisherBeansApplicationListener implements Gene
}
@Override
public Context current() {
public @Nullable Context current() {
return getDelegate().current();
}
@@ -20,6 +20,8 @@ import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -204,7 +206,7 @@ public class TracingProperties {
* Setting this property overrides the more fine-grained propagation type
* properties.
*/
private List<PropagationType> type;
private @Nullable List<PropagationType> type;
/**
* Tracing context propagation types produced by the application.
@@ -216,7 +218,7 @@ public class TracingProperties {
*/
private List<PropagationType> consume = List.of(PropagationType.values());
public void setType(List<PropagationType> type) {
public void setType(@Nullable List<PropagationType> type) {
this.type = type;
}
@@ -228,7 +230,7 @@ public class TracingProperties {
this.consume = consume;
}
public List<PropagationType> getType() {
public @Nullable List<PropagationType> getType() {
return this.type;
}
@@ -67,7 +67,9 @@ final class OtlpTracingConfigurations {
Assert.state(transport == this.properties.getTransport(),
"Requested transport %s doesn't match configured transport %s".formatted(transport,
this.properties.getTransport()));
return this.properties.getEndpoint();
String endpoint = this.properties.getEndpoint();
Assert.state(endpoint != null, "'endpoint' must not be null");
return endpoint;
}
}
@@ -20,6 +20,8 @@ import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -34,7 +36,7 @@ public class OtlpTracingProperties {
/**
* URL to the OTel collector's HTTP API.
*/
private String endpoint;
private @Nullable String endpoint;
/**
* Call timeout for the OTel Collector to process an exported batch of data. This
@@ -64,11 +66,11 @@ public class OtlpTracingProperties {
*/
private Map<String, String> headers = new HashMap<>();
public String getEndpoint() {
public @Nullable String getEndpoint() {
return this.endpoint;
}
public void setEndpoint(String endpoint) {
public void setEndpoint(@Nullable String endpoint) {
this.endpoint = endpoint;
}
@@ -17,4 +17,7 @@
/**
* Auto-configuration for exporting traces with OTLP.
*/
@NullMarked
package org.springframework.boot.tracing.autoconfigure.otlp;
import org.jspecify.annotations.NullMarked;
@@ -17,4 +17,7 @@
/**
* Auto-configuration for Micrometer Tracing.
*/
@NullMarked
package org.springframework.boot.tracing.autoconfigure;
import org.jspecify.annotations.NullMarked;
@@ -19,6 +19,7 @@ package org.springframework.boot.tracing.autoconfigure.prometheus;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.Tracer;
import io.prometheus.metrics.tracer.common.SpanContext;
import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
@@ -64,13 +65,13 @@ public final class PrometheusExemplarsAutoConfiguration {
}
@Override
public String getCurrentTraceId() {
public @Nullable String getCurrentTraceId() {
Span currentSpan = currentSpan();
return (currentSpan != null) ? currentSpan.context().traceId() : null;
}
@Override
public String getCurrentSpanId() {
public @Nullable String getCurrentSpanId() {
Span currentSpan = currentSpan();
return (currentSpan != null) ? currentSpan.context().spanId() : null;
}
@@ -89,7 +90,7 @@ public final class PrometheusExemplarsAutoConfiguration {
public void markCurrentSpanAsExemplar() {
}
private Span currentSpan() {
private @Nullable Span currentSpan() {
return this.tracer.obtain().currentSpan();
}
@@ -17,4 +17,7 @@
/**
* Auto-configuration for Prometheus Exemplars with Micrometer Tracing.
*/
@NullMarked
package org.springframework.boot.tracing.autoconfigure.prometheus;
import org.jspecify.annotations.NullMarked;
@@ -17,4 +17,7 @@
/**
* Auto-configuration for tracing with Zipkin.
*/
@NullMarked
package org.springframework.boot.tracing.autoconfigure.zipkin;
import org.jspecify.annotations.NullMarked;
@@ -17,4 +17,7 @@
/**
* Support for Docker Compose OpenTelemetry tracing service connections.
*/
@NullMarked
package org.springframework.boot.tracing.docker.compose.otlp;
import org.jspecify.annotations.NullMarked;
@@ -17,4 +17,7 @@
/**
* Support for Testcontainers OpenTelemetry tracing service connections.
*/
@NullMarked
package org.springframework.boot.tracing.testcontainers.otlp;
import org.jspecify.annotations.NullMarked;