Skip to content

Commit b7ab58b

Browse files
authored
fix(plugin): fix ai latency metric doesn't correctly calculated issue (#14639)
KAG-7113
1 parent be1b3ca commit b7ab58b

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
@@ -401,15 +401,15 @@ local function log(message, serialized)
401401
metrics.ai_llm_cost:inc(ai_metrics.usage.cost, labels_table_ai_llm_status)
402402
end
403403

404-
if ai_metrics.meta and ai_metrics.meta.llm_latency and ai_metrics.meta.llm_latency > 0 then
404+
if ai_metrics.meta and ai_metrics.meta.llm_latency and ai_metrics.meta.llm_latency >= 0 then
405405
metrics.ai_llm_provider_latency:observe(ai_metrics.meta.llm_latency, labels_table_ai_llm_status)
406406
end
407407

408-
if ai_metrics.cache and ai_metrics.cache.fetch_latency and ai_metrics.cache.fetch_latency > 0 then
408+
if ai_metrics.cache and ai_metrics.cache.fetch_latency and ai_metrics.cache.fetch_latency >= 0 then
409409
metrics.ai_cache_fetch_latency:observe(ai_metrics.cache.fetch_latency, labels_table_ai_llm_status)
410410
end
411411

412-
if ai_metrics.cache and ai_metrics.cache.embeddings_latency and ai_metrics.cache.embeddings_latency > 0 then
412+
if ai_metrics.cache and ai_metrics.cache.embeddings_latency and ai_metrics.cache.embeddings_latency >= 0 then
413413
metrics.ai_cache_embeddings_latency:observe(ai_metrics.cache.embeddings_latency, labels_table_ai_llm_status)
414414
end
415415

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 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"
68

@@ -820,14 +822,16 @@ describe("Plugin: prometheus (access) AI metrics", function()
820822
assert.res_status(200, res)
821823

822824
local body
825+
-- wait until the histogram observe finished and get the correct metrics.
823826
helpers.wait_until(function()
824827
local res = assert(admin_client:send {
825828
method = "GET",
826829
path = "/metrics",
827830
})
828831
body = assert.res_status(200, res)
829-
return res.status == 200
830-
end)
832+
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',
833+
nil, true)
834+
end, timeout, step)
831835

832836
assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
833837
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()
864868
path = "/metrics",
865869
})
866870
body = assert.res_status(200, res)
867-
return res.status == 200
868-
end)
871+
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',
872+
nil, true)
873+
end, timeout, step)
869874

870875
assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
871876
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()
898903
path = "/metrics",
899904
})
900905
body = assert.res_status(200, res)
901-
return res.status == 200
902-
end)
906+
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',
907+
nil, true)
908+
end, timeout, step)
903909

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

0 commit comments

Comments
 (0)