This library provides AssertJ support for Prometheus Java Client metrics, which simplifies testing your own (Java) exporters or own (Java) application natively exposing metrics.
Available on Maven Central (GAV: de.m3y.prometheus.assertj:assertj-prometheus:VERSION):
<dependency>
<groupId>de.m3y.prometheus.assertj</groupId>
<artifactId>assertj-prometheus</artifactId>
<version>VERSION</version>
<scope>test</scope>
</dependency>
A basic example for VersionInfoExports:
VersionInfoExports versionInfoExports = new VersionInfoExports();
// Verify jvm_info
MetricFamilySamples mfs = MetricFamilySamplesUtils.getMetricFamilySamples(
versionInfoExports.collect(), "jvm");
assertThat(mfs)
.hasTypeOfInfo()
.hasSampleLabelNames("vendor", "runtime", "version")
.hasSampleValue(
labelValues(
System.getProperty("java.runtime.name", "unknown"),
System.getProperty("java.vm.vendor", "unknown"),
System.getProperty("java.runtime.version", "unknown")
));
For further examples, have a look at the tests.
Helpers for fetching a single MFS:
Collector.MetricFamilySamples mfs;
// From default registry CollectorRegistry.defaultRegistry
mfs = MetricFamilySamplesUtils.getMetricFamilySamples("my_metric");
// From specific registry
mfs = MetricFamilySamplesUtils.getMetricFamilySamples( CollectorRegistry.defaultRegistry, "my_metric");
// From collector aka exporter
VersionInfoExports versionInfoExports = new VersionInfoExports();
mfs = MetricFamilySamplesUtils.getMetricFamilySamples(versionInfoExports.collect(), "jvm");
Example for Info:
Info info = Info.build().name("testInfo").help("help")
.labelNames("version", "vendor")
.create().register();
...
Collector.MetricFamilySamples mfs = MetricFamilySamplesUtils.getMetricFamilySamples("testInfo");
assertThat(mfs)
.hasType(INFO)
.hasTypeOfInfo()
.hasSampleSize(1)
.hasSampleValue("A", "B"); // Checks if label values exist -
// typical values are your build info version
Example for a Gauge or Counter:
Collector.MetricFamilySamples mfs = MetricFamilySamplesUtils.getMetricFamilySamples("my_metric");
assertThat(mfs)
.hasTypeOfGauge() // For a Counter: .hasTypeOfCounter()
.hasSampleLabelNames("job_type", "app_name", "status")
.hasSampleValue(
labelValues("A", "B", "C"),
10d
)
.hasSampleValue(
labelValues("X", "Y", "Z"),
da -> da.isCloseTo(10.0, withinPercentage(10d)) // AssertJ double asserts with 10% tolerance
);
Example for a Summary with sum, count and quantiles:
Collector.MetricFamilySamples mfs = MetricFamilySamplesUtils.getMetricFamilySamples("my_metric");
assertThat(mfs)
.hasSampleSize(12)
.hasSampleLabelNames("label_a")
.hasTypeOfSummary() // Required for following, summary specific asserts
.hasSampleCountValue(labelValues("value_a"), 2)
.hasSampleSumValue(labelValues("value_a"), 40)
.hasSampleValue(0.5 /* Quantile */, 10)
.hasSampleValue(0.9 /* Quantile */, 20)
...
Example for a Histogram with sum, count and buckets:
Collector.MetricFamilySamples mfs = MetricFamilySamplesUtils.getMetricFamilySamples("my_metric");
assertThat(mfs)
.hasSampleSize(12)
.hasSampleLabelNames("label_a")
.hasTypeOfHistogram() // Required for following, histogram specific asserts
.hasSampleCountValue(labelValues("value_a"), 2)
.hasSampleSumValue(labelValues("value_a"), 40)
.hasSampleCountValue(labelValues("value_b"), 2)
.hasSampleSumValue(labelValues("value_b"), 40)
.hasSampleBucketValue(labelValues("value_a"), 10, 1) // Histogram bucket assertions
...
.hasSampleBucketValue(labelValues("value_b"), Double.POSITIVE_INFINITY, 2)
mvn clean install
- JDK 8+
Licensed under Apache 2.0 License
Copyright 2018-2024 Marcel May and project contributors.