From 7732e44cb45f40e7e673a8278fdb7e990f85a5b1 Mon Sep 17 00:00:00 2001 From: owl Date: Wed, 18 Oct 2023 15:42:25 +0800 Subject: [PATCH 1/6] fix(prometheus): expose metrics in no service route --- kong/plugins/prometheus/exporter.lua | 5 +- .../26-prometheus/05-metrics_spec.lua | 52 +++++++++++++++++++ spec/fixtures/blueprints.lua | 11 +++- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/kong/plugins/prometheus/exporter.lua b/kong/plugins/prometheus/exporter.lua index c6e1635b9f9..051fe1efcdf 100644 --- a/kong/plugins/prometheus/exporter.lua +++ b/kong/plugins/prometheus/exporter.lua @@ -273,12 +273,9 @@ 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 diff --git a/spec/03-plugins/26-prometheus/05-metrics_spec.lua b/spec/03-plugins/26-prometheus/05-metrics_spec.lua index a47a7e0b221..877ad4d5937 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" + } + }) + local body = 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 8b27bf22a55..c1662b00d71 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) From f0dac19f5e9af2e7d2f9692ea4cb6fbfcc9e8401 Mon Sep 17 00:00:00 2001 From: owl Date: Wed, 18 Oct 2023 15:44:15 +0800 Subject: [PATCH 2/6] fix(prometheus): fix code --- changelog/unreleased/kong/11781.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/unreleased/kong/11781.yml diff --git a/changelog/unreleased/kong/11781.yml b/changelog/unreleased/kong/11781.yml new file mode 100644 index 00000000000..e0a9441e9e0 --- /dev/null +++ b/changelog/unreleased/kong/11781.yml @@ -0,0 +1,3 @@ +message: "Expose metrics in no service route" +type: fix +scope: Plugin From 895089e6fa4d1ec16c6dedce9b705341ced55983 Mon Sep 17 00:00:00 2001 From: owl Date: Wed, 18 Oct 2023 15:45:38 +0800 Subject: [PATCH 3/6] fix(prometheus): fix code --- changelog/unreleased/kong/11781.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/11781.yml b/changelog/unreleased/kong/11781.yml index e0a9441e9e0..f5d08c71552 100644 --- a/changelog/unreleased/kong/11781.yml +++ b/changelog/unreleased/kong/11781.yml @@ -1,3 +1,3 @@ message: "Expose metrics in no service route" -type: fix +type: bugfix scope: Plugin From 1f6e35d583b59304c6c9c312a935b6360eecbe8a Mon Sep 17 00:00:00 2001 From: owl Date: Wed, 18 Oct 2023 15:47:33 +0800 Subject: [PATCH 4/6] fix(prometheus): fix code --- .../kong/{11781.yml => prometheus_expose_no_service_metrics.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/unreleased/kong/{11781.yml => prometheus_expose_no_service_metrics.yml} (100%) diff --git a/changelog/unreleased/kong/11781.yml b/changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml similarity index 100% rename from changelog/unreleased/kong/11781.yml rename to changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml From 26bdfa6011270a8b19ab65d56926dc2e0614f12f Mon Sep 17 00:00:00 2001 From: owl Date: Wed, 18 Oct 2023 16:24:54 +0800 Subject: [PATCH 5/6] fix(prometheus): fix code --- kong/plugins/prometheus/exporter.lua | 2 ++ spec/03-plugins/26-prometheus/05-metrics_spec.lua | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/kong/plugins/prometheus/exporter.lua b/kong/plugins/prometheus/exporter.lua index 051fe1efcdf..2db6c8a25f0 100644 --- a/kong/plugins/prometheus/exporter.lua +++ b/kong/plugins/prometheus/exporter.lua @@ -281,6 +281,8 @@ local function log(message, serialized) 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 877ad4d5937..a6d56b808b0 100644 --- a/spec/03-plugins/26-prometheus/05-metrics_spec.lua +++ b/spec/03-plugins/26-prometheus/05-metrics_spec.lua @@ -185,7 +185,7 @@ for _, strategy in helpers.each_strategy() do ["Host"] = "status.example.com" } }) - local body = assert.res_status(200, res) + assert.res_status(200, res) local res = assert(proxy_ssl_client:send{ method = "GET", From 834963eec7b1b01478c2832e33bcabe224706c10 Mon Sep 17 00:00:00 2001 From: oowl Date: Wed, 18 Oct 2023 17:53:27 +0800 Subject: [PATCH 6/6] Update changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml Co-authored-by: Datong Sun --- .../unreleased/kong/prometheus_expose_no_service_metrics.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml b/changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml index f5d08c71552..e16c228eaed 100644 --- a/changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml +++ b/changelog/unreleased/kong/prometheus_expose_no_service_metrics.yml @@ -1,3 +1,3 @@ -message: "Expose metrics in no service route" +message: "Expose metrics for serviceless routes" type: bugfix scope: Plugin