Skip to content

Commit ad7db29

Browse files
ProBriangithub-actions[bot]
authored andcommitted
fix(plugin): fix ai latency metric doesn't correctly calculated issue (#14639)
KAG-7113 (cherry picked from commit b7ab58b)
1 parent e9e5900 commit ad7db29

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

kong/plugins/prometheus/exporter.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,15 @@ local function log(message, serialized)
364364
metrics.ai_llm_cost:inc(ai_metrics.usage.cost, labels_table_ai_llm_status)
365365
end
366366

367-
if ai_metrics.meta and ai_metrics.meta.llm_latency and ai_metrics.meta.llm_latency > 0 then
367+
if ai_metrics.meta and ai_metrics.meta.llm_latency and ai_metrics.meta.llm_latency >= 0 then
368368
metrics.ai_llm_provider_latency:observe(ai_metrics.meta.llm_latency, labels_table_ai_llm_status)
369369
end
370370

371-
if ai_metrics.cache and ai_metrics.cache.fetch_latency and ai_metrics.cache.fetch_latency > 0 then
371+
if ai_metrics.cache and ai_metrics.cache.fetch_latency and ai_metrics.cache.fetch_latency >= 0 then
372372
metrics.ai_cache_fetch_latency:observe(ai_metrics.cache.fetch_latency, labels_table_ai_llm_status)
373373
end
374374

375-
if ai_metrics.cache and ai_metrics.cache.embeddings_latency and ai_metrics.cache.embeddings_latency > 0 then
375+
if ai_metrics.cache and ai_metrics.cache.embeddings_latency and ai_metrics.cache.embeddings_latency >= 0 then
376376
metrics.ai_cache_embeddings_latency:observe(ai_metrics.cache.embeddings_latency, labels_table_ai_llm_status)
377377
end
378378

spec/03-plugins/26-prometheus/02-access_spec.lua

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local helpers = require "spec.helpers"
22
local shell = require "resty.shell"
33
local pl_file = require "pl.file"
4+
local timeout = 10
5+
local step = 1
46

57
local tcp_service_port = helpers.get_available_port()
68
local tcp_proxy_port = helpers.get_available_port()
@@ -814,14 +816,16 @@ describe("Plugin: prometheus (access) AI metrics", function()
814816
assert.res_status(200, res)
815817

816818
local body
819+
-- wait until the histogram observe finished and get the correct metrics.
817820
helpers.wait_until(function()
818821
local res = assert(admin_client:send {
819822
method = "GET",
820823
path = "/metrics",
821824
})
822825
body = assert.res_status(200, res)
823-
return res.status == 200
824-
end)
826+
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',
827+
nil, true)
828+
end, timeout, step)
825829

826830
assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
827831
assert.matches('http_requests_total{service="empty_service",route="http-route",code="200",source="service",workspace="default",consumer=""} 2', body, nil, true)
@@ -858,8 +862,9 @@ describe("Plugin: prometheus (access) AI metrics", function()
858862
path = "/metrics",
859863
})
860864
body = assert.res_status(200, res)
861-
return res.status == 200
862-
end)
865+
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',
866+
nil, true)
867+
end, timeout, step)
863868

864869
assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
865870
assert.matches('http_requests_total{service="empty_service",route="http-route",code="200",source="service",workspace="default",consumer=""} 3', body, nil, true)
@@ -892,8 +897,9 @@ describe("Plugin: prometheus (access) AI metrics", function()
892897
path = "/metrics",
893898
})
894899
body = assert.res_status(200, res)
895-
return res.status == 200
896-
end)
900+
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',
901+
nil, true)
902+
end, timeout, step)
897903

898904
assert.matches('http_requests_total{service="empty_service",route="http-route",code="400",source="kong",workspace="default",consumer=""} 1', body, nil, true)
899905
assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)

0 commit comments

Comments
 (0)