diff --git a/changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml b/changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml new file mode 100644 index 000000000000..e16c228eaed4 --- /dev/null +++ b/changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml @@ -0,0 +1,3 @@ +message: "Expose metrics for serviceless routes" +type: bugfix +scope: Plugin diff --git a/kong/plugins/prometheus/exporter.lua b/kong/plugins/prometheus/exporter.lua index c6e1635b9f9f..2db6c8a25f03 100644 --- a/kong/plugins/prometheus/exporter.lua +++ b/kong/plugins/prometheus/exporter.lua @@ -273,17 +273,16 @@ local function log(message, serialized) return end - local service_name + local service_name = "" if message and message.service then service_name = message.service.name or message.service.host - else - -- do not record any stats if the service is not present - return end local route_name if message and message.route then route_name = message.route.name or message.route.id + else + return end local consumer = "" diff --git a/spec/03-plugins/26-prometheus/05-metrics_spec.lua b/spec/03-plugins/26-prometheus/05-metrics_spec.lua index a47a7e0b221b..a6d56b808b01 100644 --- a/spec/03-plugins/26-prometheus/05-metrics_spec.lua +++ b/spec/03-plugins/26-prometheus/05-metrics_spec.lua @@ -64,6 +64,25 @@ for _, strategy in helpers.each_strategy() do } } + local route1 = bp.routes:insert{ + name = "serverless", + protocols = {"https"}, + hosts = {"status.example.com"}, + paths = {"/serverless"}, + no_service = true, + } + + assert(bp.plugins:insert { + name = "request-termination", + route = { id = route1.id }, + config = { + status_code = 200, + message = "request terminated by request-termination plugin", + echo = true, + }, + }) + + bp.plugins:insert{ name = "prometheus", -- globally enabled config = { @@ -158,5 +177,38 @@ for _, strategy in helpers.each_strategy() do assert.matches('kong_nginx_connections_total{node_id="' .. UUID_PATTERN .. '",subsystem="' .. ngx.config.subsystem .. '",state="%w+"} %d+', body) end) + it("expose metrics in no service route", function() + local res = assert(proxy_ssl_client:send{ + method = "GET", + path = "/serverless", + headers = { + ["Host"] = "status.example.com" + } + }) + assert.res_status(200, res) + + local res = assert(proxy_ssl_client:send{ + method = "GET", + path = "/metrics", + headers = { + ["Host"] = "status.example.com" + } + }) + assert.res_status(200, res) + + helpers.wait_until(function() + local res = assert(admin_ssl_client:send{ + method = "GET", + path = "/metrics" + }) + local body = assert.res_status(200, res) + + assert.matches('kong_nginx_metric_errors_total 0', body, nil, true) + + return body:find('kong_http_requests_total{service="",route="serverless",code="200",source="kong",consumer=""} 1', + nil, true) + end) + end) + end) end diff --git a/spec/fixtures/blueprints.lua b/spec/fixtures/blueprints.lua index 8b27bf22a55c..c1662b00d710 100644 --- a/spec/fixtures/blueprints.lua +++ b/spec/fixtures/blueprints.lua @@ -180,8 +180,15 @@ function _M.new(db) end) res.routes = new_blueprint(db.routes, function(overrides) - return { - service = overrides.service or res.services:insert(), + local service + if overrides.no_service then + service = nil + overrides.no_service = nil + else + service = overrides.service or res.services:insert() + end + return { + service = service, } end)