โ† All libraries

CandyMetrics

๐Ÿ“ˆ CandyMetrics

Telemetry primitives + CandyWish session middleware

port of promwish metrics telemetry statsd prometheus

Counters, gauges, histograms, UpDownCounters, AsyncCounters, AsyncGauges with pluggable backends โ€” drop-in middleware for SSH session metrics. Six instrument kinds + four backends: InMemory, JsonStream, StatsD, Prometheus textfile, plus a Multi fan-out. Built-in cardinality tracking with FIFO eviction.

Install

composer require sugarcraft/candy-metrics

Quickstart

use SugarCraft\Metrics\Registry;
use SugarCraft\Metrics\Backend\StatsdBackend;

$reg = new Registry(new StatsdBackend('127.0.0.1', 8125));

$reg->counter('http.requests', 1, ['route' => '/api/foo', 'status' => '200']);
$reg->gauge  ('queue.depth',  42);

$stop = $reg->time('http.duration', ['route' => '/api/foo']);
handleRequest();
$stop();

// Pre-tag every emit:
$req = $reg->withTags(['request_id' => $rid, 'user' => $userId]);
$req->counter('events');

What's in the box

CounterMonotonic value that accumulates (connect counts, errors).
GaugeInstantaneous value that replaces on set (queue depth, RSS).
HistogramDistribution of samples (latency, payload size).
UpDownCounterSynchronous counter supporting positive and negative increments (active connections, queue depth delta).
AsyncCounterAsynchronous counter observed at collection time via callback. External monotonic values (GC counts, DB pool size).
AsyncGaugeAsynchronous gauge observed at collection time via callback. External instantaneous readings (memory, queue depth).
Tagged metrics->withTags(['user' => $u]) returns a registry that pre-tags every emit.
Time helper$stop = $reg->time('http.duration'); $stop() โ€” emit a histogram sample on close.
BackendsInMemoryBackend, JsonStreamBackend, StatsdBackend, PrometheusFileBackend, MultiBackend.
Histogram bucketsPrometheus backend emits 14 cumulative le buckets (0.005โ€“100) +Inf for classic Prometheus compatibility.
Cardinality trackingFIFO eviction of oldest label combinations when per-metric limit (default 10 000) is exceeded. Inspect with cardinality().
Descriptor registrationPre-emit # TYPE / # HELP lines before first sample (Prometheus textfile collector metadata).

Source & demos

Try the quickstart โ†’

API

ClassMethodDescription
Descriptornew(name, help, type, labelKeys)Metric registration DTO (type: counter|gauge|histogram|summary)
Registrynew(backend)Create registry with backend
Registrynew(backend, defaultTags, cardinalityLimit)Create registry with backend, default tags, and per-metric cardinality limit (default 10 000)
Registryregister(Descriptor)Register metric descriptor for early TYPE/HELP emission
Registrycounter(name, value, tags)Increment counter
Registrygauge(name, value, tags)Set gauge value
Registryhistogram(name, value, tags)Record a histogram sample (14 classic buckets +Inf on Prometheus backend)
RegistryupDownCounter(name, amount, tags)Add positive or negative increment to a synchronous up-down counter
RegistryasyncCounter(name, value, tags)Record an observation from an asynchronous counter callback
RegistryasyncGauge(name, value, tags)Record an observation from an asynchronous gauge callback
Registrytime(name, tags)Time a block, returns callable
RegistrywithTags(tags)Pre-tag every emit
Registrybackend()Returns the current backend
Registrycardinality(name)Number of unique label-value combinations currently tracked for a metric
RegistrydeleteLabelValues(name, tags)Evict a specific label combination (FIFO eviction also runs automatically when limit is exceeded)
UpDownCounternew(registry, name, help)Create an up-down counter instrument
UpDownCounteradd(amount, tags)Add positive or negative increment
AsyncCounternew(registry, name, help, callback, tags)Create an async counter instrument
AsyncCounterobserve()Invoke callback and record the returned sum
AsyncGaugenew(registry, name, help, callback, tags)Create an async gauge instrument
AsyncGaugeobserve()Invoke callback and record the returned instantaneous value
InMemoryBackendnew()In-memory storage
StatsdBackendnew(host, port)StatsD protocol
PrometheusFileBackendnew(path)Prometheus textfile (atomic write, 14 bucket boundaries +Inf)

Demos.

VHS-recorded GIFs of every example shipped with the library. Regenerated automatically on every push that touches the source.

Counters / gauges / histograms

Counters / gauges / histograms

Registry โ†’ InMemoryBackend totals after a tally pass.