Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions kong/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 12 additions & 6 deletions spec/03-plugins/26-prometheus/02-access_spec.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading