diff --git a/kong/plugins/prometheus/exporter.lua b/kong/plugins/prometheus/exporter.lua index 7b4abdb33e7..d5100070b1f 100644 --- a/kong/plugins/prometheus/exporter.lua +++ b/kong/plugins/prometheus/exporter.lua @@ -401,15 +401,15 @@ local function log(message, serialized) metrics.ai_llm_cost:inc(ai_metrics.usage.cost, labels_table_ai_llm_status) end - if ai_metrics.meta and ai_metrics.meta.llm_latency and ai_metrics.meta.llm_latency > 0 then + if ai_metrics.meta and ai_metrics.meta.llm_latency and ai_metrics.meta.llm_latency >= 0 then metrics.ai_llm_provider_latency:observe(ai_metrics.meta.llm_latency, labels_table_ai_llm_status) end - if ai_metrics.cache and ai_metrics.cache.fetch_latency and ai_metrics.cache.fetch_latency > 0 then + if ai_metrics.cache and ai_metrics.cache.fetch_latency and ai_metrics.cache.fetch_latency >= 0 then metrics.ai_cache_fetch_latency:observe(ai_metrics.cache.fetch_latency, labels_table_ai_llm_status) end - if ai_metrics.cache and ai_metrics.cache.embeddings_latency and ai_metrics.cache.embeddings_latency > 0 then + if ai_metrics.cache and ai_metrics.cache.embeddings_latency and ai_metrics.cache.embeddings_latency >= 0 then metrics.ai_cache_embeddings_latency:observe(ai_metrics.cache.embeddings_latency, labels_table_ai_llm_status) end diff --git a/spec/03-plugins/26-prometheus/02-access_spec.lua b/spec/03-plugins/26-prometheus/02-access_spec.lua index 31e588435f7..627a07eda0f 100644 --- a/spec/03-plugins/26-prometheus/02-access_spec.lua +++ b/spec/03-plugins/26-prometheus/02-access_spec.lua @@ -1,6 +1,8 @@ local helpers = require "spec.helpers" local shell = require "resty.shell" local pl_file = require "pl.file" +local timeout = 10 +local step = 1 local UUID_PATTERN = "%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x" @@ -820,14 +822,16 @@ describe("Plugin: prometheus (access) AI metrics", function() assert.res_status(200, res) local body + -- wait until the histogram observe finished and get the correct metrics. helpers.wait_until(function() local res = assert(admin_client:send { method = "GET", path = "/metrics", }) body = assert.res_status(200, res) - return res.status == 200 - end) + return body:find('ai_llm_provider_latency_ms_bucket{ai_provider="openai",ai_model="gpt-3.5-turbo",cache_status="",vector_db="",embeddings_provider="",embeddings_model="",workspace="default",le="+Inf"} 1', + nil, true) + end, timeout, step) assert.matches('kong_nginx_metric_errors_total 0', body, nil, true) assert.matches('http_requests_total{service="empty_service",route="http-route",code="200",source="service",workspace="default",consumer=""} 2', body, nil, true) @@ -864,8 +868,9 @@ describe("Plugin: prometheus (access) AI metrics", function() path = "/metrics", }) body = assert.res_status(200, res) - return res.status == 200 - end) + return body:find('ai_llm_provider_latency_ms_bucket{ai_provider="openai",ai_model="gpt-3.5-turbo",cache_status="",vector_db="",embeddings_provider="",embeddings_model="",workspace="default",le="+Inf"} 2', + nil, true) + end, timeout, step) assert.matches('kong_nginx_metric_errors_total 0', body, nil, true) assert.matches('http_requests_total{service="empty_service",route="http-route",code="200",source="service",workspace="default",consumer=""} 3', body, nil, true) @@ -898,8 +903,9 @@ describe("Plugin: prometheus (access) AI metrics", function() path = "/metrics", }) body = assert.res_status(200, res) - return res.status == 200 - end) + return body:find('ai_llm_provider_latency_ms_bucket{ai_provider="openai",ai_model="gpt-3.5-turbo",cache_status="",vector_db="",embeddings_provider="",embeddings_model="",workspace="default",le="+Inf"} 2', + nil, true) + end, timeout, step) assert.matches('http_requests_total{service="empty_service",route="http-route",code="400",source="kong",workspace="default",consumer=""} 1', body, nil, true) assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)