🙈 By 洪昕 2024-03-29
1. 介绍
NJet将开源社区提供的STS模块(https://github.com/vozlt/nginx-module-sts)进行修改适配后,用于输出Stream Server 的指标信息,支持返回 json, html 及prometheus 格式。
2. 配置指令
数据面配置指令
server_traffic_status_zone
语法 | server_traffic_status_zone [shared:name:size] |
默认值 | shared:stream_server_traffic_status:1m |
配置位置 | stream |
描述: 配置一个共享内存区域,该区域存储stream server的运行状态。
server_traffic_status
语法 | server_traffic_status<on |
默认值 | off |
配置位置 | stream,server |
描述: 是否记录stream server 的指标,如果配置了server_traffic_status_zone指令,将自动开启指标采集。
server_traffic_status_filter_by_set_key
语法 | server_traffic_status_filter_by_set_key key [name] |
默认值 | - |
配置位置 | stream,server |
描述: 对Stream统计的流量进行分组(name)及子分组(key), 可以使用变量,例如 $server_addr, $server_port等。参数name 不指定时,默认值是streamFilterZones。
控制面配置指令
stream_server_traffic_status_display
语法 | stream_server_traffic_status_display |
默认值 | - |
配置位置 | location |
描述: 该指令用于获取stream server 的指标信息,默认输出格式由指令stream_server_traffic_status_display进行设置。
stream_server_traffic_status_display_format
语法 | stream_server_traffic_status_display_format <json |
默认值 | json |
配置位置 | location |
描述: 该指令用于配置指标的默认返回格式
3. 配置文件示例
njet.conf njt_stream_stsc_module 模块采用静态编译,不用单独加载。
worker_processes auto;
cluster_name njet;
node_name node1;
error_log logs/error.log error;
helper ctrl /usr/local/njet/modules/njt_helper_ctrl_module.so /usr/local/njet/conf/njet_ctrl.conf;
helper broker /usr/local/njet/modules/njt_helper_broker_module.so;
events {
worker_connections 1024;
}
http {
}
stream {
server_traffic_status_zone shared:aabbcc:2m;
upstream tcpa {
server 127.0.0.1:1234;
}
server {
listen 1234;
server_traffic_status off;
server_traffic_status_filter_by_set_key * $server_addr;
return "aa\n";
}
server {
listen 1235;
server_traffic_status on;
proxy_pass tcpa;
}
}
njet_ctrl.conf 需要加载动态模块njt_stream_stsd_module.so
load_module /usr/local/njet/modules/njt_http_sendmsg_module.so;
load_module /usr/local/njet/modules/njt_ctrl_config_api_module.so;
load_module /usr/local/njet/modules/njt_helper_health_check_module.so;
load_module /usr/local/njet/modules/njt_http_upstream_api_module.so;
load_module /usr/local/njet/modules/njt_http_location_api_module.so;
load_module /usr/local/njet/modules/njt_doc_module.so;
load_module /usr/local/njet/modules/njt_http_vtsd_module.so;
load_module /usr/local/njet/modules/njt_stream_stsd_module.so;
error_log logs/error_ctrl.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
access_log off;
server {
listen 8081;
location / {
return 200 "njet control panel\n";
}
location /api {
dyn_module_api;
}
location /doc {
doc_api;
}
location /metrics {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
location /stream_metrics {
stream_server_traffic_status_display;
stream_server_traffic_status_display_format html;
}
}
}
4.4.动态配置
NJet 能够动态配置stream server是否采集指标。
要使用动态配置功能,njet.conf 中需要加载sts 动态配置模块:
load_module /usr/local/njet/modules/njt_stream_dyn_sts_module.so;
动态配置API 报文的Schema 定义如下:
{
"$id": "dynsts",
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"type": "object",
"required": ["servers"],
"properties": {
"servers": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["listens", "server_traffic_status"],
"properties": {
"listens": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
},
"server_traffic_status": {
"type": "boolean"
},
"server_traffic_status_filter_by_set_key": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
}
}
}
}
}
}
报文样例:
{
"servers": [
{
"listens": [
"0.0.0.0:18082"
],
"server_traffic_status": true,
"server_traffic_status_filter_by_set_key":
[
"* $server_addr"
]
}
]
}
使用GET 查询当前配置
curl localhost:8081/api/v1/config/stream_dyn_sts
使用PUT 对配置进行更新
curl -X PUT -d
'{"servers":[{"listens":["0.0.0.0:1234"],"server_traffic_status":false,"server_traffic_status_filter_by_set_key":["\"*\" \"$server_addr\""]},{"listens":["0.0.0.0:1235"],"server_traffic_status":true,"server_traffic_status_filter_by_set_key":[]}]}'
localhost:8081/api/v1/config/stream_dyn_sts