Skip to content

Commit 3444294

Browse files
committed
Introduce builder for PrometheusProperties and a failing unit test in case counter exemplars are disabled in the code definition but specifically enabled for the metric in the properties
Signed-off-by: Jens Wilke <signed-off@cruftex.net>
1 parent 68b28f4 commit 3444294

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public static PrometheusProperties get() throws PrometheusPropertiesException {
3636
return instance;
3737
}
3838

39+
public static Builder builder() {
40+
return new Builder();
41+
}
42+
3943
public PrometheusProperties(
4044
MetricsProperties defaultMetricsProperties,
4145
Map<String, MetricsProperties> metricProperties,
@@ -95,4 +99,78 @@ public ExporterPushgatewayProperties getExporterPushgatewayProperties() {
9599
public ExporterOpenTelemetryProperties getExporterOpenTelemetryProperties() {
96100
return exporterOpenTelemetryProperties;
97101
}
102+
103+
public static class Builder {
104+
private MetricsProperties defaultMetricsProperties;
105+
private Map<String, MetricsProperties> metricProperties = new HashMap<>();
106+
private ExemplarsProperties exemplarProperties;
107+
private ExporterProperties exporterProperties;
108+
private ExporterFilterProperties exporterFilterProperties;
109+
private ExporterHttpServerProperties httpServerConfig;
110+
private ExporterPushgatewayProperties pushgatewayProperties;
111+
private ExporterOpenTelemetryProperties otelConfig;
112+
113+
private Builder() {}
114+
115+
public Builder defaultMetricsProperties(MetricsProperties defaultMetricsProperties) {
116+
this.defaultMetricsProperties = defaultMetricsProperties;
117+
return this;
118+
}
119+
120+
public Builder metricProperties(Map<String, MetricsProperties> metricProperties) {
121+
this.metricProperties = metricProperties;
122+
return this;
123+
}
124+
125+
/** Convenience for adding a single named MetricsProperties */
126+
public Builder putMetricProperty(String name, MetricsProperties props) {
127+
this.metricProperties.put(name, props);
128+
return this;
129+
}
130+
131+
public Builder exemplarProperties(ExemplarsProperties exemplarProperties) {
132+
this.exemplarProperties = exemplarProperties;
133+
return this;
134+
}
135+
136+
public Builder exporterProperties(ExporterProperties exporterProperties) {
137+
this.exporterProperties = exporterProperties;
138+
return this;
139+
}
140+
141+
public Builder exporterFilterProperties(ExporterFilterProperties exporterFilterProperties) {
142+
this.exporterFilterProperties = exporterFilterProperties;
143+
return this;
144+
}
145+
146+
public Builder httpServerConfig(ExporterHttpServerProperties httpServerConfig) {
147+
this.httpServerConfig = httpServerConfig;
148+
return this;
149+
}
150+
151+
public Builder pushgatewayProperties(ExporterPushgatewayProperties pushgatewayProperties) {
152+
this.pushgatewayProperties = pushgatewayProperties;
153+
return this;
154+
}
155+
156+
public Builder otelConfig(ExporterOpenTelemetryProperties otelConfig) {
157+
this.otelConfig = otelConfig;
158+
return this;
159+
}
160+
161+
public PrometheusProperties build() {
162+
return new PrometheusProperties(
163+
defaultMetricsProperties,
164+
metricProperties,
165+
exemplarProperties,
166+
exporterProperties,
167+
exporterFilterProperties,
168+
httpServerConfig,
169+
pushgatewayProperties,
170+
otelConfig
171+
);
172+
}
173+
174+
}
175+
98176
}

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CounterTest.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
66
import static org.assertj.core.data.Offset.offset;
77

8+
import io.prometheus.metrics.config.MetricsProperties;
9+
import io.prometheus.metrics.config.PrometheusProperties;
810
import io.prometheus.metrics.core.exemplars.ExemplarSamplerConfigTestUtil;
911
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_31_1.Metrics;
1012
import io.prometheus.metrics.expositionformats.internal.PrometheusProtobufWriterImpl;
@@ -319,8 +321,6 @@ void incWithExemplar2() {
319321
public void testExemplarSamplerDisabled() {
320322
Counter counter =
321323
Counter.builder()
322-
// .withExemplarSampler((inc, prev) -> {throw new RuntimeException("unexpected call to
323-
// exemplar sampler");})
324324
.name("count_total")
325325
.withoutExemplars()
326326
.build();
@@ -330,6 +330,42 @@ public void testExemplarSamplerDisabled() {
330330
assertThat(getData(counter).getExemplar()).isNull();
331331
}
332332

333+
@Test
334+
public void testExemplarSamplerDisabled_enabledByDefault() {
335+
PrometheusProperties properties = PrometheusProperties.builder()
336+
.defaultMetricsProperties(MetricsProperties.builder()
337+
.exemplarsEnabled(true)
338+
.build())
339+
.build();
340+
Counter counter =
341+
Counter.builder(properties)
342+
.name("count_total")
343+
.withoutExemplars()
344+
.build();
345+
counter.incWithExemplar(3.0, Labels.of("a", "b"));
346+
assertThat(getData(counter).getExemplar()).isNull();
347+
counter.inc(2.0);
348+
assertThat(getData(counter).getExemplar()).isNull();
349+
}
350+
351+
@Test
352+
public void testExemplarSamplerDisabledInDefinition_enabledByPropertiesOnMetric_FAILING() {
353+
PrometheusProperties properties = PrometheusProperties.builder()
354+
.putMetricProperty("count", MetricsProperties.builder()
355+
.exemplarsEnabled(true)
356+
.build())
357+
.build();
358+
Counter counter =
359+
Counter.builder(properties)
360+
.name("count_total")
361+
.withoutExemplars()
362+
.build();
363+
counter.incWithExemplar(3.0, Labels.of("a", "b"));
364+
assertThat(getData(counter).getExemplar()).isNull();
365+
counter.inc(2.0);
366+
assertThat(getData(counter).getExemplar()).isNull();
367+
}
368+
333369
@Test
334370
public void testConstLabelsFirst() {
335371
assertThatExceptionOfType(IllegalArgumentException.class)

0 commit comments

Comments
 (0)