|
| 1 | +# better-prometheus |
| 2 | + |
| 3 | +[](https://shepherd.dev/github/danog/better-prometheus) |
| 4 | +[](https://shepherd.dev/github/danog/better-prometheus) |
| 5 | + |
| 6 | + |
| 7 | +A better Prometheus library for PHP applications. |
| 8 | + |
| 9 | +Offers a modern, clean PHP 8.1 API, with support for **default label values**, based on and compatible with the original `promphp/prometheus_client_php` library. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +composer require danog/better-prometheus |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```php |
| 20 | +<?php |
| 21 | + |
| 22 | +require 'vendor/autoload.php'; |
| 23 | + |
| 24 | +use danog\BetterPrometheus\BetterCollectorRegistry; |
| 25 | +use Prometheus\Storage\InMemory; |
| 26 | +use Prometheus\Storage\Redis; |
| 27 | + |
| 28 | +$adapter = new InMemory; |
| 29 | +// Any other promphp adapter may also be used... |
| 30 | +// $adapter = new Redis(); |
| 31 | + |
| 32 | +$registry = new BetterCollectorRegistry($adapter); |
| 33 | + |
| 34 | +// Note the difference with promphp: the labels are keys => values, not just keys. |
| 35 | +$counter = $registry->getOrRegisterCounter( |
| 36 | + 'test', |
| 37 | + 'some_counter', |
| 38 | + 'it increases', |
| 39 | + // Note: these are default label key+values, they will be sent verbatim, no changes |
| 40 | + ['someLabel' => 'defaultValue'] |
| 41 | +); |
| 42 | + |
| 43 | +// Specify some additional labels post-construction like this (both keys and values)... |
| 44 | +$counter->incBy(3, ['type' => 'blue']); |
| 45 | + |
| 46 | +// ...or add some more default labels to the counter, creating a new counter: |
| 47 | +$newCounter = $counter->addLabels(['someOtherLabel' => 'someOtherDefaultValue']); |
| 48 | +assert($newCounter !== $counter); // true |
| 49 | +$counter->incBy(3, ['type' => 'blue']); |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +// Gauges can also be used |
| 54 | +$gauge = $registry->getOrRegisterGauge( |
| 55 | + 'test', |
| 56 | + 'some_gauge', |
| 57 | + 'it sets', |
| 58 | + ['someLabel' => 'defaultValue'] |
| 59 | +); |
| 60 | +$gauge->set(2.5, ['type' => 'blue']); |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +// As well as histograms |
| 65 | +$histogram = $registry->getOrRegisterHistogram( |
| 66 | + 'test', |
| 67 | + 'some_histogram', |
| 68 | + 'it observes', |
| 69 | + ['someLabel' => 'defaultValue'], |
| 70 | + // [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9] |
| 71 | +); |
| 72 | +$histogram->observe(3.5, ['type' => 'blue']); |
| 73 | + |
| 74 | + |
| 75 | +// And suummaries |
| 76 | +$summary = $registry->getOrRegisterSummary( |
| 77 | + 'test', |
| 78 | + 'some_summary', |
| 79 | + 'it observes a sliding window', |
| 80 | + ['someLabel' => 'defaultValue'], |
| 81 | + // 84600, |
| 82 | + // [0.01, 0.05, 0.5, 0.95, 0.99] |
| 83 | +); |
| 84 | + |
| 85 | +$summary->observe(5, ['type' => 'blue']); |
| 86 | +``` |
| 87 | + |
| 88 | +## API documentation |
| 89 | + |
| 90 | +See [here »](https://github.com/danog/better-prometheus/blob/master/docs/docs/index.md) for the full API documentation. |
0 commit comments