Skip to content

Commit

Permalink
fix(metrics): correct metrics service name when traffic was send to c…
Browse files Browse the repository at this point in the history
…anary service (kubernetes#10)

Co-authored-by: Lisheng Zheng <zhenglisheng@caicloud.io>
  • Loading branch information
2 people authored and Huanle Han committed Jan 5, 2021
1 parent d7c1bf4 commit 0ffc278
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
33 changes: 31 additions & 2 deletions rootfs/etc/nginx/lua/monitor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 "-",
Expand Down Expand Up @@ -66,7 +95,6 @@ local function flush(premature)
ngx.log(ngx.ERR, "error while encoding metrics: ", err)
return
end

send(payload)
end

Expand Down Expand Up @@ -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
26 changes: 26 additions & 0 deletions rootfs/etc/nginx/lua/test/monitor_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0ffc278

Please sign in to comment.