influx: Nginx-module-lua 客户端用于 InfluxDB
安装
如果您尚未设置 RPM 仓库订阅,请 注册。然后您可以继续以下步骤。
CentOS/RHEL 7 或 Amazon Linux 2
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install lua-resty-influx
CentOS/RHEL 8+、Fedora Linux、Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install lua5.1-resty-influx
要在 NGINX 中使用此 Lua 库,请确保已安装 nginx-module-lua。
本文档描述了 lua-resty-influx v0.2.1,发布于 2017 年 12 月 27 日。
此库提供了一个 OpenResty 接口,通过 UDP 和 HTTP 接口将数据点写入 InfluxDB 服务器。提供基于对象的接口和每个工作进程的缓冲接口。
概述
对象接口:
http {
server {
access_by_lua_block {
local i = require "resty.influx.object"
local influx, err =i:new({
host = "127.0.0.1",
port = 8086,
proto = "http",
db = "db",
hostname = "localhost",
auth = "user:password",
})
if (not influx) then
ngx.say(err)
return
end
influx:set_measurement("foo")
influx:add_tag("foo", "bar")
influx:add_field("value", 1)
influx:buffer()
-- 添加和缓冲额外的数据点
local ok, err = influx:flush()
if (not ok) then
ngx.say(err)
end
}
}
}
缓冲接口:
http {
init_worker_by_lua_block {
local ibuf = require "resty.influx.buffer"
local ok, err = ibuf.init({
host = "127.0.0.1",
port = 8089,
proto = "udp",
})
if (not ok) then
ngx.log(ngx.ERR, err)
end
}
server {
access_by_lua_block {
local ibuf = require "resty.influx.buffer"
ibuf.buffer({
measurement = "foo",
tags = {
{ foo = "bar" }
},
fields = {
{ value = 1 }
}
})
}
log_by_lua_block {
local ibuf = require "resty.influx.buffer"
ibuf.flush()
}
}
}
用法
选项
lua-resty-influx 提供了一个纯对象基础的接口,以及一个缓冲接口,该接口按工作进程存储数据点,然后通过 ngx.timer.at 异步缓冲。缓冲接口的创建应在 init_worker_by_lua 阶段通过 resty.influx.buffer.init 函数处理;面向对象接口的创建应在您的适当阶段处理程序中通过 resty.influx.object:new 处理。在这两种情况下,以下选项可用:
host
默认值: 127.0.0.1
设置 ngx.socket.udp 和 resty.http 尝试连接的主机。
port
默认值: 8086
设置 ngx.socket.udp 和 resty.http 尝试连接的端口。默认值为 8086,因为默认协议为 HTTP。
db
默认值: 'lua-resty-influx'
设置 resty.http 尝试连接的数据库。配置协议为 udp 时,此选项将被忽略。
hostname
默认值: host
设置 resty.http 为 HTTP 请求定义的 Host 头的主机名。默认情况下,这等于配置的 host 选项。当配置协议为 udp 时,此选项将被忽略。
proto
默认值: http
设置 resty.influx 连接到远程服务器的协议。请注意,UDP 在发送许多小的数据点集时可以显著提高性能,但代价是错误处理和身份验证。
precision
默认值: ms
设置 resty.influx 定义时间戳的精度。目前支持 ms、s 和 none;当配置为 none 时,作为行协议消息的一部分不会发送时间戳,远程 Influx 服务器将基于服务器本地时钟使用纳秒精度。
ssl
默认值: false
配置 HTTP 请求在发送数据之前执行 TLS 握手。当配置协议为 udp 时,此选项将被忽略。
auth
默认值: ''
设置提供给远程 HTTP(S) 的用户名和密码。此值必须以 user:password 格式作为单个字符串提供。当配置协议为 udp 时,此选项将被忽略。
面向对象接口
通过对象接口可以使用以下方法:
influx:set_measurement
语法: influx:set_measurement(string)
设置与当前对象关联的数据点的测量值。
influx:add_tag
语法: influx:add_tag(key, value)
将数据点标签作为键值对添加。键和值根据 (https://docs.influxdata.com/influxdb/v1.0/write_protocols/line_protocol_reference/) 进行转义。
influx:add_field
语法: influx:add_field(key, value)
将数据点字段作为键值对添加。字段和值根据 (https://docs.influxdata.com/influxdb/v1.0/write_protocols/line_protocol_reference/) 进行转义。整数值(以 i 结尾的数字值)会被正确插值。
influx:stamp
语法: influx:stamp(time?)
为与当前对象关联的数据点打上时间戳,可以提供一个可选的任意值(必须作为数字提供);否则,这将根据提供给对象接口的 new 的选项中指定的精度为对象打上适当的时间戳。
influx:clear
语法: influx:clear()
清除与当前对象关联的数据点的测量、标签和字段。当调用 buffer 或 write 时,这会在内部被调用。
influx:buffer
语法: local ok, err = influx:buffer()
缓冲与当前对象关联的数据点的内容,以便稍后刷新。成功时返回 true;否则,返回 false 和描述错误的字符串(例如,缓冲条件无效)。
influx:flush
语法: local ok, err = influx:flush()
刷新与当前对象关联的所有缓冲数据点。成功时返回 true;否则,返回 false 和描述错误的字符串(例如,等待缓冲的剩余数据,或没有可用的缓冲数据点)。
influx:write
语法: local ok, err = inflush:write()
写入与当前对象关联的数据点,而不清除现有对象缓冲区。这实际上是对单个数据点调用 buffer 和 flush 的简写。请注意,之前缓冲的数据点仍然保留在缓冲区中,如果需要,必须通过 flush 发送出去。
缓冲接口
通过缓冲接口可以使用以下函数:
influx.buffer
语法: influx.buffer(data_table)
在每个工作进程的缓冲区中缓冲一个新的数据点。data_table 必须是一个包含以下键的表:
measurement: 表示数据点测量的字符串tags: 包含表示标签元素的键值对表的整数索引表。有关示例,请参见概述。fields: 包含表示字段元素的键值对表的整数索引表。有关示例,请参见概述。
请注意,目前时间戳会自动设置为 ms 精度。
influx.flush
语法: influx.flush()
将当前工作进程中缓冲的所有数据点写入配置的 Influx 主机。成功时返回 true;否则,返回 false 和描述来自 ngx.timer.at 的错误的字符串。
此操作会立即返回并异步运行。
GitHub
您可以在 nginx-module-influx 的 GitHub 仓库 中找到有关此模块的其他配置提示和文档。