Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep EOF at end of OpenMetrics output #7982

Merged
merged 1 commit into from
Nov 10, 2023
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 @@ -131,10 +131,10 @@ public Optional<Object> format() {
return Optional.empty();
}

String prometheusOutput = filter(prometheusMeterRegistry.get()
.scrape(MicrometerPrometheusFormatter.MEDIA_TYPE_TO_FORMAT.get(
resultMediaType),
meterNamesOfInterest));
String prometheusOutput = prometheusMeterRegistry.get()
.scrape(MicrometerPrometheusFormatter.MEDIA_TYPE_TO_FORMAT.get(
resultMediaType),
meterNamesOfInterest);

return prometheusOutput.isBlank() ? Optional.empty() : Optional.of(prometheusOutput);
}
Expand Down Expand Up @@ -218,16 +218,6 @@ Set<String> meterNamesOfInterest(PrometheusMeterRegistry prometheusMeterRegistry
return result;
}

/**
* Filter the Prometheus-format report.
*
* @param output Prometheus-format report
* @return output filtered
*/
private static String filter(String output) {
return output.replaceFirst("# EOF\r?\n?", "");
}

private static Optional<PrometheusMeterRegistry> prometheusMeterRegistry(MeterRegistry meterRegistry) {
io.micrometer.core.instrument.MeterRegistry mMeterRegistry =
meterRegistry.unwrap(io.micrometer.core.instrument.MeterRegistry.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;

import io.helidon.common.media.type.MediaTypes;
import io.helidon.common.testing.junit5.OptionalMatcher;
import io.helidon.metrics.api.Counter;
import io.helidon.metrics.api.MeterRegistry;
Expand All @@ -36,13 +37,21 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

class TestPrometheusFormatting {

private static final String SCOPE_TAG_NAME = "this-scope";

/*
Only OpenMetrics format, not the Prometheus exposition format, has the trailing EOF which (for example) the Prometheus
server expects to see when it probes targets. That's why, below, when we format the output, we specify OpenMetrics as the
media type.
*/
private static final String OPENMETRICS_EOF = "# EOF\n";
private static MeterRegistry meterRegistry;

private static MetricsConfig metricsConfig;
Expand Down Expand Up @@ -87,13 +96,15 @@ void testRetrievingAll() {
e.record(2, TimeUnit.SECONDS);

MicrometerPrometheusFormatter formatter = MicrometerPrometheusFormatter.builder(meterRegistry)
.resultMediaType(MediaTypes.APPLICATION_OPENMETRICS_TEXT)
.scopeTagName(SCOPE_TAG_NAME)
.build();
Optional<Object> outputOpt = formatter.format();

assertThat("Formatted output",
checkAndCast(outputOpt),
allOf(containsString(scopeExpr("c1_total",
allOf(
containsString(scopeExpr("c1_total",
"this_scope",
"app",
"1.0")),
Expand All @@ -108,7 +119,9 @@ void testRetrievingAll() {
containsString(scopeExpr("t1_1_seconds_count",
"this_scope",
"app",
"1.0"))));
"1.0")),
endsWith(OPENMETRICS_EOF)));

}

@Test
Expand All @@ -122,6 +135,7 @@ void testRetrievingByName() {
d.record(7, TimeUnit.SECONDS);

MicrometerPrometheusFormatter formatter = MicrometerPrometheusFormatter.builder(meterRegistry)
.resultMediaType(MediaTypes.APPLICATION_OPENMETRICS_TEXT)
.scopeTagName(SCOPE_TAG_NAME)
.meterNameSelection(Set.of("c2"))
.build();
Expand All @@ -139,7 +153,8 @@ void testRetrievingByName() {
"1.0"))),
not(containsString(scopeExpr("t2_seconds_sum",
"this_scope",
"app", "7.0")))));
"app", "7.0"))),
endsWith(OPENMETRICS_EOF)));

}

Expand All @@ -159,6 +174,7 @@ void testRetrievingByScope() {
e.record(2, TimeUnit.SECONDS);

MicrometerPrometheusFormatter formatter = MicrometerPrometheusFormatter.builder(meterRegistry)
.resultMediaType(MediaTypes.APPLICATION_OPENMETRICS_TEXT)
.scopeTagName(SCOPE_TAG_NAME)
.scopeSelection(Set.of("app"))
.build();
Expand All @@ -182,11 +198,12 @@ void testRetrievingByScope() {
containsString(scopeExpr("t3_1_seconds_count",
"this_scope",
"app",
"1.0"))));
"1.0")),
endsWith(OPENMETRICS_EOF)));
}

private static String scopeExpr(String meterName, String key, String value, String suffix) {
return meterName + "{" + key + "=\"" + value + "\",} " + suffix;
return meterName + "{" + key + "=\"" + value + "\"} " + suffix;
}

private static String checkAndCast(Optional<Object> outputOpt) {
Expand Down