Skip to content

Commit 697effc

Browse files
committed
nullaway
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent db0fc2b commit 697effc

File tree

14 files changed

+118
-86
lines changed

14 files changed

+118
-86
lines changed

prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.SynchronousQueue;
2222
import java.util.concurrent.ThreadPoolExecutor;
2323
import java.util.concurrent.TimeUnit;
24+
import javax.annotation.Nullable;
2425
import javax.security.auth.Subject;
2526

2627
/**
@@ -54,9 +55,9 @@ private HTTPServer(
5455
ExecutorService executorService,
5556
HttpServer httpServer,
5657
PrometheusRegistry registry,
57-
Authenticator authenticator,
58-
String authenticatedSubjectAttributeName,
59-
HttpHandler defaultHandler) {
58+
@Nullable Authenticator authenticator,
59+
@Nullable String authenticatedSubjectAttributeName,
60+
@Nullable HttpHandler defaultHandler) {
6061
if (httpServer.getAddress() == null) {
6162
throw new IllegalArgumentException("HttpServer hasn't been bound to an address");
6263
}
@@ -88,14 +89,17 @@ private HTTPServer(
8889
}
8990

9091
private void registerHandler(
91-
String path, HttpHandler handler, Authenticator authenticator, String subjectAttributeName) {
92+
String path,
93+
HttpHandler handler,
94+
@Nullable Authenticator authenticator,
95+
@Nullable String subjectAttributeName) {
9296
HttpContext context = server.createContext(path, wrapWithDoAs(handler, subjectAttributeName));
9397
if (authenticator != null) {
9498
context.setAuthenticator(authenticator);
9599
}
96100
}
97101

98-
private HttpHandler wrapWithDoAs(HttpHandler handler, String subjectAttributeName) {
102+
private HttpHandler wrapWithDoAs(HttpHandler handler, @Nullable String subjectAttributeName) {
99103
if (subjectAttributeName == null) {
100104
return handler;
101105
}
@@ -169,15 +173,15 @@ public static Builder builder(PrometheusProperties config) {
169173
public static class Builder {
170174

171175
private final PrometheusProperties config;
172-
private Integer port = null;
173-
private String hostname = null;
174-
private InetAddress inetAddress = null;
175-
private ExecutorService executorService = null;
176-
private PrometheusRegistry registry = null;
177-
private Authenticator authenticator = null;
178-
private HttpsConfigurator httpsConfigurator = null;
179-
private HttpHandler defaultHandler = null;
180-
private String authenticatedSubjectAttributeName = null;
176+
@Nullable private Integer port = null;
177+
@Nullable private String hostname = null;
178+
@Nullable private InetAddress inetAddress = null;
179+
@Nullable private ExecutorService executorService = null;
180+
@Nullable private PrometheusRegistry registry = null;
181+
@Nullable private Authenticator authenticator = null;
182+
@Nullable private HttpsConfigurator httpsConfigurator = null;
183+
@Nullable private HttpHandler defaultHandler = null;
184+
@Nullable private String authenticatedSubjectAttributeName = null;
181185

182186
private Builder(PrometheusProperties config) {
183187
this.config = config;
@@ -313,7 +317,7 @@ private int findPort() {
313317
return 0; // random port will be selected
314318
}
315319

316-
private void assertNull(Object o, String msg) {
320+
private void assertNull(@Nullable Object o, String msg) {
317321
if (o != null) {
318322
throw new IllegalStateException(msg);
319323
}

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/OpenTelemetryExporter.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.prometheus.metrics.model.registry.PrometheusRegistry;
66
import java.util.HashMap;
77
import java.util.Map;
8+
import javax.annotation.Nullable;
89

910
public class OpenTelemetryExporter implements AutoCloseable {
1011
private final MetricReader reader;
@@ -29,16 +30,16 @@ public static Builder builder(PrometheusProperties config) {
2930
public static class Builder {
3031

3132
private final PrometheusProperties config;
32-
private PrometheusRegistry registry = null;
33-
String protocol;
34-
String endpoint;
33+
@Nullable private PrometheusRegistry registry = null;
34+
@Nullable String protocol;
35+
@Nullable String endpoint;
3536
final Map<String, String> headers = new HashMap<>();
36-
String interval;
37-
String timeout;
38-
String serviceName;
39-
String serviceNamespace;
40-
String serviceInstanceId;
41-
String serviceVersion;
37+
@Nullable String interval;
38+
@Nullable String timeout;
39+
@Nullable String serviceName;
40+
@Nullable String serviceNamespace;
41+
@Nullable String serviceInstanceId;
42+
@Nullable String serviceVersion;
4243
final Map<String, String> resourceAttributes = new HashMap<>();
4344

4445
private Builder(PrometheusProperties config) {

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/OtelAutoConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.prometheus.metrics.exporter.opentelemetry;
22

3+
import static java.util.Objects.requireNonNull;
4+
35
import io.opentelemetry.api.common.Attributes;
46
import io.opentelemetry.api.common.AttributesBuilder;
57
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
@@ -35,7 +37,7 @@ static MetricReader createReader(
3537
config.getExporterOpenTelemetryProperties(),
3638
instrumentationScopeInfo);
3739

38-
MetricReader reader = readerRef.get();
40+
MetricReader reader = requireNonNull(readerRef.get());
3941
reader.register(
4042
new PrometheusMetricProducer(registry, instrumentationScopeInfo, getResourceField(sdk)));
4143
return reader;

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusMetricProducer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.Collection;
2424
import java.util.List;
25+
import javax.annotation.Nullable;
2526

2627
class PrometheusMetricProducer implements CollectionRegistration {
2728

@@ -93,6 +94,7 @@ private Resource resourceFromTargetInfo(MetricSnapshots snapshots) {
9394
return result.build();
9495
}
9596

97+
@Nullable
9698
private InstrumentationScopeInfo instrumentationScopeFromOtelScopeInfo(
9799
MetricSnapshots snapshots) {
98100
for (MetricSnapshot snapshot : snapshots) {
@@ -125,7 +127,7 @@ private InstrumentationScopeInfo instrumentationScopeFromOtelScopeInfo(
125127
return null;
126128
}
127129

128-
private void addUnlessNull(List<MetricData> result, MetricData data) {
130+
private void addUnlessNull(List<MetricData> result, @Nullable MetricData data) {
129131
if (data != null) {
130132
result.add(data);
131133
}

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PropertiesResourceProvider.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
import io.opentelemetry.sdk.resources.Resource;
66
import java.util.HashMap;
77
import java.util.Map;
8+
import javax.annotation.Nullable;
89

910
final class PropertiesResourceProvider {
1011

1112
static Resource mergeResource(
1213
Map<String, String> resourceAttributes,
13-
String serviceName,
14-
String serviceNamespace,
15-
String serviceInstanceId,
16-
String serviceVersion) {
14+
@Nullable String serviceName,
15+
@Nullable String serviceNamespace,
16+
@Nullable String serviceInstanceId,
17+
@Nullable String serviceVersion) {
1718
Map<String, String> resource = new HashMap<>(resourceAttributes);
1819
if (serviceName != null) {
1920
resource.put("service.name", serviceName);

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PropertyMapper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.HashMap;
77
import java.util.Map;
88
import java.util.function.Function;
9+
import javax.annotation.Nullable;
910

1011
class PropertyMapper {
1112

@@ -29,7 +30,8 @@ static PropertyMapper create(
2930
.addString(builder.serviceName, properties.getServiceName(), "otel.service.name");
3031
}
3132

32-
PropertyMapper addString(String builderValue, String propertyValue, String otelKey) {
33+
PropertyMapper addString(
34+
@Nullable String builderValue, @Nullable String propertyValue, String otelKey) {
3335
if (builderValue != null) {
3436
// the low priority config should not be used for the metrics settings, so that both general
3537
// and metrics settings
@@ -42,6 +44,7 @@ PropertyMapper addString(String builderValue, String propertyValue, String otelK
4244
return this;
4345
}
4446

47+
@Nullable
4548
private static String mapToOtelString(Map<String, String> map) {
4649
if (map.isEmpty()) {
4750
return null;

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/MetricDataFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
1111
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
1212
import io.prometheus.metrics.model.snapshots.UnknownSnapshot;
13+
import javax.annotation.Nullable;
1314

1415
public class MetricDataFactory {
1516

@@ -42,6 +43,7 @@ public MetricData create(GaugeSnapshot snapshot) {
4243
resource);
4344
}
4445

46+
@Nullable
4547
public MetricData create(HistogramSnapshot snapshot) {
4648
if (!snapshot.getDataPoints().isEmpty()) {
4749
HistogramSnapshot.HistogramDataPointSnapshot firstDataPoint = snapshot.getDataPoints().get(0);

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusClassicHistogram.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Objects;
1313
import java.util.stream.Collectors;
14+
import javax.annotation.Nullable;
1415

1516
class PrometheusClassicHistogram extends PrometheusData<HistogramPointData>
1617
implements HistogramData {
@@ -36,6 +37,7 @@ public Collection<HistogramPointData> getPoints() {
3637
return points;
3738
}
3839

40+
@Nullable
3941
private HistogramPointData toOtelDataPoint(
4042
HistogramSnapshot.HistogramDataPointSnapshot dataPoint, long currentTimeMillis) {
4143
if (!dataPoint.hasClassicHistogramData()) {

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusData.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.concurrent.TimeUnit;
2121
import java.util.stream.Collectors;
2222
import java.util.stream.StreamSupport;
23+
import javax.annotation.Nullable;
2324

2425
abstract class PrometheusData<T extends PointData> implements Data<T> {
2526

@@ -45,7 +46,7 @@ protected Attributes labelsToAttributes(Labels labels) {
4546
}
4647
}
4748

48-
protected List<DoubleExemplarData> convertExemplar(Exemplar exemplar) {
49+
protected List<DoubleExemplarData> convertExemplar(@Nullable Exemplar exemplar) {
4950
if (exemplar == null) {
5051
return Collections.emptyList();
5152
}
@@ -58,7 +59,8 @@ protected List<DoubleExemplarData> convertExemplars(Exemplars exemplars) {
5859
.collect(Collectors.toList());
5960
}
6061

61-
protected DoubleExemplarData toDoubleExemplarData(Exemplar exemplar) {
62+
@Nullable
63+
protected DoubleExemplarData toDoubleExemplarData(@Nullable Exemplar exemplar) {
6264
if (exemplar == null) {
6365
return null;
6466
}

prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusMetricData.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import io.opentelemetry.sdk.resources.Resource;
99
import io.prometheus.metrics.model.snapshots.MetricMetadata;
1010
import io.prometheus.metrics.model.snapshots.Unit;
11+
import javax.annotation.Nullable;
1112

1213
class PrometheusMetricData<T extends PrometheusData<?>> implements MetricData {
1314

1415
private final Resource resource;
1516
private final InstrumentationScopeInfo instrumentationScopeInfo;
1617
private final String name;
17-
private final String description;
18-
private final String unit;
18+
@Nullable private final String description;
19+
@Nullable private final String unit;
1920
T data;
2021

2122
PrometheusMetricData(
@@ -48,7 +49,8 @@ private String getNameWithoutUnit(MetricMetadata metricMetadata) {
4849

4950
// See
5051
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/6cf4dec6cb42d87d8840e9f67d4acf66d4eb8fda/pkg/translator/prometheus/normalize_name.go#L19
51-
static String convertUnit(Unit unit) {
52+
@Nullable
53+
static String convertUnit(@Nullable Unit unit) {
5254
if (unit == null) {
5355
return null;
5456
}
@@ -129,11 +131,13 @@ public String getName() {
129131
}
130132

131133
@Override
134+
@Nullable
132135
public String getDescription() {
133136
return description;
134137
}
135138

136139
@Override
140+
@Nullable
137141
public String getUnit() {
138142
return unit;
139143
}

0 commit comments

Comments
 (0)