mirror of
https://github.com/danog/better-prometheus.git
synced 2024-11-26 20:34:38 +01:00
Finalize
This commit is contained in:
parent
348ac83ed0
commit
a0e2110249
4
.github/workflows/main.yml
vendored
4
.github/workflows/main.yml
vendored
@ -37,6 +37,10 @@ jobs:
|
|||||||
key: ${{ matrix.os }}-composer-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
|
key: ${{ matrix.os }}-composer-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
|
||||||
restore-keys: ${{ matrix.os }}-composer-${{ matrix.php-versions }}-
|
restore-keys: ${{ matrix.os }}-composer-${{ matrix.php-versions }}-
|
||||||
|
|
||||||
|
- name: Run composer
|
||||||
|
run: |
|
||||||
|
composer update
|
||||||
|
|
||||||
- name: Run codestyle check
|
- name: Run codestyle check
|
||||||
env:
|
env:
|
||||||
PHP_CS_FIXER_IGNORE_ENV: 1
|
PHP_CS_FIXER_IGNORE_ENV: 1
|
||||||
|
71
README.md
71
README.md
@ -14,6 +14,77 @@ Offers a modern, clean PHP 8.1 API, with support for **default label values**, b
|
|||||||
composer require danog/better-prometheus
|
composer require danog/better-prometheus
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
|
use danog\BetterPrometheus\BetterCollectorRegistry;
|
||||||
|
use Prometheus\Storage\InMemory;
|
||||||
|
use Prometheus\Storage\Redis;
|
||||||
|
|
||||||
|
$adapter = new InMemory;
|
||||||
|
// Any other promphp adapter may also be used...
|
||||||
|
// $adapter = new Redis();
|
||||||
|
|
||||||
|
$registry = new BetterCollectorRegistry($adapter);
|
||||||
|
|
||||||
|
// Note the difference with promphp: the labels are keys => values, not just keys.
|
||||||
|
$counter = $registry->getOrRegisterCounter(
|
||||||
|
'test',
|
||||||
|
'some_counter',
|
||||||
|
'it increases',
|
||||||
|
// Note: these are default label key+values, they will be sent verbatim, no changes
|
||||||
|
['someLabel' => 'defaultValue']
|
||||||
|
);
|
||||||
|
|
||||||
|
// Specify some additional labels post-construction like this (both keys and values)...
|
||||||
|
$counter->incBy(3, ['type' => 'blue']);
|
||||||
|
|
||||||
|
// ...or add some more default labels to the counter, creating a new counter:
|
||||||
|
$newCounter = $counter->addLabels(['someOtherLabel' => 'someOtherDefaultValue']);
|
||||||
|
assert($newCounter !== $counter); // true
|
||||||
|
$counter->incBy(3, ['type' => 'blue']);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Gauges can also be used
|
||||||
|
$gauge = $registry->getOrRegisterGauge(
|
||||||
|
'test',
|
||||||
|
'some_gauge',
|
||||||
|
'it sets',
|
||||||
|
['someLabel' => 'defaultValue']
|
||||||
|
);
|
||||||
|
$gauge->set(2.5, ['type' => 'blue']);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// As well as histograms
|
||||||
|
$histogram = $registry->getOrRegisterHistogram(
|
||||||
|
'test',
|
||||||
|
'some_histogram',
|
||||||
|
'it observes',
|
||||||
|
['someLabel' => 'defaultValue'],
|
||||||
|
// [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]
|
||||||
|
);
|
||||||
|
$histogram->observe(3.5, ['type' => 'blue']);
|
||||||
|
|
||||||
|
|
||||||
|
// And suummaries
|
||||||
|
$summary = $registry->getOrRegisterSummary(
|
||||||
|
'test',
|
||||||
|
'some_summary',
|
||||||
|
'it observes a sliding window',
|
||||||
|
['someLabel' => 'defaultValue'],
|
||||||
|
// 84600,
|
||||||
|
// [0.01, 0.05, 0.5, 0.95, 0.99]
|
||||||
|
);
|
||||||
|
|
||||||
|
$summary->observe(5, ['type' => 'blue']);
|
||||||
|
```
|
||||||
|
|
||||||
## API documentation
|
## API documentation
|
||||||
|
|
||||||
See [here »](https://github.com/danog/better-prometheus/blob/master/docs/docs/index.md) for the full API documentation.
|
See [here »](https://github.com/danog/better-prometheus/blob/master/docs/docs/index.md) for the full API documentation.
|
@ -45,9 +45,9 @@ abstract class BetterCollector
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance of this collector, with these additional labels.
|
* Create a new instance of this collector, with these additional labels.
|
||||||
*
|
*
|
||||||
* @param array<string, string> $labels
|
* @param array<string, string> $labels
|
||||||
*/
|
*/
|
||||||
abstract public function addLabels(array $labels): static;
|
abstract public function addLabels(array $labels): static;
|
||||||
|
@ -10,6 +10,8 @@ use Prometheus\MetricFamilySamples;
|
|||||||
use Prometheus\Storage\Adapter;
|
use Prometheus\Storage\Adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* A better collector registry.
|
||||||
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
final class BetterCollectorRegistry
|
final class BetterCollectorRegistry
|
||||||
@ -47,7 +49,6 @@ final class BetterCollectorRegistry
|
|||||||
public readonly Adapter $storageAdapter,
|
public readonly Adapter $storageAdapter,
|
||||||
bool $registerDefaultMetrics = true
|
bool $registerDefaultMetrics = true
|
||||||
) {
|
) {
|
||||||
$this->storageAdapter = $storageAdapter;
|
|
||||||
if ($registerDefaultMetrics) {
|
if ($registerDefaultMetrics) {
|
||||||
$this->defaultGauge = $this->getOrRegisterGauge(
|
$this->defaultGauge = $this->getOrRegisterGauge(
|
||||||
"",
|
"",
|
||||||
@ -69,7 +70,7 @@ final class BetterCollectorRegistry
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @psalm-suppress TooManyArguments
|
* @psalm-suppress TooManyArguments
|
||||||
*
|
*
|
||||||
* @return list<MetricFamilySamples>
|
* @return list<MetricFamilySamples>
|
||||||
*/
|
*/
|
||||||
public function getMetricFamilySamples(bool $sortMetrics = true): array
|
public function getMetricFamilySamples(bool $sortMetrics = true): array
|
||||||
|
Loading…
Reference in New Issue
Block a user