candycore/candy-metrics
最新稳定版本:v0.2.0
Composer 安装命令:
composer require candycore/candy-metrics
包简介
Telemetry primitives for SugarCraft / CandyWish — counters, gauges, histograms with pluggable backends (in-memory, StatsD UDP, Prometheus textfile, JSON stream). Drop-in middleware for SSH session metrics.
关键字:
README 文档
README
CandyMetrics
Lightweight telemetry primitives for SugarCraft / CandyWish servers. Counters, gauges, histograms with pluggable backends — drop-in middleware for SSH session metrics.
composer require sugarcraft/candy-metrics
Concepts
| Primitive | Behaviour |
|---|---|
| Counter | Monotonic value that accumulates (connect counts, errors). |
| Gauge | Instantaneous value that replaces on set (queue depth, RSS). |
| Histogram | Distribution of samples (latency, payload size). |
A Registry is the application-facing facade; it forwards every emit to the configured Backend. Backends decide how to persist or forward.
Usage
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();
withTags() returns a registry that pre-tags every emit:
$req = $reg->withTags(['request_id' => $rid, 'user' => $userId]); $req->counter('events'); // tagged with request_id + user automatically
Backends
InMemoryBackend
Useful for tests and for fanning out to multiple backends. Counters add up, gauges hold the last value, histograms keep every sample.
JsonStreamBackend
Newline-delimited JSON, one event per line. The simplest, most diagnostic-friendly target. Default writes to stderr.
{"ts":"2026-05-02T16:30:00+00:00","kind":"counter","name":"hits","value":1,"tags":{"route":"/x"}}
StatsdBackend
UDP datagrams in the etsy / DogStatsD wire format. Tags emitted as |#k:v,... (drop with dogstatsd: false for legacy servers).
hits:1|c|#route:/x,env:prod
PrometheusFileBackend
Atomically rewrites a .prom textfile-collector file with the current state of every metric. Pairs with node_exporter --collector.textfile.directory=…. Counter values accumulate across flush()s; histograms expose _count and _sum.
MultiBackend
Fan out to multiple backends — e.g. live StatsD plus a JSON audit trail.
$reg = new Registry(new MultiBackend( new StatsdBackend(), new JsonStreamBackend('/var/log/metrics.jsonl'), ));
CandyWish session middleware
Wires session telemetry into a CandyWish stack:
use SugarCraft\Wish\Server; use SugarCraft\Metrics\Registry; use SugarCraft\Metrics\Backend\PrometheusFileBackend; use SugarCraft\Metrics\Middleware\SessionMetrics; $reg = new Registry(new PrometheusFileBackend('/var/lib/wish/metrics.prom')); Server::new() ->use(new SessionMetrics($reg)) ->use(/* ... your stack ... */) ->serve();
Per session this emits:
| Metric | Type | Tags |
|---|---|---|
wish.session.connect |
counter | user, term |
wish.session.duration |
histogram | user, term |
wish.session.error |
counter | user, term, exception |
Pass extraTags (a callable receiving the Session) to add things like client subnet, geo, build version.
Status
Phase 9+ — first cut. 23 tests / 57 assertions across Registry, four backends, and the SessionMetrics middleware.
candycore/candy-metrics 适用场景与选型建议
candycore/candy-metrics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Metrics」 「ssh」 「statsd」 「counter」 「wish」 「telemetry」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 candycore/candy-metrics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 candycore/candy-metrics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 candycore/candy-metrics 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Collections of statsd collectors with templated metric names
Statsd (Object Oriented) client library for PHP
A flexible FTP, SSL-FTP, and SSH-based SFTP client for PHP. This lib provides helpers that are easy to use to manage the remote files.
Altax is an extensible deployment tool for PHP.
A abstraction for generating metrics in Laravel.
Laravel SDK for working with prometheus metrics
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 54
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-07