Skip to content

Commit 0f378df

Browse files
committed
properties should never be null
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 07c54b8 commit 0f378df

File tree

2 files changed

+71
-41
lines changed

2 files changed

+71
-41
lines changed

prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterPushgatewayProperties.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,41 @@ static ExporterPushgatewayProperties load(Map<Object, Object> properties)
5858
}
5959
return new ExporterPushgatewayProperties(address, job, scheme);
6060
}
61+
62+
public static Builder builder() {
63+
return new Builder();
64+
}
65+
66+
public static class Builder {
67+
@Nullable private String address;
68+
@Nullable private String job;
69+
@Nullable private String scheme;
70+
@Nullable private EscapingScheme escapingScheme;
71+
72+
private Builder() {}
73+
74+
public Builder address(String address) {
75+
this.address = address;
76+
return this;
77+
}
78+
79+
public Builder job(String job) {
80+
this.job = job;
81+
return this;
82+
}
83+
84+
public Builder scheme(String scheme) {
85+
this.scheme = scheme;
86+
return this;
87+
}
88+
89+
public Builder escapingScheme(EscapingScheme escapingScheme) {
90+
this.escapingScheme = escapingScheme;
91+
return this;
92+
}
93+
94+
public ExporterPushgatewayProperties build() {
95+
return new ExporterPushgatewayProperties(address, job, scheme, escapingScheme);
96+
}
97+
}
6198
}

prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusProperties.java

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5-
import javax.annotation.Nullable;
65

