PHPackages                             rancoud/prometheus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. rancoud/prometheus

ActiveLibrary[Testing &amp; Quality](/categories/testing)

rancoud/prometheus
==================

Prometheus PHP Client using Rancoud's packages

1.0.4(3w ago)0480↓27.8%MITPHPPHP &gt;=8.4.0CI passing

Since May 17Pushed 3w ago1 watchersCompare

[ Source](https://github.com/rancoud/Prometheus)[ Packagist](https://packagist.org/packages/rancoud/prometheus)[ RSS](/packages/rancoud-prometheus/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (5)Dependencies (12)Versions (14)Used By (0)

Prometheus Package
==================

[](#prometheus-package)

[![Packagist PHP Version Support](https://camo.githubusercontent.com/a22526f1690cd2b5f8c163d3f33568d1068c9d87f639f4caaef82c6720fc36c6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f72616e636f75642f50726f6d657468657573)](https://camo.githubusercontent.com/a22526f1690cd2b5f8c163d3f33568d1068c9d87f639f4caaef82c6720fc36c6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f72616e636f75642f50726f6d657468657573)[![Packagist Version](https://camo.githubusercontent.com/8e7e926ccfb12beef35d0285815db402a7642efab65afbf35cf4caa4203361cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72616e636f75642f50726f6d657468657573)](https://packagist.org/packages/rancoud/Prometheus)[![Packagist Downloads](https://camo.githubusercontent.com/9b41bea6e9bd6839687df0843413668955dc57463bec1de667a9bc18bfdd429a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72616e636f75642f50726f6d657468657573)](https://packagist.org/packages/rancoud/Prometheus)[![Composer dependencies](https://camo.githubusercontent.com/aae95fbaa83bc6a3f4597f3a75da45ea46ec236fc324617f0e5a2f15e07fe750/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d302d627269676874677265656e)](https://github.com/rancoud/Prometheus/blob/main/composer.json)[![Test workflow](https://camo.githubusercontent.com/5992b6e7d3e5d86f09e2ff4e08a01ff81881310959c75b8b24369ccaa13d94fe/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72616e636f75642f50726f6d6574686575732f746573742e796d6c3f6272616e63683d6d61696e)](https://github.com/rancoud/Prometheus/actions/workflows/test.yml)[![Codecov](https://camo.githubusercontent.com/819b63b624c438573c60af2fb6e29bda0996a4622f08a7ef5e58f60c2c99ae17/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f72616e636f75642f50726f6d6574686575733f6c6f676f3d636f6465636f76)](https://codecov.io/gh/rancoud/Prometheus)

Prometheus client library using database, memory storage.

Based on documentation [https://prometheus.io/docs/instrumenting/writing\_clientlibs/](https://prometheus.io/docs/instrumenting/writing_clientlibs/)and [https://github.com/PromPHP/prometheus\_client\_php](https://github.com/PromPHP/prometheus_client_php)

Use `rancoud/Database` package () when using MySQL, PostgreSQL or SQLite.

Installation
------------

[](#installation)

```
composer require rancoud/prometheus
```

How to use it?
--------------

[](#how-to-use-it)

### Counter metric example

[](#counter-metric-example)

Simple counter and expose result

```
use Rancoud\Prometheus\Counter;
use Rancoud\Prometheus\Descriptor;
use Rancoud\Prometheus\Storage\InMemory;

// Define a counter
$counter = new Counter(
        new InMemory(),         // inc(3);

// Now you can expose as plain text result
echo $counter->expose();

// Result of expose() below:
#TYPE login counter
login 4
```

Counter with help text and labels

```
use Rancoud\Prometheus\Counter;
use Rancoud\Prometheus\Descriptor;
use Rancoud\Prometheus\Storage\InMemory;

$descriptor = new Descriptor("request_count", ['method', 'path'])
    ->setHelp('Number of request by method and path');

$counter = new Counter(new InMemory(), $descriptor);

$counter->inc(5, ['GET', 'home']);
$counter->inc(3, ['GET', 'login']);
$counter->inc(1, ['POST', 'login']);

echo $counter->expose();

// Result of expose() below:
#HELP request_count Number of request by method and path
#TYPE request_count counter
request_count{method="GET",path="home"} 5
request_count{method="GET",path="login"} 3
request_count{method="POST",path="login"} 1
```

### Gauge metric example

[](#gauge-metric-example)

```
use Rancoud\Prometheus\Gauge;
use Rancoud\Prometheus\Descriptor;
use Rancoud\Prometheus\Storage\InMemory;

// Define a gauge
$gauge = new Gauge(
        new InMemory(),
        new Descriptor("account_count")
            ->setHelp('Number of account')
    );

// You can set a value
$gauge->set(100);

// You can increment
$gauge->inc(15);

// You can decrement
$gauge->dec(5);

echo $gauge->expose();

// Result of expose() below:
#HELP account_count Number of account
#TYPE account_count gauge
account_count 110
```

### Histogram metric example

[](#histogram-metric-example)

```
use Rancoud\Prometheus\Histogram;
use Rancoud\Prometheus\Descriptor;
use Rancoud\Prometheus\Storage\InMemory;

// Define a histogram
$histogram = new Histogram(
        new InMemory(),
        new Descriptor("http_request_duration_seconds")
    );

// You can observe a value
$histogram->observe(0.56);

echo $histogram->expose();

// Result of expose() below:
#TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.005"} 0
http_request_duration_seconds_bucket{le="0.01"} 0
http_request_duration_seconds_bucket{le="0.025"} 0
http_request_duration_seconds_bucket{le="0.05"} 0
http_request_duration_seconds_bucket{le="0.075"} 0
http_request_duration_seconds_bucket{le="0.1"} 0
http_request_duration_seconds_bucket{le="0.25"} 0
http_request_duration_seconds_bucket{le="0.5"} 0
http_request_duration_seconds_bucket{le="0.75"} 1
http_request_duration_seconds_bucket{le="1"} 1
http_request_duration_seconds_bucket{le="2.5"} 1
http_request_duration_seconds_bucket{le="5"} 1
http_request_duration_seconds_bucket{le="7.5"} 1
http_request_duration_seconds_bucket{le="10"} 1
http_request_duration_seconds_bucket{le="+Inf"} 1
http_request_duration_seconds_count 1
http_request_duration_seconds_sum 0.56
```

You can set your own buckets.

```
new Descriptor("http_request_duration_seconds")->setHistogramBuckets([0, 5, 10]);
```

### Summary metric example

[](#summary-metric-example)

```
use Rancoud\Prometheus\Summary;
use Rancoud\Prometheus\Descriptor;
use Rancoud\Prometheus\Storage\InMemory;

// Define a summary
$summary = new Summary(
        new InMemory(),
        new Descriptor("http_request_duration_seconds")
    );

// You can observe a value
$summary->observe(0.56);

echo $summary->expose();

// Result of expose() below:
#TYPE http_request_duration_seconds summary
http_request_duration_seconds{quantile="0.01"} 0.56
http_request_duration_seconds{quantile="0.05"} 0.56
http_request_duration_seconds{quantile="0.5"} 0.56
http_request_duration_seconds{quantile="0.95"} 0.56
http_request_duration_seconds{quantile="0.99"} 0.56
http_request_duration_seconds_count 1
http_request_duration_seconds_sum 0.56
```

You can set your own quantiles.

```
new Descriptor("http_request_duration_seconds")->setSummaryQuantiles([0.1, 0.5, 0.9]);
```

You can change the TTL in seconds you want to keep the observed values.

```
new Descriptor("http_request_duration_seconds")->setSummaryTTL(10);
```

### Registry

[](#registry)

A registry is an object where all metrics are stored.

Registry instance example

```
use Rancoud\Prometheus\Counter;
use Rancoud\Prometheus\Descriptor;
use Rancoud\Prometheus\Registry;
use Rancoud\Prometheus\Storage\InMemory;

// Define a registry
$registry = new Registry();

// Define 2 counters
$counter1 = new Counter(new InMemory(), new Descriptor("login"));
$counter2 = new Counter(new InMemory(), new Descriptor("logout"));

// Add counters in registry
$registry->register($counter1, $counter2);

// Update counters otherwise is not exposed
$counter1->inc(4);
$counter2->inc(2);

// Now you can expose as plain text result
echo $registry->expose();

// Result of expose() below:
#TYPE login counter
login 4
#TYPE logout counter
logout 2
```

Default Registry with static Singleton example

```
use Rancoud\Prometheus\Counter;
use Rancoud\Prometheus\Descriptor;
use Rancoud\Prometheus\Registry;
use Rancoud\Prometheus\Storage\InMemory;

// Define 2 counters AND register them in the default registry
$counter1 = new Counter(new InMemory(), new Descriptor("login"))->register();
$counter2 = new Counter(new InMemory(), new Descriptor("logout"))->register();

// Update counters otherwise is not exposed
$counter1->inc(4);
$counter2->inc(2);

// Now you can expose as plain text result
echo Registry::getDefault()->expose();

// Result of expose() below:
#TYPE login counter
login 4
#TYPE logout counter
logout 2
```

### Database storage example

[](#database-storage-example)

Using SQLite with memory database is same as using InMemory Database.

```
use Rancoud\Database\Configurator;
use Rancoud\Database\Database;
use Rancoud\Prometheus\Counter;
use Rancoud\Prometheus\Descriptor;
use Rancoud\Prometheus\Storage\SQLite;

$params = [
    'driver'    => 'sqlite',
    'host'      => '',
    'user'      => '',
    'password'  => '',
    'database'  => 'prometheus.db'
];

$configurator = new Configurator($params);

$database = new Database($configurator);

$storage = new SQLite($database);

$counter = new Counter($storage, new Descriptor("example"));
```

Collector (metric)
------------------

[](#collector-metric)

Constructor

```
public function __construct(Adapter $storage, Descriptor $descriptor)
```

Returns raw metrics (descriptor + samples) as iterable.

```
public function collect(): iterable
```

Returns text of metric as string.

```
public function expose(): string
```

Returns metric name.

```
public function metricName(): string
```

Register in the default Registry.

```
public function register(): self
```

### Counter Metric

[](#counter-metric)

Increments counter.

```
public function inc(float|int $value = 1, array $labels = []): void
```

### Gauge Metric

[](#gauge-metric)

Increments counter.

```
public function inc(float|int $value = 1, array $labels = []): void
```

Decrements counter.

```
public function dec(float|int $value = 1, array $labels = []): void
```

Sets value of gauge.

```
public function set(float|int $value, array $labels = []): void
```

Sets value of gauge with function \\time() to use current Unix timestamp.

```
public function setToCurrentTime(array $labels = []): void
```

### Histogram Metric

[](#histogram-metric)

Adds a new sample.

```
public function observe(float $value, array $labels = []): void
```

Generates linear buckets.
Creates 'count' regular buckets, each 'width' wide, where the lowest bucket has an upper bound of 'start'.

```
public static function linearBuckets(float $start, float $width, int $countBuckets): array
```

Generates exponential buckets.
Creates 'count' regular buckets, where the lowest bucket has an upper bound of 'start' and each following bucket's upper bound is 'factor' times the previous bucket's upper bound.

```
public static function exponentialBuckets(float $start, float $growthFactor, int $countBuckets): array
```

### Summary Metric

[](#summary-metric)

Adds a new sample.

```
public function observe(float $value, array $labels = []): void
```

Descriptor
----------

[](#descriptor)

Constructor

```
public function __construct(string $name, array $labels = [])
```

When exposed it will output a line #HELP {your message}.

```
public function setHelp(string $help): self
```

Set histogram buckets instead of using default buckets.

```
public function setHistogramBuckets(array $buckets): self
```

Set summary TTL instead of using default TTL.

```
public function setSummaryTTL(int $ttlInSeconds): self
```

Set summary quantiles instead of using default quantiles.

```
public function setSummaryQuantiles(array $quantiles): self
```

Returns name.

```
public function name(): string
```

Returns labels.

```
public function labels(): array
```

Returns labels count.

```
public function labelsCount(): int
```

Returns histogram buckets.

```
public function buckets(): array
```

Returns summary quantiles.

```
public function quantiles(): array
```

Returns summary TTL.

```
public function ttlInSeconds(): int
```

Exports HELP.

```
public function exportHelp(): string
```

Exports TYPE.

```
public function exportType(string $type): string
```

Exports value (counter, gauge, histogram \_sum and \_count, summary \_sum and \_count).

```
public function exportValue(float|int $value, array $labelValues, string $suffixName = ''): string
```

Exports value (histogram).

```
public function exportHistogramValue(string $bucket, int $value, array $labelValues): string
```

Exports value (summary).

```
public function exportSummaryValue(float $quantile, array $values, array $labelValues): string
```

Registry
--------

[](#registry-1)

Registers metric.

```
public function register(Collector ...$collectors): void
```

Unregisters metric.

```
public function unregister(Collector ...$collectors): void
```

Returns raw metrics registered (descriptor + samples) as iterable.

```
public function collect(): iterable
```

Returns text of metrics registered as string.

```
public function expose(): string
```

Registers metric in the default Registry (singleton).

```
public static function registerInDefault(Collector $collector): void
```

Returns the default Registry (singleton).

```
public static function getDefault(): self
```

Storage (Adapter interface)
---------------------------

[](#storage-adapter-interface)

Returns metrics (counter, gauge, histogram and summary) as iterable.
If metric type and name is provided it will return only the specify metric.

```
public function collect(string $metricType = '', string $metricName = ''): iterable
```

Returns text of metrics (counter, gauge, histogram and summary) as iterable.
If metric type and name is provided it will return only the specify metric.

```
public function expose(string $metricType = '', string $metricName = ''): iterable
```

Updates counter metric.

```
public function updateCounter(Descriptor $descriptor, float|int $value = 1, array $labelValues = []): void
```

Updates gauge metric.

```
public function updateGauge(Descriptor $descriptor, Operation $operation, float|int $value = 1, array $labelValues = []): void
```

Adds sample to histogram metric.

```
public function updateHistogram(Descriptor $descriptor, float $value, array $labelValues = []): void
```

Adds sample to summary metric.

```
public function updateSummary(Descriptor $descriptor, float $value, array $labelValues = []): void
```

Removes all data saved.

```
public function wipeStorage(): void
```

Overrides Time Function for summary metric.

```
public function setTimeFunction(callable|string $time): void
```

### InMemory

[](#inmemory)

Returns text of counters metric as iterable.

```
public function exposeCounters(string $metricName = ''): iterable
```

Returns text of gauges metric as iterable.

```
public function exposeGauges(string $metricName = ''): iterable
```

Returns text of histograms metric as iterable.

```
public function exposeHistograms(string $metricName = ''): iterable
```

Returns text of summaries metric as iterable.

```
public function exposeSummaries(string $metricName = ''): iterable
```

### SQLite

[](#sqlite)

Returns text of counters metric as iterable.

```
public function exposeCounters(string $metricName = ''): iterable
```

Returns text of gauges metric as iterable.

```
public function exposeGauges(string $metricName = ''): iterable
```

Returns text of histograms metric as iterable.

```
public function exposeHistograms(string $metricName = ''): iterable
```

Returns text of summaries metric as iterable.

```
public function exposeSummaries(string $metricName = ''): iterable
```

Remove all expired summaries sample according to the TTL.

```
public function deleteExpiredSummaries(): void
```

Drop all tables.

```
public function deleteStorage(): void
```

How to Dev
----------

[](#how-to-dev)

`composer ci` for php-cs-fixer and phpunit and coverage
`composer lint` for php-cs-fixer
`composer test` for phpunit and coverage

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance95

Actively maintained with recent releases

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 86.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~95 days

Total

5

Last Release

22d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1884186?v=4)[Rancoud](/maintainers/rancoud)[@rancoud](https://github.com/rancoud)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (65 commits)")[![rancoud](https://avatars.githubusercontent.com/u/1884186?v=4)](https://github.com/rancoud "rancoud (10 commits)")

---

Tags

composercoveragepackagistphpphp84phpunitprometheusprometheus-client

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/rancoud-prometheus/health.svg)

```
[![Health](https://phpackages.com/badges/rancoud-prometheus/health.svg)](https://phpackages.com/packages/rancoud-prometheus)
```

###  Alternatives

[dms/phpunit-arraysubset-asserts

This package provides ArraySubset and related asserts once deprecated in PHPUnit 8

14228.7M340](/packages/dms-phpunit-arraysubset-asserts)[orchestra/workbench

Workbench Companion for Laravel Packages Development

8219.1M69](/packages/orchestra-workbench)[phpbenchmark/phpbenchmark

Easy to use benchmark toolkit for your PHP-application. This library contains classes for comparing algorithms as well as benchmarking application responses

8011.5k2](/packages/phpbenchmark-phpbenchmark)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
