taskinosman/telegraf-metrix

Telegraf 指标 ucp/tcp 收集器用于 PHP

dev-master 2018-02-12 14:05 UTC

This package is not auto-updated.

Last update: 2024-09-27 23:13:47 UTC


README

支持 telegraf 行协议并通过 udp(目前)发送指标。行协议使用纳秒级精度的时间戳。从版本 0.0.14 开始,您可以可选地添加纳秒时间戳作为最后一个参数。如果您省略此时间戳,telegraf 将根据其本地时间将其添加到您的数据中。有时您可能想添加此时间戳以发送过去的数据。时间戳是纳秒级精度。PHP 只能理解微秒。您可能需要将此值乘以 1000 以将其转换为纳秒。

我预先准备了 UDP 协议,因为 PHP 是无状态的,并且不能本地运行异步函数。我不想用需要握手和许多检查的 TCP 来阻塞我的操作。

计划在下一个版本中支持 TCP

安装

composer require taskinosman/telegraf-metrix @dev

Telegraf 配置

在 telegraf.conf 中启用 [[inputs.socket_listener]] 并使用类似 udp://:8094 的服务地址以启用从 0.0.0.0 的 UDP。您也可以选择激活 127.0.0.1 的 UDP,这是推荐的方法。

用法

  • 默认目标是 udp://127.0.0.1:8094
  • 目标要么使用所有组件 scheme://host:port 定义,要么保持默认值
  • 测量名称:^[a-zA-Z0-9_, .]+$
  • 标签键,标签值:^[a-zA-Z0-9_,. =]+$
  • 标签是可选的
  • 字段是可选的
  • send() 返回格式化的行协议或错误时的 false
  • 时间戳是可选的,telegraf 如果您省略会为您处理
$metrix = new metrix("udp://127.0.0.1");

while(true) {

    $metrix->send('Random Generator',
            ['type'=>'random'],
            ['measurement 1 to 100'=>rand(1,100),'measurement 1 to 500'=>rand(1,500)]
    );

    $metrix->send('Memory',
            ['from'=>'php'],
            ['usage'=>memory_get_usage(),'peak'=>memory_get_peak_usage()]
    );

    $metrix->send('Mixed',
            ['from'=>'key.subkey.lastkey'],
            time()
    );

    sleep(1);
}

脉冲功能(alpha)

包括基本脉冲/心跳功能。此功能将使用给定 $appname 作为标识符的应用程序心跳通过 udp 发送到 telegraf。这些脉冲可以用来检测应用程序的异常或停机时间。

给定的示例将每 15 秒自动向 telegraf 发送一次脉冲/心跳

$metrix = new metrix("udp://127.0.0.1");
$metrix->pulseEnable('example.php',15);

行协议详情

https://docs.influxdb.org.cn/influxdb/v0.9/write_protocols/line/

[key] [tags] [fields] [timestamp]

Fields are key-value metrics associated with the measurement. Every line must have at least one field. Multiple fields must be separated with commas and not spaces.

Field keys are always strings and follow the same syntactical rules as described above for tag keys and values. Field values can be one of four types. The first value written for a given field on a given measurement defines the type of that field for all series under that measurement.

Integers are numeric values that do not include a decimal and are followed by a trailing i when inserted (e.g. 1i, 345i, 2015i, -10i). Note that all values must have a trailing i. If they do not they will be written as floats.

Floats are numeric values that are not followed by a trailing i. (e.g. 1, 1.0, -3.14, 6.0e5, 10).

Boolean values indicate true or false. Valid boolean strings are (t, T, true, True, TRUE, f, F, false, False and FALSE).

Strings are text values. All string values must be surrounded in double-quotes ". If the string contains a double-quote, it must be escaped with a backslash, e.g. \".