Skip to content

wsjhk/nginx-custom-metrics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ngxcustom-metrics代码是将ingress-nginx-controller中的metrics监控相关的代码抽取出来后修改为可独立运行的代码,用于集成到nginx中来做相关的监控。

部署ngxcustom-metrics:
1)将lua脚本拷贝到nginx-tmpl.conf配置的对应目录下;

2)配置nginx的/nginx_status模块和端口(注意这个端口和uri的配置,需要和如下环境变量一致);

3)Nginx的deployment中添加如下环境变量:
    //MetricsPerHost=true
    //EnableMetrics=true
    ListenPorts="8888"
    //POD_NAMESPACE="slb-nginx"
    //POD_NAME="slb-nginx-demo-01"
    NGINX_CLASS="slb-nginx-demo"

4)nginx.conf的http模块下配置如下内容(全局生效):
log_by_lua_block {
    monitor.call()
    plugins.run()
}
或者将block的两行代码写入一个log_by_lua_monitor_call.lua文件,然后在nginx.conf中通过
log_by_lua_file '/usr/local/nginx/lua/log_by_lua_monitor_call.lua';全局引入生效。

5)nginx.conf的配置参考nginx-tmpl.conf


编译:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ngxcustom-metrics nginx/main.go



优化:
1.程序启动后内存一直不断上升,直到OOM。通过go tool pprof分析后是NewHistogramVec和NewSummaryVec导致的。因为metrics收集的字段中包含了path,数量太多。
因为是一层的监控,所以监控粒度可以不那么小,故将其去掉。
```
diff --git a/metric/collectors/socket.go b/metric/collectors/socket.go
index 3fc50fd..75b4cd6 100644
--- a/metric/collectors/socket.go
+++ b/metric/collectors/socket.go
@@ -77,7 +77,7 @@ var (
        requestTags = []string{
                "status",
                "method",
-               "path",
+               //"path",
                "upstream_host",
                "upstream_status",
        }
@@ -190,7 +190,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost bool) (*Soc
                                ConstLabels: constLabels,
                                Objectives:  defObjectives,
                        },
-                       []string{"host", "path", "upstream_host", "upstream_status"},
+                       []string{"host", "upstream_host", "upstream_status"},
                ),
        }

@@ -231,7 +231,7 @@ func (sc *SocketCollector) handleMessage(msg []byte) {
                requestLabels := prometheus.Labels{
                        "status":    stats.Status,
                        "method":    stats.Method,
-                       "path":      stats.Path,
+                       //"path":      stats.Path,
                        "upstream_host":        stats.UpstreamAddr,
                        "upstream_status":      stats.UpstreamStatus,
                }
@@ -248,7 +248,7 @@ func (sc *SocketCollector) handleMessage(msg []byte) {

                latencyLabels := prometheus.Labels{
                        "host": stats.Host,
-                       "path": stats.Path,
+                       //"path": stats.Path,
                        "upstream_host":        stats.UpstreamAddr,
                        "upstream_status":      stats.UpstreamStatus,
                }
diff --git a/monitor.lua b/monitor.lua
index 48909ea..b69559c 100644
--- a/monitor.lua
+++ b/monitor.lua
@@ -31,7 +31,7 @@ local function metrics()
   return {
     host = ngx.var.host or "-",
     namespace = ngx.var.namespace or "-",
-    path = ngx.var.request_uri or "-",
+--     path = ngx.var.request_uri or "-",

     method = ngx.var.request_method or "-",
     status = ngx.var.status or "-",
diff --git a/ngxcustom-metrics b/ngxcustom-metrics
index 21b7e5b..68090bd 100755
```
做如上变更后,重新编译后内存较稳定。

About

get metrics Like nginx-ingress-controller metrics code

Resources

Stars

Watchers

Forks

Packages

No packages published