Code Monkey home page Code Monkey logo

assertj-prometheus-metrics's Introduction

AssertJ support for Prometheus Metrics

Maven Central javadoc

This library provides AssertJ support for Prometheus Java Client metrics, which simplifies testing your own (Java) exporters or own (Java) application natively exposing metrics.

Download

Available on Maven Central (GAV: de.m3y.prometheus.assertj:assertj-prometheus:0.5). Add to your POM:

<dependency>
    <groupId>de.m3y.prometheus.assertj</groupId>
    <artifactId>assertj-prometheus</artifactId>
    <version>0.6</version>
    <scope>test</scope>
</dependency>

Examples

A very simple 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

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");

Info

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 

Counter or Gauge

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
        );

Summary

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)
        ...

Histogram

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)

Building

mvn clean install

Requirements

  • JDK 8+

License and Copyright

Licensed under Apache 2.0 License

Copyright 2018-2022 Marcel May and project contributors.

assertj-prometheus-metrics's People

Contributors

dependabot[bot] avatar marcelmay avatar

Stargazers

 avatar

Watchers

 avatar  avatar

assertj-prometheus-metrics's Issues

Update dependencies and add Double.NaN /assertj 3.15 workaround

Compile scope:

  • io.prometheus:simpleclient .................... 0.6.0 -> 0.8.1
  • io.prometheus:simpleclient_hotspot ...... 0.6.0 -> 0.8.1
  • org.assertj:assertj-core ......................... 3.13.2 -> 3.15.0
    Workaround when comparing NaN doubles for changed assertj behavior
    (see assertj/assertj#1783 )

Test scope:

  • junit:junit ............................. 4.12 -> 4.13

Support info type metrics

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-
                                   // typically values are your build info version 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.