tuupola / instrument-middleware
PSR-7 中间件用于PHP应用程序的仪表化
Requires
- php: ^5.5 || ^7.0
- psr/http-message: ^1.0
- tuupola/instrument: ^1.2
- tuupola/witchcraft: ^1.1
Requires (Dev)
- phpunit/phpunit: ^4.8
- squizlabs/php_codesniffer: ^2.5
- zendframework/zend-diactoros: ^1.3
This package is auto-updated.
Last update: 2024-08-30 01:08:57 UTC
README
Instrument的配套中间件。[Instrument](https://github.com/tuupola/instrument)。自动化基于PSR-7的应用程序代码的基本仪表化。
安装
使用 composer 进行安装。
$ composer require tuupola/instrument-middleware
用法
您必须能够访问 InfluxDB 数据库以存储数据。配置Instrument实例并将其传递给中间件。这是唯一必须的参数。请注意!中间件的顺序很重要。Instrument中间件 必须 是最后添加的。
require __DIR__ . "/vendor/autoload.php"; $app = new \Slim\App; $influxdb = InfluxDB\Client::fromDSN("http+influxdb://foo:bar@localhost:8086/instrument"); $app->add(new Instrument\Middleware([ "instrument" => new Instrument\Instrument([ "adapter" => new Instrument\Adapter\InfluxDB($influxdb), "transformer" => new Instrument\Transformer\InfluxDB ]) ]));
或者如果您使用的是更简洁的Slim 3容器。
require __DIR__ . "/vendor/autoload.php"; $app = new \Slim\App; $container = $app->getContainer(); $container["influxdb"] = function ($container) { return InfluxDB\Client::fromDSN("http+influxdb://foo:bar@localhost:8086/instrument"); }; $container["instrument"] = function ($container) { return new Instrument\Instrument([ "adapter" => new Instrument\Adapter\InfluxDB($container["influxdb"]), "transformer" => new Instrument\Transformer\InfluxDB ]); }; $container["instrumentMiddleware"] = function ($container) { return new Instrument\Middleware([ "instrument" => $container["instrument"] ]); }; $app->add("instrumentMiddleware");
记录了什么?
让我们假设您有以下路由。
$app->get("/", function ($request, $response, $arguments) { return $response->write("Here be dragons...\n"); }); $app->get("/hello/{name}", function ($request, $response, $arguments) { return $response->write("Hello {$arguments['name']}!\n"); });
当请求发生时,中间件将基本的仪表化数据保存到数据库中。
$ curl http://192.168.50.53/
Here be dragons...
$ curl http://192.168.50.53/hello/foo
Hello foo!
> select * from instrument name: instrument ---------------- time bootstrap memory method process route status total 1475316633441185508 158 1048576 GET 53 / 200 213 1475316763025260932 140 1048576 GET 69 /hello/foo 200 211
字段 bootstrap
是请求开始与执行第一个中间件之间经过的时间。字段 total
是请求开始与最后一个中间件退出之间经过的时间。再次注意,Instrument中间件 必须 是最后添加的,因此它将在进入中间件堆栈时首先执行,在退出时最后执行。
字段 memory
和 process
是请求处理过程中的峰值PHP内存使用量和经过的时间。这包括路由或控制器以及所有其他中间件。
标签 method
和 status
是请求方法和响应的HTTP状态码。标签 route
是不带查询字符串的请求URI。
添加或覆盖标签
您可以通过使用 tags
参数来添加标签。它可以是数组或返回数组的匿名函数。函数接收 $request
和 $response
对象作为参数。如果您返回任何默认标签,它将覆盖中间件设置的值。
$app->add(new Instrument\Middleware([ "instrument" => $instrument, "tags" => ["host" => "localhost", "method" => "XXX"] ]));
基本上与以下代码相同。
$app->add(new Instrument\Middleware([ "instrument" => $instrument, "tags" => function ($request, $response) { return ["host" => "localhost", "method" => "XXX"]; } ]));
> select * from instrument name: instrument ---------------- time bootstrap memory host method process route status total 1475316633441185508 158 1048576 localhost XXX 53 / 200 213 1475316763025260932 140 1048576 localhost XXX 69 /hello/foo 200 211
自定义字段和标签名称
所有字段和标签名称都可以自定义。以下示例更改了所有标签和字段名称。它还更改了测量名称。在InfluxDB术语中,MEASUREMENT
与SQL世界中的 TABLE
相同。
$app->add(new Instrument\Middleware([ "instrument" => $instrument, "measurement" = "api", "bootstrap" = "startup", "process" = "execution", "total" = "all", "memory" = "mem", "status" = "code", "route" = "uri", "method" = "verb" ]));
> select * from api name: api ---------------- time startup mem verb execution uri code all 1475316633441185508 158 1048576 GET 53 / 200 213 1475316763025260932 140 1048576 GET 69 /hello/foo 200 211
要禁用标签或字段,请将其设置为 false
。
$app->add(new Instrument\Middleware([ "instrument" => $instrument, "measurement" = "api", "bootstrap" = "startup", "process" = false, "total" = "total", "memory" = false, "status" = false, "route" = false, "method" = false ]));
> select * from api name: api ---------------- time startup total 1475316633441185508 158 213 1475316763025260932 140 211
手动添加数据
您还可以手动向测量添加额外的数据。
$app->get("/manual", function ($request, $response, $arguments) { $timing = $this->instrument->timing("instrument"); $timing->start("db"); /* Some expensive database queries. */ $timing->stop("db"); return $response->write("Manually adding additional data...\n"); });
$ curl http://192.168.50.53/manual Manually adding additional data...
> select * from instrument name: instrument ---------------- time bootstrap db memory method process route status total 1475318315949095876 155 411 1048576 GET 466 /manual 200 623
测试
您可以手动运行测试...
$ make test
... 或者在每个代码更改时自动运行。
$ make watch
贡献
请参阅CONTRIBUTING 以获取详细信息。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 tuupola@appelsiini.net 而不是使用问题跟踪器。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。