graze / statsd-dd
1.0.0
2021-04-14 20:34 UTC
Requires
- php: ^7.1|^8.0
Requires (Dev)
- graze/standards: ^2
- johnkary/phpunit-speedtrap: ^2|^3
- phpunit/phpunit: ^7|^8|^9
- squizlabs/php_codesniffer: ^3.6
This package is not auto-updated.
Last update: 2022-02-01 12:52:37 UTC
README
与 DataDog StatsD 代理通信的客户端。由 League/StatsD 分支。
有关指标的更多信息,请参阅: Datadogs 指标指南。
安装
通过 Composer
$ composer require graze/dog-statsd
用法
配置
$statsd = new Graze\DogStatsD\Client(); $statsd->configure([ 'host' => '127.0.0.1', 'port' => 8125, 'namespace' => 'example', ]);
或者
$statsd1 = DogStatsD\Client::instance('server1')->configure([...]); $statsd2 = DogStatsD\Client::instance('server2')->configure([...]);
StatsD 客户端默认在打开套接字时等待 ini_get('default_socket_timeout')
秒。要减少超时时间,请将 'timeout' => <int>
添加到您的配置中。
如果 StatsD 客户端无法将数据发送到 StatsD 服务器,它将抛出 ConnectionException
、抛出警告或忽略所有错误。这可以通过 onError
属性进行配置
'onError' => 'error' // 'error', 'exception' or 'ignore'
默认设置为 'error'
核心 StatsD 实现
要使用核心 statsd 实现(不包含 DataDog 添加的额外功能),请将以下内容包含在您的配置中
'dataDog' => false
方法
计数器
$statsd->increment('web.pageview'); $statsd->decrement('storage.remaining'); $statsd->increment([ 'first.metric', 'second.metric' ], 2); $statsd->increment('web.clicks', 1, 0.5);
仪表
$statsd->gauge('api.logged_in_users', 123456);
集合
$userID = 23; $statsd->set('api.unique_logins', $userID);
直方图
$result = $db->fetch(); $statsd->histogram('db.results', count($result), 0.5);
计时器
$statsd->timing('api.response_time', 256);
计时块
$statsd->time('api.dbcall', function () { // this code execution will be timed and recorded in ms });
标签
$statsd->increment('web.pageview', 1, ['page' => 'some/page']); $statsd->guage('api.logged_in_users', 123456, ['environement' => 'live']); $statsd->set('api.unique_logins', $userID, ['tag']); $statsd->timing('api.response_time', 245, ['end-point' => 'page', 'env' => 'test']);
标签处理器
您可以为每个指标在运行时添加标签处理器来注入标签。
$statsd->addTagProcessor(function (array $tags) { $tags['new-key'] = 'new-value'; return $tags; });
事件
$statsd->event( 'build.success', 'The build super_awesome_application_build_1 has completed', [ 'time' => time(), 'alert' => Client::ALERT_SUCCESS, ], [ 'environment' => 'live', ] );
服务检查
$statsd->serviceCheck( 'service.api.account', Client::STATUS_OK, [ 'host' => 'this.hostname.com', 'time' => time(), ], [ 'environment' => 'staging', ] );
默认标签
在每个请求中发送相同的基标签
$client = new Client(); $client->configure([ 'tags' => [ 'env' => 'live', 'release' => 'app-2.3.1', ], ]);
测试
$ make test
贡献
有关详细信息,请参阅CONTRIBUTING。
安全性
如果您发现任何安全相关的问题,请通过电子邮件security@graze.com报告,而不是使用问题跟踪器。
致谢
来自 thephpleague/statsd 的分支
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。