Skip to content

Commit

Permalink
fix: avoid recording duplicate metrics
Browse files Browse the repository at this point in the history
Problem:
All metrics are recorded by incrementing counters in a shared dictionary.
As the number of metrics increase, so does the contention among Nginx
worker process to increase those counters increases leading to higher
CPU utilization(#2) and potentially will result in a drop in peak RPS.
This also affects the collection phase of metrics where the shared
dictionary needs to be locked for a longer time.

Solution:
Since half of the metrics being recorded are duplicate anyways, those
metrics can be dropped. This doesn't result in any loss functionally
since these metrics can be easily in Prometheus.

From #8
  • Loading branch information
hbagdi authored Aug 3, 2018
1 parent 7e940c0 commit a44518e
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 19 deletions.
16 changes: 0 additions & 16 deletions kong/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ local function init()
{"state"})
metrics.db_reachable = prometheus:gauge("datastore_reachable",
"Datastore reachable from Kong, 0 is unreachable")
metrics.status_total = prometheus:counter("http_status_total",
"HTTP status codes aggreggated across all services in Kong",
{"code"})
metrics.latency_total = prometheus:histogram("latency_total",
"Latency added by Kong, total request time and upstream latency aggreggated across all services in Kong",
{"type"},
DEFAULT_BUCKETS) -- TODO make this configurable
metrics.bandwidth_total = prometheus:counter("bandwidth_total",
"Total bandwidth in bytes for all proxied requests in Kong",
{"type"})

-- per service
metrics.status = prometheus:counter("http_status",
Expand Down Expand Up @@ -67,36 +57,30 @@ local function log(message)
service_name = service_name or ""

metrics.status:inc(1, { message.response.status, service_name })
metrics.status_total:inc(1, { message.response.status })

local request_size = tonumber(message.request.size)
if request_size and request_size > 0 then
metrics.bandwidth:inc(request_size, { "ingress", service_name })
metrics.bandwidth_total:inc(request_size, { "ingress" })
end

local response_size = tonumber(message.response.size)
if response_size and response_size > 0 then
metrics.bandwidth:inc(response_size, { "egress", service_name })
metrics.bandwidth_total:inc(response_size, { "egress" })
end

local request_latency = tonumber(message.latencies.request)
if request_latency and request_latency >= 0 then
metrics.latency:observe(request_latency, { "request", service_name })
metrics.latency_total:observe(request_latency, { "request" })
end

local upstream_latency = tonumber(message.latencies.proxy)
if upstream_latency ~= nil and upstream_latency >= 0 then
metrics.latency:observe(upstream_latency, {"upstream", service_name })
metrics.latency_total:observe(upstream_latency, { "upstream" })
end

local kong_proxy_latency = tonumber(message.latencies.kong)
if kong_proxy_latency ~= nil and kong_proxy_latency >= 0 then
metrics.latency:observe(kong_proxy_latency, { "kong", service_name })
metrics.latency_total:observe(kong_proxy_latency, { "kong" })
end
end

Expand Down
2 changes: 0 additions & 2 deletions spec/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ describe("Plugin: prometheus (access)", function()
path = "/metrics",
})
local body = assert.res_status(200, res)
assert.matches('kong_http_status_total{code="200"} 1', body, nil, true)
assert.matches('kong_http_status{code="200",service="mock-service"} 1', body, nil, true)

ngx.sleep(1)
Expand All @@ -75,7 +74,6 @@ describe("Plugin: prometheus (access)", function()
path = "/metrics",
})
local body = assert.res_status(200, res)
assert.matches('kong_http_status_total{code="400"} 1', body, nil, true)
assert.matches('kong_http_status{code="400",service="mock-service"} 1', body, nil, true)
end)
end)
1 change: 0 additions & 1 deletion spec/03-custom-serve_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe("Plugin: prometheus (custom server)",function()
path = "/metrics",
})
local body = assert.res_status(200, res)
assert.matches('kong_http_status_total{code="200"} 1', body, nil, true)
assert.matches('kong_http_status{code="200",service="mock-service"} 1', body, nil, true)
end)
end)
Expand Down

0 comments on commit a44518e

Please sign in to comment.