prom unit parsing impl
This commit is contained in:
parent
13a55821f4
commit
7c3fb18b79
2 changed files with 37 additions and 3 deletions
|
@ -25,19 +25,27 @@ public class PrometheusEndpointParser {
|
|||
|
||||
String name;
|
||||
String help;
|
||||
Type type = null;
|
||||
Type type;
|
||||
String unit;
|
||||
Set<String> allowedNames = new HashSet<>();
|
||||
List<Sample> samples = new ArrayList<>();
|
||||
|
||||
void registerAndReset() {
|
||||
if (!samples.isEmpty()) {
|
||||
registered.add(
|
||||
new MetricFamilySamples(name, type, Optional.ofNullable(help).orElse(name), List.copyOf(samples)));
|
||||
new MetricFamilySamples(
|
||||
name,
|
||||
Optional.ofNullable(unit).orElse(""),
|
||||
type,
|
||||
Optional.ofNullable(help).orElse(name),
|
||||
List.copyOf(samples))
|
||||
);
|
||||
}
|
||||
//resetting state:
|
||||
name = null;
|
||||
help = null;
|
||||
type = null;
|
||||
unit = null;
|
||||
allowedNames.clear();
|
||||
samples.clear();
|
||||
}
|
||||
|
@ -60,6 +68,7 @@ public class PrometheusEndpointParser {
|
|||
switch (parts[1]) {
|
||||
case "HELP" -> processHelp(context, parts);
|
||||
case "TYPE" -> processType(context, parts);
|
||||
case "UNIT" -> processUnit(context, parts);
|
||||
default -> { /* probably a comment */ }
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +79,19 @@ public class PrometheusEndpointParser {
|
|||
return context.getRegistered();
|
||||
}
|
||||
|
||||
private static void processUnit(ParserContext context, String[] parts) {
|
||||
if (!parts[2].equals(context.name)) {
|
||||
// starting new metric family - need to register (if possible) prev one
|
||||
context.registerAndReset();
|
||||
context.name = parts[2];
|
||||
context.type = DEFAULT_TYPE;
|
||||
context.allowedNames.add(context.name);
|
||||
}
|
||||
if (parts.length == 4) {
|
||||
context.unit = parts[3];
|
||||
}
|
||||
}
|
||||
|
||||
private static void processHelp(ParserContext context, String[] parts) {
|
||||
if (!parts[2].equals(context.name)) {
|
||||
// starting new metric family - need to register (if possible) prev one
|
||||
|
|
|
@ -47,6 +47,11 @@ class PrometheusEndpointParserTest {
|
|||
# TYPE something_untyped untyped
|
||||
something_untyped{} -123123
|
||||
|
||||
# TYPE unit_test_seconds counter
|
||||
# UNIT unit_test_seconds seconds
|
||||
# HELP unit_test_seconds Testing that unit parsed properly
|
||||
unit_test_seconds_total 4.20072246e+06
|
||||
|
||||
# HELP http_request_duration_seconds A histogram of the request duration.
|
||||
# TYPE http_request_duration_seconds histogram
|
||||
http_request_duration_seconds_bucket{le="0.05"} 24054
|
||||
|
@ -58,7 +63,7 @@ class PrometheusEndpointParserTest {
|
|||
http_request_duration_seconds_sum 53423
|
||||
http_request_duration_seconds_count 144320
|
||||
""";
|
||||
var parsed = parse(expose.lines());
|
||||
List<MetricFamilySamples> parsed = parse(expose.lines());
|
||||
assertThat(parsed).contains(
|
||||
new MetricFamilySamples(
|
||||
"http_requests_total",
|
||||
|
@ -87,6 +92,13 @@ class PrometheusEndpointParserTest {
|
|||
"something_untyped",
|
||||
List.of(new Sample("something_untyped", List.of(), List.of(), -123123))
|
||||
),
|
||||
new MetricFamilySamples(
|
||||
"unit_test_seconds",
|
||||
"seconds",
|
||||
Type.COUNTER,
|
||||
"Testing that unit parsed properly",
|
||||
List.of(new Sample("unit_test_seconds_total", List.of(), List.of(), 4.20072246e+06))
|
||||
),
|
||||
new MetricFamilySamples(
|
||||
"http_request_duration_seconds",
|
||||
Type.HISTOGRAM,
|
||||
|
|
Loading…
Add table
Reference in a new issue