76
/**
87
* The Prometheus Java client library can be configured at runtime (e.g. using a properties file).
@@ -13,14 +12,14 @@ public class PrometheusProperties {
1312

1413
private static final PrometheusProperties instance = PrometheusPropertiesLoader.load();
1514

16-
@Nullable private final MetricsProperties defaultMetricsProperties;
15+
private final MetricsProperties defaultMetricsProperties;
1716
private final Map<String, MetricsProperties> metricProperties = new HashMap<>();
18-
@Nullable private final ExemplarsProperties exemplarProperties;
19-
@Nullable private final ExporterProperties exporterProperties;
20-
@Nullable private final ExporterFilterProperties exporterFilterProperties;
21-
@Nullable private final ExporterHttpServerProperties exporterHttpServerProperties;
22-
@Nullable private final ExporterOpenTelemetryProperties exporterOpenTelemetryProperties;
23-
@Nullable private final ExporterPushgatewayProperties exporterPushgatewayProperties;
17+
private final ExemplarsProperties exemplarProperties;
18+
private final ExporterProperties exporterProperties;
19+
private final ExporterFilterProperties exporterFilterProperties;
20+
private final ExporterHttpServerProperties exporterHttpServerProperties;
21+
private final ExporterOpenTelemetryProperties exporterOpenTelemetryProperties;
22+
private final ExporterPushgatewayProperties exporterPushgatewayProperties;
2423

2524
/**
2625
* Get the properties instance. When called for the first time, {@code get()} loads the properties
@@ -42,14 +41,14 @@ public static Builder builder() {
4241
}
4342

4443
public PrometheusProperties(
45-
@Nullable MetricsProperties defaultMetricsProperties,
44+
MetricsProperties defaultMetricsProperties,
4645
Map<String, MetricsProperties> metricProperties,
47-
@Nullable ExemplarsProperties exemplarProperties,
48-
@Nullable ExporterProperties exporterProperties,
49-
@Nullable ExporterFilterProperties exporterFilterProperties,
50-
@Nullable ExporterHttpServerProperties httpServerConfig,
51-
@Nullable ExporterPushgatewayProperties pushgatewayProperties,
52-
@Nullable ExporterOpenTelemetryProperties otelConfig) {
46+
ExemplarsProperties exemplarProperties,
47+
ExporterProperties exporterProperties,
48+
ExporterFilterProperties exporterFilterProperties,
49+
ExporterHttpServerProperties httpServerConfig,
50+
ExporterPushgatewayProperties pushgatewayProperties,
51+
ExporterOpenTelemetryProperties otelConfig) {
5352
this.defaultMetricsProperties = defaultMetricsProperties;
5453
this.metricProperties.putAll(metricProperties);
5554
this.exemplarProperties = exemplarProperties;
@@ -64,7 +63,6 @@ public PrometheusProperties(
6463
* The default metric properties apply for metrics where {@link #getMetricProperties(String)} is
6564
* {@code null}.
6665
*/
67-
@Nullable
6866
public MetricsProperties getDefaultMetricProperties() {
6967
return defaultMetricsProperties;
7068
}
@@ -74,54 +72,51 @@ public MetricsProperties getDefaultMetricProperties() {
7472
* #getDefaultMetricProperties()}. May return {@code null} if no metric-specific properties are
7573
* configured for a metric name.
7674
*/
77-
@Nullable
7875
public MetricsProperties getMetricProperties(String metricName) {
7976
return metricProperties.get(metricName.replace(".", "_"));
8077
}
8178

82-
@Nullable
8379
public ExemplarsProperties getExemplarProperties() {
8480
return exemplarProperties;
8581
}
8682

87-
@Nullable
8883
public ExporterProperties getExporterProperties() {
8984
return exporterProperties;
9085
}
9186

92-
@Nullable
9387
public ExporterFilterProperties getExporterFilterProperties() {
9488
return exporterFilterProperties;
9589
}
9690

97-
@Nullable
9891
public ExporterHttpServerProperties getExporterHttpServerProperties() {
9992
return exporterHttpServerProperties;
10093
}
10194

102-
@Nullable
10395
public ExporterPushgatewayProperties getExporterPushgatewayProperties() {
10496
return exporterPushgatewayProperties;
10597
}
10698

107-
@Nullable
10899
public ExporterOpenTelemetryProperties getExporterOpenTelemetryProperties() {
109100
return exporterOpenTelemetryProperties;
110101
}
111102

112103
public static class Builder {
113-
@Nullable private MetricsProperties defaultMetricsProperties;
104+
private MetricsProperties defaultMetricsProperties = MetricsProperties.builder().build();
114105
private Map<String, MetricsProperties> metricProperties = new HashMap<>();
115-
@Nullable private ExemplarsProperties exemplarProperties;
116-
@Nullable private ExporterProperties exporterProperties;
117-
@Nullable private ExporterFilterProperties exporterFilterProperties;
118-
@Nullable private ExporterHttpServerProperties exporterHttpServerProperties;
119-
@Nullable private ExporterPushgatewayProperties pushgatewayProperties;
120-
@Nullable private ExporterOpenTelemetryProperties otelConfig;
106+
private ExemplarsProperties exemplarProperties = ExemplarsProperties.builder().build();
107+
private ExporterProperties exporterProperties = ExporterProperties.builder().build();
108+
private ExporterFilterProperties exporterFilterProperties =
109+
ExporterFilterProperties.builder().build();
110+
private ExporterHttpServerProperties exporterHttpServerProperties =
111+
ExporterHttpServerProperties.builder().build();
112+
private ExporterPushgatewayProperties pushGatewayProperties =
113+
ExporterPushgatewayProperties.builder().build();
114+
private ExporterOpenTelemetryProperties otelConfig =
115+
ExporterOpenTelemetryProperties.builder().build();
121116

122117
private Builder() {}
123118

124-
public Builder defaultMetricsProperties(@Nullable MetricsProperties defaultMetricsProperties) {
119+
public Builder defaultMetricsProperties(MetricsProperties defaultMetricsProperties) {
125120
this.defaultMetricsProperties = defaultMetricsProperties;
126121
return this;
127122
}
@@ -137,36 +132,34 @@ public Builder putMetricProperty(String name, MetricsProperties props) {
137132
return this;
138133
}
139134

140-
public Builder exemplarProperties(@Nullable ExemplarsProperties exemplarProperties) {
135+
public Builder exemplarProperties(ExemplarsProperties exemplarProperties) {
141136
this.exemplarProperties = exemplarProperties;
142137
return this;
143138
}
144139

145-
public Builder exporterProperties(@Nullable ExporterProperties exporterProperties) {
140+
public Builder exporterProperties(ExporterProperties exporterProperties) {
146141
this.exporterProperties = exporterProperties;
147142
return this;
148143
}
149144

150-
public Builder exporterFilterProperties(
151-
@Nullable ExporterFilterProperties exporterFilterProperties) {
145+
public Builder exporterFilterProperties(ExporterFilterProperties exporterFilterProperties) {
152146
this.exporterFilterProperties = exporterFilterProperties;
153147
return this;
154148
}
155149

156150
public Builder exporterHttpServerProperties(
157-
@Nullable ExporterHttpServerProperties exporterHttpServerProperties) {
151+
ExporterHttpServerProperties exporterHttpServerProperties) {
158152
this.exporterHttpServerProperties = exporterHttpServerProperties;
159153
return this;
160154
}
161155

162-
public Builder pushgatewayProperties(
163-
@Nullable ExporterPushgatewayProperties pushgatewayProperties) {
164-
this.pushgatewayProperties = pushgatewayProperties;
156+
public Builder pushgatewayProperties(ExporterPushgatewayProperties pushgatewayProperties) {
157+
this.pushGatewayProperties = pushgatewayProperties;
165158
return this;
166159
}
167160

168161
public Builder exporterOpenTelemetryProperties(
169-
@Nullable ExporterOpenTelemetryProperties exporterOpenTelemetryProperties) {
162+
ExporterOpenTelemetryProperties exporterOpenTelemetryProperties) {
170163
this.otelConfig = exporterOpenTelemetryProperties;
171164
return this;
172165
}
@@ -179,7 +172,7 @@ public PrometheusProperties build() {
179172
exporterProperties,
180173
exporterFilterProperties,
181174
exporterHttpServerProperties,
182-
pushgatewayProperties,
175+
pushGatewayProperties,
183176
otelConfig);
184177
}
185178
}

0 commit comments

Comments
 (0)