lua-resty-moesif
一个与 OpenResty 兼容的 Moesif Lua 库
$ opm get xiaocang/lua-resty-moesif
NGINX OpenResty 的 Moesif 插件
NGINX OpenResty 插件,用于记录 API 调用并发送到 Moesif 进行 API 分析和日志分析。
如何安装
推荐使用 Luarocks 安装 Moesif。
luarocks install --server=http://luarocks.org/manifests/moesif lua-resty-moesif
或者,OpenResty 提供了自己的包管理器 OPM,它也可以用来安装 Moesif。请注意,OPM 维护不佳,版本发布可能延迟几天,因此我们建议尽可能使用 LuaRocks。
opm get Moesif/lua-resty-moesif
共享配置 (ngx.shared)
以下选项对所有请求都是静态的。在共享字典 ngx.shared.moesif_conf
中设置这些选项。
lua_shared_dict moesif_conf 2m;
init_by_lua_block {
local config = ngx.shared.moesif_conf;
config:set("application_id", "Your Moesif Application Id")
}
application_id
__
(必填), 字符串, 用于与 Moesif 进行身份验证的应用程序 ID。这是必需的。
disable_capture_request_body
__
(可选) 布尔值, 用于禁用请求体日志记录的选项。默认值为 false
。
disable_capture_response_body
__
(可选) 布尔值, 用于禁用响应体日志记录的选项。默认值为 false
。
request_masks
__
(可选) 字符串, 用于屏蔽特定请求体字段的选项。用逗号分隔多个字段,例如 "field_a, field_b"
。
response_masks
__
(可选) 字符串, 用于屏蔽特定响应体字段的选项。用逗号分隔多个字段,例如 "field_a, field_b"
。
disable_transaction_id
__
(可选) 布尔值, 设置为 true 将阻止插入 <code>X-Moesif-Transaction-Id</code> 头部。默认值为 false
。
debug
__
(可选) 布尔值, 如果遇到集成问题,将其设置为 true 以打印调试日志。
动态变量 (ngx.var)
以下变量对于每个请求都是动态的。在 ngx.var
字典中设置这些变量。
header_filter_by_lua_block {
ngx.var.user_id = ngx.resp.get_headers()["User-Id"]
}
user_id
__
(可选) 字符串, 这使 Moesif 能够将 API 请求归因于单个用户,以便您可以了解谁在调用您的 API。这可以与 company_id
同时使用。一家公司可以拥有一个或多个用户。
company_id
__
(可选) 字符串, 如果您的业务是 B2B,这使 Moesif 能够将 API 请求归因于公司或帐户,以便您可以了解谁在调用您的 API。这可以与 user_id
同时使用。一家公司可以拥有一个或多个用户。
api_version
__
(可选) 字符串, 您希望为此请求添加的可选 API 版本。
如何使用
编辑您的 nginx.conf
文件以配置 Moesif OpenResty 插件:如果需要,请将 /usr/local/openresty/site/lualib
替换为正确的插件安装路径。
lua_shared_dict moesif_conf 2m;
init_by_lua_block {
local config = ngx.shared.moesif_conf;
config:set("application_id", "Your Moesif Application Id")
}
lua_package_path "/usr/local/openresty/luajit/share/lua/5.1/lua/resty/moesif/?.lua;;";
server {
listen 80;
resolver 8.8.8.8;
# Default values for Moesif variables
set $user_id nil;
set $company_id nil;
header_filter_by_lua_block {
# Optionally, identify the user such as by a header value
ngx.var.user_id = ngx.req.get_headers()["User-Id"]
# Optionally, identify the company (account) such as by a header value
ngx.var.company_id = ngx.req.get_headers()["Company-Id"]
}
access_by_lua '
local req_body, res_body = "", ""
local req_post_args = {}
ngx.req.read_body()
req_body = ngx.req.get_body_data()
local content_type = ngx.req.get_headers()["content-type"]
if content_type and string.find(content_type:lower(), "application/x-www-form-urlencoded", nil, true) then
req_post_args = ngx.req.get_post_args()
end
-- keep in memory the bodies for this request
ngx.ctx.moesif = {
req_body = req_body,
res_body = res_body,
req_post_args = req_post_args
}
';
body_filter_by_lua '
local chunk = ngx.arg[1]
local moesif_data = ngx.ctx.moesif or {res_body = ""} -- minimize the number of calls to ngx.ctx while fallbacking on default value
moesif_data.res_body = moesif_data.res_body .. chunk
ngx.ctx.moesif = moesif_data
';
log_by_lua_file /usr/local/openresty/luajit/share/lua/5.1/lua/resty/moesif/send_event.lua;
# Sample Hello World API
location /api {
add_header Content-Type "application/json";
return 200 '{\r\n \"message\": \"Hello World\",\r\n \"completed\": true\r\n}';
}
}
示例
基于 OpenResty 快速入门教程,提供了一个 Moesif 集成示例。
恭喜!如果一切操作都正确,Moesif 现在应该会跟踪所有与您之前指定的路由匹配的网络请求。如果您在设置过程中遇到任何问题,请联系 support@moesif.com。
其他集成
要查看有关集成选项的更多文档,请访问 集成选项文档。
POD 错误
注意!以上文档存在一些编码错误,如下所述:
- 大约在第 41 行
-
未终止的 B<...> 序列
- 大约在第 46 行
-
未终止的 B<...> 序列
- 大约在第 51 行
-
未终止的 B<...> 序列
- 大约在第 56 行
-
未终止的 B<...> 序列
- 大约在第 61 行
-
未终止的 B<...> 序列
- 大约在第 66 行
-
未终止的 B<...> 序列
- 大约在第 71 行
-
未终止的 B<...> 序列
- 大约在第 87 行
-
未终止的 B<...> 序列
- 大约在第 92 行
-
未终止的 B<...> 序列
- 大约在第 97 行
-
未终止的 B<...> 序列
作者
Keyur Doshi (Moesif)
许可证
2bsd
版本
-
一个与 OpenResty 兼容的 Moesif Lua 库 2020-03-10 04:23:43
-
2020-03-10 03:07:44