feat: Add OpenMetrics2 enabled flag by zeitlinger · Pull Request #1953 · prometheus/client_java · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ public Builder exporterOpenTelemetryProperties(
}

public Builder enableOpenMetrics2(Consumer<OpenMetrics2Properties.Builder> configurator) {
OpenMetrics2Properties.Builder openMetrics2Builder = OpenMetrics2Properties.builder();
OpenMetrics2Properties.Builder openMetrics2Builder =
OpenMetrics2Properties.builder().enabled(true);
configurator.accept(openMetrics2Builder);
this.openMetrics2Properties = openMetrics2Builder.build();
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ void load() {
load(
new HashMap<>(
Map.of(
"io.prometheus.openmetrics2.enabled",
"true",
"io.prometheus.openmetrics2.content_negotiation",
"true",
"io.prometheus.openmetrics2.composite_values",
Expand All @@ -23,6 +25,7 @@ void load() {
"true",
"io.prometheus.openmetrics2.native_histograms",
"true")));
assertThat(properties.getEnabled()).isTrue();
assertThat(properties.getContentNegotiation()).isTrue();
assertThat(properties.getCompositeValues()).isTrue();
assertThat(properties.getExemplarCompliance()).isTrue();
Expand All @@ -31,6 +34,11 @@ void load() {

@Test
void loadInvalidValue() {
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(
() -> load(new HashMap<>(Map.of("io.prometheus.openmetrics2.enabled", "invalid"))))
.withMessage(
"io.prometheus.openmetrics2.enabled: Expecting 'true' or 'false'. Found: invalid");
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(
() ->
Expand Down Expand Up @@ -79,11 +87,13 @@ private static OpenMetrics2Properties load(Map<String, String> map) {
void builder() {
OpenMetrics2Properties properties =
OpenMetrics2Properties.builder()
.enabled(true)
.contentNegotiation(true)
.compositeValues(false)
.exemplarCompliance(true)
.nativeHistograms(false)
.build();
assertThat(properties.getEnabled()).isTrue();
assertThat(properties.getContentNegotiation()).isTrue();
assertThat(properties.getCompositeValues()).isFalse();
assertThat(properties.getExemplarCompliance()).isTrue();
Expand All @@ -93,6 +103,7 @@ void builder() {
@Test
void builderEnableAll() {
OpenMetrics2Properties properties = OpenMetrics2Properties.builder().enableAll().build();
assertThat(properties.getEnabled()).isTrue();
assertThat(properties.getContentNegotiation()).isTrue();
assertThat(properties.getCompositeValues()).isTrue();
assertThat(properties.getExemplarCompliance()).isTrue();
Expand All @@ -102,6 +113,7 @@ void builderEnableAll() {
@Test
void defaultValues() {
OpenMetrics2Properties properties = OpenMetrics2Properties.builder().build();
assertThat(properties.getEnabled()).isFalse();
assertThat(properties.getContentNegotiation()).isFalse();
assertThat(properties.getCompositeValues()).isFalse();
assertThat(properties.getExemplarCompliance()).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ public ExpositionFormatWriter findWriter(@Nullable String acceptHeader) {
}

private boolean isOpenMetrics2Enabled() {
OpenMetrics2Properties props = openMetrics2TextFormatWriter.getOpenMetrics2Properties();
return props.getContentNegotiation()
|| props.getCompositeValues()
|| props.getExemplarCompliance()
|| props.getNativeHistograms();
return openMetrics2TextFormatWriter.getOpenMetrics2Properties().getEnabled();
}

public PrometheusProtobufWriter getPrometheusProtobufWriter() {
Expand Down
Loading