previousnext / php-prometheus
A PHP library for serializing to the prometheus text format.
1.3.0
2024-06-04 01:57 UTC
Requires
- php: ^8.1
- ext-json: *
- symfony/property-access: ^5.4|^6.2|^7.1
- symfony/serializer: ^5.4|^6.2|^7.1
Requires (Dev)
- drupal/coder: ^8.3.16
- phpspec/prophecy: ^1.16
- phpunit/phpunit: ^9.5.28
- symfony/phpunit-bridge: ^5.4|^6.2|^7.1
README
A PHP library for serializing to the prometheus text format.
NOTE This library does not keep state. It is intended purely as a serialization library. Therefore, there are no methods into increment or decrement values for metrics, only to set them in order to be serialized.
Installation
composer require previousnext/php-prometheus
Usage
Gauge
$gauge = new Gauge("foo", "bar", "A test gauge"); $gauge->set(100, ['baz' => 'wiz']); $gauge->set(90, ['wobble' => 'wibble', 'bing' => 'bong']); $gauge->set(0); $serializer = MetricSerializerFactory::create(); $output = $serializer->serialize($gauge, 'prometheus');
Expected output:
# HELP foo_bar A test gauge
# TYPE foo_bar gauge
foo_bar{baz="wiz"} 100
foo_bar{wobble="wibble",bing="bong"} 90
foo_bar 0
Counter
$counter = new Counter("foo", "bar", "A counter for testing"); $counter->set(100, ['baz' => 'wiz']); $serializer = MetricSerializerFactory::create(); $output = $serializer->serialize($counter, 'prometheus');
Expected output:
# HELP foo_bar A counter for testing
# TYPE foo_bar counter
foo_bar{baz="wiz"} 100
Summary
$summary = new Summary("foo", "bar", "Summary help text", 'baz'); $buckets = [0, 0.25, 0.5, 0.75, 1]; $values = [2, 4, 6, 8, 10]; $summary->setValues($buckets, $values); $summary->setSum(54321); $summary->setCount(212); $serializer = MetricSerializerFactory::create(); $output = $serializer->serialize($summary, 'prometheus');
Expected output:
# HELP foo_bar Summary help text
# TYPE foo_bar summary
foo_bar{baz="0"} 2
foo_bar{baz="0.25"} 4
foo_bar{baz="0.5"} 6
foo_bar{baz="0.75"} 8
foo_bar{baz="1"} 10
foo_bar_sum 54321
foo_bar_count 212
Developing
PHP CodeSniffer
./bin/phpcs
PHPUnit
./bin/phpunit