Skip to content

Commit

Permalink
rename with llm prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineJac committed Jun 7, 2024
1 parent d284742 commit 008c3bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
46 changes: 23 additions & 23 deletions kong/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ local function init()

-- AI mode
if ai_request then
metrics.ai_requests = prometheus:counter("ai_requests_total",
metrics.ai_llm_requests = prometheus:counter("ai_llm_requests_total",
"AI requests total per ai_provider in Kong",
{"ai_provider", "ai_model", "cache", "db_name", "workspace"})

metrics.ai_cost = prometheus:counter("ai_cost_total",
metrics.ai_llm_cost = prometheus:counter("ai_llm_cost_total",
"AI requests cost per ai_provider/cache in Kong",
{"ai_provider", "ai_model", "cache", "db_name", "workspace"})

metrics.ai_tokens = prometheus:counter("ai_tokens_total",
metrics.ai_llm_tokens = prometheus:counter("ai_llm_tokens_total",
"AI requests cost per ai_provider/cache in Kong",
{"ai_provider", "ai_model", "cache", "db_name", "token_type", "workspace"})
end
Expand Down Expand Up @@ -225,8 +225,8 @@ local upstream_target_addr_health_table = {
{ value = 0, labels = { 0, 0, 0, "dns_error", ngx.config.subsystem } },
}
-- ai
local labels_table_ai_status = {0, 0, 0, 0, 0}
local labels_table_ai_tokens = {0, 0, 0, 0, 0, 0}
local labels_table_ai_llm_status = {0, 0, 0, 0, 0}
local labels_table_ai_llm_tokens = {0, 0, 0, 0, 0, 0}

local function set_healthiness_metrics(table, upstream, target, address, status, metrics_bucket)
for i = 1, #table do
Expand Down Expand Up @@ -346,36 +346,36 @@ local function log(message, serialized)
db_name = ai_plugin.cache.db_name
end

labels_table_ai_status[1] = ai_plugin.meta.provider_name
labels_table_ai_status[2] = ai_plugin.meta.request_model
labels_table_ai_status[3] = cache_type
labels_table_ai_status[4] = db_name
labels_table_ai_status[5] = workspace
metrics.ai_requests:inc(1, labels_table_ai_status)
labels_table_ai_llm_status[1] = ai_plugin.meta.provider_name
labels_table_ai_llm_status[2] = ai_plugin.meta.request_model
labels_table_ai_llm_status[3] = cache_type
labels_table_ai_llm_status[4] = db_name
labels_table_ai_llm_status[5] = workspace
metrics.ai_llm_requests:inc(1, labels_table_ai_llm_status)

if ai_plugin.usage.cost_request and ai_plugin.usage.cost_request > 0 then
metrics.ai_cost:inc(ai_plugin.usage.cost_request, labels_table_ai_status)
metrics.ai_llm_cost:inc(ai_plugin.usage.cost_request, labels_table_ai_llm_status)
end

labels_table_ai_tokens[1] = ai_plugin.meta.provider_name
labels_table_ai_tokens[2] = ai_plugin.meta.request_model
labels_table_ai_tokens[3] = cache_type
labels_table_ai_tokens[4] = db_name
labels_table_ai_tokens[6] = workspace
labels_table_ai_llm_tokens[1] = ai_plugin.meta.provider_name
labels_table_ai_llm_tokens[2] = ai_plugin.meta.request_model
labels_table_ai_llm_tokens[3] = cache_type
labels_table_ai_llm_tokens[4] = db_name
labels_table_ai_llm_tokens[6] = workspace

if ai_plugin.usage.prompt_tokens and ai_plugin.usage.prompt_tokens > 0 then
labels_table_ai_tokens[5] = "prompt_tokens"
metrics.ai_tokens:inc(ai_plugin.usage.prompt_tokens, labels_table_ai_tokens)
labels_table_ai_llm_tokens[5] = "prompt_tokens"
metrics.ai_llm_tokens:inc(ai_plugin.usage.prompt_tokens, labels_table_ai_llm_tokens)
end

if ai_plugin.usage.completion_tokens and ai_plugin.usage.completion_tokens > 0 then
labels_table_ai_tokens[5] = "completion_tokens"
metrics.ai_tokens:inc(ai_plugin.usage.completion_tokens, labels_table_ai_tokens)
labels_table_ai_llm_tokens[5] = "completion_tokens"
metrics.ai_llm_tokens:inc(ai_plugin.usage.completion_tokens, labels_table_ai_llm_tokens)
end

if ai_plugin.usage.total_tokens and ai_plugin.usage.total_tokens > 0 then
labels_table_ai_tokens[5] = "total_tokens"
metrics.ai_tokens:inc(ai_plugin.usage.total_tokens, labels_table_ai_tokens)
labels_table_ai_llm_tokens[5] = "total_tokens"
metrics.ai_llm_tokens:inc(ai_plugin.usage.total_tokens, labels_table_ai_llm_tokens)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/prometheus/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ return {
fields = {
{ per_consumer = { description = "A boolean value that determines if per-consumer metrics should be collected. If enabled, the `kong_http_requests_total` and `kong_bandwidth_bytes` metrics fill in the consumer label when available.", type = "boolean", default = false }, },
{ status_code_metrics = { description = "A boolean value that determines if status code metrics should be collected. If enabled, `http_requests_total`, `stream_sessions_total` metrics will be exported.", type = "boolean", default = false }, },
{ ai_metrics = { description = "A boolean value that determines if ai metrics should be collected. If enabled, the `kong_ai_requests_total`, `kong_ai_cost_total`, `ai_cache_retrieve_latency_ms` and `ai_cache_store_latency_ms` metrics will be exported.", type = "boolean", default = false }, },
{ ai_metrics = { description = "A boolean value that determines if ai metrics should be collected. If enabled, the `kong_ai_llm_requests_total`, `kong_ai_llm_cost_total`, `ai_cache_retrieve_latency_ms` and `ai_cache_store_latency_ms` metrics will be exported.", type = "boolean", default = false }, },
{ latency_metrics = { description = "A boolean value that determines if latency metrics should be collected. If enabled, `kong_latency_ms`, `upstream_latency_ms` and `request_latency_ms` metrics will be exported.", type = "boolean", default = false }, },
{ bandwidth_metrics = { description = "A boolean value that determines if bandwidth metrics should be collected. If enabled, `bandwidth_bytes` and `stream_sessions_total` metrics will be exported.", type = "boolean", default = false }, },
{ upstream_health_metrics = { description = "A boolean value that determines if upstream metrics should be collected. If enabled, `upstream_target_health` metric will be exported.", type = "boolean", default = false }, },
Expand Down
24 changes: 12 additions & 12 deletions spec/03-plugins/26-prometheus/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -764,13 +764,13 @@ describe("Plugin: prometheus (access) AI metrics", function()
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=""} 1', body, nil, true)

assert.matches('ai_requests_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 1', body, nil, true)
assert.matches('ai_llm_requests_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 1', body, nil, true)

assert.matches('ai_cost_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 0.00037', body, nil, true)
assert.matches('ai_llm_cost_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 0.00037', body, nil, true)

assert.matches('ai_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="completion_tokens",workspace="default"} 12', body, nil, true)
assert.matches('ai_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="prompt_tokens",workspace="default"} 25', body, nil, true)
assert.matches('ai_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="total_tokens",workspace="default"} 37', body, nil, true)
assert.matches('ai_llm_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="completion_tokens",workspace="default"} 12', body, nil, true)
assert.matches('ai_llm_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="prompt_tokens",workspace="default"} 25', body, nil, true)
assert.matches('ai_llm_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="total_tokens",workspace="default"} 37', body, nil, true)
end)

it("increments the count for proxied AI requests", function()
Expand Down Expand Up @@ -800,13 +800,13 @@ describe("Plugin: prometheus (access) AI metrics", function()
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)

assert.matches('ai_requests_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 2', body, nil, true)
assert.matches('ai_llm_requests_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 2', body, nil, true)

assert.matches('ai_cost_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 0.00074', body, nil, true)
assert.matches('ai_llm_cost_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 0.00074', body, nil, true)

assert.matches('ai_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="completion_tokens",workspace="default"} 24', body, nil, true)
assert.matches('ai_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="prompt_tokens",workspace="default"} 50', body, nil, true)
assert.matches('ai_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="total_tokens",workspace="default"} 74', body, nil, true)
assert.matches('ai_llm_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="completion_tokens",workspace="default"} 24', body, nil, true)
assert.matches('ai_llm_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="prompt_tokens",workspace="default"} 50', body, nil, true)
assert.matches('ai_llm_tokens_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",token_type="total_tokens",workspace="default"} 74', body, nil, true)
end)

it("behave correctly if AI metrics are not found", function()
Expand All @@ -832,7 +832,7 @@ describe("Plugin: prometheus (access) AI metrics", function()
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)

assert.matches('ai_requests_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 2', body, nil, true)
assert.matches('ai_cost_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 0.00074', body, nil, true)
assert.matches('ai_llm_requests_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 2', body, nil, true)
assert.matches('ai_llm_cost_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache="not_cached",db_name="",workspace="default"} 0.00074', body, nil, true)
end)
end)

0 comments on commit 008c3bb

Please sign in to comment.