tuupola / instrument
InfluxDB的应用指标工具箱
Requires
- php: ^7.0|^8.0
- influxdb/influxdb-php: ^1.2
- symfony/stopwatch: ^3.0|^4.0|^5.0
Requires (Dev)
- klaussilveira/simple-shm: ^1.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
- squizlabs/php_codesniffer: ^3.6
Suggests
- klaussilveira/simple-shm: If you want to use the Gauge datatype.
This package is auto-updated.
Last update: 2024-09-20 17:13:00 UTC
README
使用Instrument,您可以监控和测量PHP应用程序的性能。它可以收集和存储指标,如脚本执行时间和内存使用情况或数据库中的时间。
用法
使用 composer 安装。
$ composer require tuupola/instrument
您还必须有权访问 InfluxDB 数据库以存储数据。安装后,连接到您的数据库并开始发送指标。
require __DIR__ . "/vendor/autoload.php"; $influxdb = InfluxDB\Client::fromDSN("http+influxdb://user:pass@localhost:8086/instrument"); $instrument = new Instrument\Instrument([ "adapter" => new Instrument\Adapter\InfluxDB($influxdb), "transformer" => new Instrument\Transformer\InfluxDB ]); $instrument->count("users", 100); $instrument->send();
可选的,如果您想使用 gauge 数据类型,则需要 shmop 扩展 和 klaussilveira/simple-shm 库。
composer require klaussilveira/simple-shm
还有一个 配套中间件,如果您使用基于PSR-7的框架,则可以自动化应用程序代码的基本instrumenting。
演示
包含示例 Grafana 仪表板。要查看Instrument的实际效果,请启动Vagrant演示服务器并发出一些示例请求。
$ cd demo $ vagrant up $ while sleep 1; do curl http://192.168.50.53/random; done
上述命令启动服务器,并每秒插入随机Instrument数据。现在,您可以访问提供的 演示仪表板(管理员:admin)以实时查看此操作。
写入数据
假设您已经熟悉 InfluxDB数据结构。每个度量必须有一个 name
。度量应包含一个 value
或多个值 fields
或两者都包含。度量可以有一个或多个 tags
。
例如,要创建一个名为 users
的新 count
度量,包含一个值为 100
的值,可以使用以下任一方法。
$instrument->count("users", 100); $instrument->count("users")->set(100); $instrument->send();
> SELECT * FROM users
name: users
---------
time value
1457067288109133121 100
记录多个值并附加度量。
$instrument ->count("users") ->set("total", 100) ->set("active", 27) ->tags(["host" => "localhost"]); $instrument->send();
> SELECT * FROM users
name: users
---------
time total active host
1457067288109134122 100 27 localhost
事件数据类型不包含数值度量。
$instrument ->event("deploy", "New version deployed") ->tags(["host" => "localhost"]); $instrument->send();
> SELECT * FROM events;
name: events
------------
time title description host
1464277178854200406 deploy New version deployed localhost
数据类型
计数
计数是最简单的数据类型。除了设置值外,您还可以增加和减少它。
$requests = $instrument->count("requests", 50); /* 50 */ $requests->increase(); /* 51 */ $requests->decrease(); /* 50 */ $requests->increase(5); /* 55 */ $instrument->send();
或者,如果您更喜欢流畅的接口,您也可以这样做。
$instrument ->count("users") ->set("active", 27) /* 27 */ ->increase("active", 5) /* 32 */ ->decrease("active", 2); /* 30 */ $instrument->send();
计时
使用计时,您可以测量毫秒级的执行时间。您可以选择自行传递值或使用提供的辅助函数来测量代码执行时间。
$instrument->timing("roundtrip")->set("firstbyte", 28); $instrument->timing("roundtrip")->set("lastbyte", 40); $instrument->timing("roundtrip")->set("processing", function () { /* Code to be measured */ }); $instrument->timing("roundtrip")->start("fetching"); /* Code to be measured */ $instrument->timing("roundtrip")->stop("fetching"); $instrument->send();
由于计时内部使用 symfony/stopwatch,因此您可以作为奖励获得PHP内存使用情况。它不会自动包含在度量数据中,但您可以手动包含它。
$memory = $instrument->timing("roundtrip")->memory() $instrument->timing("roundtrip")->set("memory", $memory); $instrument->send();
仪表
仪表与计数相同。但是,它记得请求之间的值。仪表值在服务器重新启动时重置。您需要 shmop 扩展 和 klaussilveira/simple-shm 才能使用仪表。
$errors = $instrument->gauge("errors"); $errors->increase("fatal"); /* 1 */ $errors->increase("critical"); /* 1 */ unset($errors); $errors = $instrument->gauge("errors"); $errors->increase("fatal"); /* 2 */ $errors->increase("critical", 4); /* 5 */ $instrument->send();
可以使用 delete()
方法从共享内存中删除单个值。可以使用 clear()
方法一次性删除具有指定名称的仪表的所有值。
$errors = $instrument->gauge("errors"); $errors->delete("fatal"); /* null */ $errors->clear();
事件
事件可用于在仪表板上显示注释。默认情况下,它们不包含数值测量。相反,它包含标题
和描述
字段。这些字段应包含事件的简称和详细描述。
$instrument ->event("deploy", "Version 0.9.0 deployed") ->tags(["host" => "localhost"]); $instrument ->event("deploy", "Version 0.9.1 deployed") ->tags(["host" => "localhost"]); $instrument->send();
> SELECT * FROM events;
name: events
------------
time title description host
1464277178854200406 deploy Version 0.9.0 deployed localhost
1464277178854201240 deploy Version 0.9.1 deployed localhost
如果您正在使用Grafana,您可以使用上述数据,通过使用SELECT * FROM events WHERE $timeFilter
作为注释查询。同时设置如下所示的列映射。
测试
您可以选择手动运行测试...
$ composer test
... 或者在每个代码更改时自动运行。这需要entr工作。
$ composer watch
贡献
有关详细信息,请参阅CONTRIBUTING。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件tuupola@appelsiini.net联系,而不是使用问题跟踪器。
许可协议
MIT许可协议(MIT)。有关更多信息,请参阅许可文件。