From 0ffc27874eb372a567c91965a1baca0047287a6b Mon Sep 17 00:00:00 2001 From: Lisheng Zheng Date: Mon, 29 Jun 2020 19:05:08 +0800 Subject: [PATCH] fix(metrics): correct metrics service name when traffic was send to canary service (#10) Co-authored-by: Lisheng Zheng --- rootfs/etc/nginx/lua/monitor.lua | 33 ++++++++++++++++++++-- rootfs/etc/nginx/lua/test/monitor_test.lua | 26 +++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/rootfs/etc/nginx/lua/monitor.lua b/rootfs/etc/nginx/lua/monitor.lua index 1c0672e4173..191e6815795 100644 --- a/rootfs/etc/nginx/lua/monitor.lua +++ b/rootfs/etc/nginx/lua/monitor.lua @@ -27,12 +27,41 @@ local function send(payload) assert(s:close()) end +-- when the traffic was send to canary service, the canary service name is more appropriate for metrics. +local function get_service_name(namespace, serviceName, alternativeUpstreamName) + local svcName = serviceName + -- the variable indicates the traffic if be send to the canary service + if namespace and alternativeUpstreamName and #alternativeUpstreamName > #namespace then + -- the format of alternativeUpstreamName is `ns-svc-port` + local endPos = #alternativeUpstreamName + while endPos > 1 do + if string.sub(alternativeUpstreamName, endPos, endPos) == "-" then + endPos = endPos - 1 + break + end + endPos = endPos - 1 + end + + local startPos = #namespace + 2 + if string.sub(alternativeUpstreamName, 1, #namespace) ~= namespace then + return svcName + end + + if startPos <= endPos then + svcName = string.sub(alternativeUpstreamName, startPos, endPos) + end + end + return svcName +end + local function metrics() + local serviceName = get_service_name(ngx.var.namespace, ngx.var.service_name, ngx.var.proxy_alternative_upstream_name) + return { host = ngx.var.host or "-", namespace = ngx.var.namespace or "-", ingress = ngx.var.ingress_name or "-", - service = ngx.var.service_name or "-", + service = serviceName or "-", path = ngx.var.location_path or "-", method = ngx.var.request_method or "-", @@ -66,7 +95,6 @@ local function flush(premature) ngx.log(ngx.ERR, "error while encoding metrics: ", err) return end - send(payload) end @@ -98,6 +126,7 @@ setmetatable(_M, {__index = { flush = flush, set_metrics_max_batch_size = set_metrics_max_batch_size, get_metrics_batch = function() return metrics_batch end, + get_service_name = get_service_name, }}) return _M diff --git a/rootfs/etc/nginx/lua/test/monitor_test.lua b/rootfs/etc/nginx/lua/test/monitor_test.lua index 78efed9390f..623b9228856 100644 --- a/rootfs/etc/nginx/lua/test/monitor_test.lua +++ b/rootfs/etc/nginx/lua/test/monitor_test.lua @@ -150,4 +150,30 @@ describe("Monitor", function() assert.stub(tcp_mock.close).was_called_with(tcp_mock) end) end) + + describe("get_service_name", function() + it("get alternative service name)", function() + local monitor = require("monitor") + local service = monitor.get_service_name("be", "mainline", "be-canary-8080") + assert.equal(service, "canary") + end) + + it("get mainline service name", function() + local monitor = require("monitor") + local service = monitor.get_service_name("be", "mainline", "") + assert.equal(service, "mainline") + end) + + it("mismatch namespace", function() + local monitor = require("monitor") + local service = monitor.get_service_name("be", "mainline", "fe-canary-8080") + assert.equal(service, "mainline") + end) + + it("special character", function() + local monitor = require("monitor") + local service = monitor.get_service_name("be-fe", "main-line", "be-fe-canary-service-8080") + assert.equal(service, "canary-service") + end) + end) end)