kislayphp / metrics
High-performance C++ PHP extension providing comprehensive metrics collection and monitoring for PHP microservices
Package info
Language:Shell
Type:php-ext
Ext name:ext-kislayphp_metrics
pkg:composer/kislayphp/metrics
Requires
- php: >=8.2
Requires (Dev)
None
Suggests
- kislayphp/config: Configurable metrics collection
- kislayphp/core: HTTP/HTTPS server metrics
- kislayphp/discovery: Service health metrics
- kislayphp/eventbus: Real-time metrics streaming
- kislayphp/gateway: API gateway metrics
- kislayphp/queue: Queue processing metrics
Provides
Conflicts
None
Replaces
None
README
Application metrics for PHP microservices. Counter, gauge, histogram, and timer types with zero instrumentation overhead. Prometheus-compatible export.
Part of the KislayPHP ecosystem.
โจ What It Does
kislayphp/metrics provides C++-backed metrics primitives for PHP applications. All metric operations use atomic operations โ measuring your code doesn't slow it down.
<?php $metrics = new Kislay\Metrics\Collector(); $metrics->counter('requests_total')->increment(); $metrics->histogram('request_duration_ms')->observe($ms); echo $metrics->export(); // Prometheus text format
๐ฆ Installation
pie install kislayphp/metrics
Enable in php.ini:
extension=kislayphp_metrics.so
๐ Quick Start
<?php $metrics = new Kislay\Metrics\Collector(); // Counter โ monotonically increasing $metrics->counter('http_requests_total', ['method' => 'GET', 'status' => '200']) ->increment(); // Gauge โ value that goes up and down $metrics->gauge('active_connections')->set(42); $metrics->gauge('queue_depth')->increment(); $metrics->gauge('queue_depth')->decrement(); // Histogram โ distribution of values $start = microtime(true); // ... handle request ... $metrics->histogram('request_duration_seconds') ->observe(microtime(true) - $start); // Timer (convenience wrapper for histogram) โ start()/stop() on the same // object; start() returns void, not a chainable handle. $timer = $metrics->timer('db_query_seconds'); $timer->start(); // ... run query ... $timer->stop(); // records the elapsed time into the underlying histogram // Export as Prometheus text format $app->get('/metrics', function($req, $res) use ($metrics) { $res->send($metrics->export(), 'text/plain; version=0.0.4'); });
๐ Public API
namespace Kislay\Metrics; class Collector { public function counter(string $name, array $labels = []): Counter; public function gauge(string $name, array $labels = []): Gauge; public function histogram(string $name, array $labels = [], array $buckets = []): Histogram; public function timer(string $name, array $labels = []): Timer; public function export(): string; // Prometheus text format public function reset(): void; } class Counter { public function increment(float $by = 1): void; public function get(): float; } class Gauge { public function set(float $value): void; public function increment(float $by = 1): void; public function decrement(float $by = 1): void; public function get(): float; } class Histogram { public function observe(float $value): void; } class Timer { public function __construct(string $name, Histogram $histogram); public function start(): void; public function stop(): float; public function record(float $ms): void; }
Note: Counter::get()/Gauge::get() genuinely return float (fixed 2026-08-12 โ previously returned int despite this signature). Collector::timer() (also added 2026-08-12) constructs and returns a real Timer bound to a lazily-created/looked-up Histogram of the same name, same lookup-or-create semantics as counter()/gauge()/histogram().
Known gap: the labels parameter shown above on counter()/gauge()/histogram()/timer() is accepted syntactically but not implemented โ labels are not attached to the exported series. Don't rely on it for cardinality/dimensioning yet.
start() returns void, not a chainable handle โ call start()/stop() on the same Timer instance (there is no separate TimerHandle class). stop() returns the elapsed milliseconds AND records that value into the underlying Histogram automatically.
Legacy aliases: KislayPHP\Metrics\*
๐ Ecosystem
core ยท gateway ยท discovery ยท metrics ยท queue ยท eventbus