Skip to content

Commit

Permalink
tests(prometheus): swap setup/teardown for their lazy variants
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Dec 5, 2024
1 parent 3f058b2 commit f97ab64
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
8 changes: 4 additions & 4 deletions spec/03-plugins/26-prometheus/01-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ describe("Plugin: prometheus (API)",function()
local admin_client

describe("with no 'prometheus_metrics' shm defined", function()
setup(function()
lazy_setup(function()
helpers.get_db_utils()
assert(helpers.start_kong({
plugins = "bundled, prometheus",
}))

admin_client = helpers.admin_client()
end)
teardown(function()
lazy_teardown(function()
if admin_client then
admin_client:close()
end
Expand Down Expand Up @@ -41,15 +41,15 @@ describe("Plugin: prometheus (API)",function()
end)

describe("with 'prometheus_metrics' defined", function()
setup(function()
lazy_setup(function()
helpers.get_db_utils()
assert(helpers.start_kong({
plugins = "bundled, prometheus",
}))

admin_client = helpers.admin_client()
end)
teardown(function()
lazy_teardown(function()
if admin_client then
admin_client:close()
end
Expand Down
46 changes: 23 additions & 23 deletions spec/03-plugins/26-prometheus/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("Plugin: prometheus (access)", function()
local proxy_client_grpc
local proxy_client_grpcs

setup(function()
lazy_setup(function()
local bp = helpers.get_db_utils()

local service = bp.services:insert {
Expand Down Expand Up @@ -89,7 +89,7 @@ describe("Plugin: prometheus (access)", function()
proxy_client_grpcs = helpers.proxy_client_grpcs()
end)

teardown(function()
lazy_teardown(function()
if proxy_client then
proxy_client:close()
end
Expand Down Expand Up @@ -316,7 +316,7 @@ end)
describe("Plugin: prometheus (access) no stream listeners", function()
local admin_client

setup(function()
lazy_setup(function()
local bp = helpers.get_db_utils()

bp.plugins:insert {
Expand All @@ -337,7 +337,7 @@ describe("Plugin: prometheus (access) no stream listeners", function()
admin_client = helpers.admin_client()
end)

teardown(function()
lazy_teardown(function()
if admin_client then
admin_client:close()
end
Expand Down Expand Up @@ -376,7 +376,7 @@ describe("Plugin: prometheus (access) per-consumer metrics", function()
local proxy_client
local admin_client

setup(function()
lazy_setup(function()
local bp = helpers.get_db_utils()

local service = bp.services:insert {
Expand Down Expand Up @@ -428,7 +428,7 @@ describe("Plugin: prometheus (access) per-consumer metrics", function()
admin_client = helpers.admin_client()
end)

teardown(function()
lazy_teardown(function()
if proxy_client then
proxy_client:close()
end
Expand Down Expand Up @@ -523,7 +523,7 @@ describe("Plugin: prometheus (access) granular metrics switch", function()

local success_scrape = ""

setup(function()
lazy_setup(function()
local bp = helpers.get_db_utils()

local service = bp.services:insert {
Expand Down Expand Up @@ -567,7 +567,7 @@ describe("Plugin: prometheus (access) granular metrics switch", function()
admin_client = helpers.admin_client()
end)

teardown(function()
lazy_teardown(function()
if proxy_client then
proxy_client:close()
end
Expand Down Expand Up @@ -619,7 +619,7 @@ describe("Plugin: prometheus (access) AI metrics", function()
local admin_client
local prometheus_plugin

setup(function()
lazy_setup(function()
local bp = helpers.get_db_utils()

local fixtures = {
Expand All @@ -630,27 +630,27 @@ describe("Plugin: prometheus (access) AI metrics", function()
server {
server_name openai;
listen ]]..MOCK_PORT..[[;
default_type 'application/json';
location = "/llm/v1/chat/good" {
content_by_lua_block {
local pl_file = require "pl.file"
local json = require("cjson.safe")
ngx.req.read_body()
local body, err = ngx.req.get_body_data()
body, err = json.decode(body)
local token = ngx.req.get_headers()["authorization"]
local token_query = ngx.req.get_uri_args()["apikey"]
if token == "Bearer openai-key" or token_query == "openai-key" or body.apikey == "openai-key" then
ngx.req.read_body()
local body, err = ngx.req.get_body_data()
body, err = json.decode(body)
if err or (body.messages == ngx.null) then
ngx.status = 400
ngx.print(pl_file.read("spec/fixtures/ai-proxy/openai/llm-v1-chat/responses/bad_request.json"))
Expand All @@ -673,7 +673,7 @@ describe("Plugin: prometheus (access) AI metrics", function()
port = 8080, --MOCK_PORT,
path = "/",
})

-- 200 chat good with one option
local chat_good = assert(bp.routes:insert {
service = empty_service,
Expand Down Expand Up @@ -727,7 +727,7 @@ describe("Plugin: prometheus (access) AI metrics", function()
admin_client = helpers.admin_client()
end)

teardown(function()
lazy_teardown(function()
if proxy_client then
proxy_client:close()
end
Expand Down Expand Up @@ -764,7 +764,7 @@ 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.not_match('ai_llm_requests_total', body, nil, true)
assert.not_match('ai_llm_cost_total', body, nil, true)
assert.not_match('ai_llm_tokens_total', body, nil, true)
Expand Down Expand Up @@ -798,7 +798,7 @@ describe("Plugin: prometheus (access) AI metrics", function()

ngx.sleep(2)
end)

it("add the count for proxied AI requests", function()
local res = assert(proxy_client:send {
method = "GET",
Expand All @@ -825,7 +825,7 @@ 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_llm_requests_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache_status="",vector_db="",embeddings_provider="",embeddings_model="",workspace="default"} 1', body, nil, true)

assert.matches('ai_llm_cost_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache_status="",vector_db="",embeddings_provider="",embeddings_model="",workspace="default"} 0.00037', body, nil, true)
Expand Down Expand Up @@ -863,7 +863,7 @@ 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=""} 3', body, nil, true)

assert.matches('ai_llm_requests_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache_status="",vector_db="",embeddings_provider="",embeddings_model="",workspace="default"} 2', body, nil, true)

assert.matches('ai_llm_cost_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache_status="",vector_db="",embeddings_provider="",embeddings_model="",workspace="default"} 0.00074', body, nil, true)
Expand Down Expand Up @@ -902,4 +902,4 @@ describe("Plugin: prometheus (access) AI metrics", function()
assert.matches('ai_llm_cost_total{ai_provider="openai",ai_model="gpt-3.5-turbo",cache_status="",vector_db="",embeddings_provider="",embeddings_model="",workspace="default"} 0.00074', body, nil, true)
assert.matches('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', body, nil, true)
end)
end)
end)
4 changes: 2 additions & 2 deletions spec/03-plugins/26-prometheus/03-custom-serve_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe("Plugin: prometheus (custom server)",function()
local proxy_client

describe("with custom nginx server block", function()
setup(function()
lazy_setup(function()
local bp = helpers.get_db_utils()

local service = bp.services:insert {
Expand Down Expand Up @@ -39,7 +39,7 @@ describe("Plugin: prometheus (custom server)",function()

proxy_client = helpers.proxy_client()
end)
teardown(function()
lazy_teardown(function()
if proxy_client then
proxy_client:close()
end
Expand Down
8 changes: 4 additions & 4 deletions spec/03-plugins/26-prometheus/04-status_api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("Plugin: prometheus (access via status API)", function()
return assert.res_status(200, res)
end

setup(function()
lazy_setup(function()
local bp = helpers.get_db_utils()

local upstream_hc_off = bp.upstreams:insert({
Expand Down Expand Up @@ -184,7 +184,7 @@ describe("Plugin: prometheus (access via status API)", function()
end
end)

teardown(function()
lazy_teardown(function()
helpers.stop_kong()
end)

Expand Down Expand Up @@ -438,7 +438,7 @@ describe("Plugin: prometheus (access) granular metrics switch", function()

local success_scrape = ""

setup(function()
lazy_setup(function()
local bp = helpers.get_db_utils()

local service = bp.services:insert {
Expand Down Expand Up @@ -483,7 +483,7 @@ describe("Plugin: prometheus (access) granular metrics switch", function()
status_client = helpers.http_client("127.0.0.1", tcp_status_port, 20000)
end)

teardown(function()
lazy_teardown(function()
if proxy_client then
proxy_client:close()
end
Expand Down
4 changes: 2 additions & 2 deletions spec/03-plugins/26-prometheus/05-metrics_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ for _, strategy in helpers.each_strategy() do
local admin_ssl_client -- admin_ssl_client (lua-resty-http) does not support h2
local proxy_ssl_client -- proxy_ssl_client (lua-resty-http) does not support h2

setup(function()
lazy_setup(function()
bp = helpers.get_db_utils(strategy, {"services", "routes", "plugins"})

local mock_ssl_service = bp.services:insert{
Expand Down Expand Up @@ -103,7 +103,7 @@ for _, strategy in helpers.each_strategy() do

end)

teardown(function()
lazy_teardown(function()
if admin_ssl_client then
admin_ssl_client:close()
end
Expand Down
4 changes: 2 additions & 2 deletions spec/03-plugins/26-prometheus/06-hybrid-mode_metrics_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local tcp_status_port = helpers.get_available_port()
describe("Plugin: prometheus (Hybrid Mode)", function()
local status_client

setup(function()
lazy_setup(function()
assert(helpers.start_kong {
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "bundled",
Expand All @@ -28,7 +28,7 @@ describe("Plugin: prometheus (Hybrid Mode)", function()
end
end)

teardown(function()
lazy_teardown(function()
helpers.stop_kong()
end)

Expand Down
4 changes: 2 additions & 2 deletions spec/03-plugins/26-prometheus/08-unit_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("Plugin: prometheus (unit)", function()
local orig_ngx_get_phase = ngx.get_phase
local orig_ngx_timer = ngx.timer

setup(function()
lazy_setup(function()
ngx.shared = require("spec.fixtures.shm-stub")
ngx.get_phase = function() -- luacheck: ignore
return "init_worker"
Expand All @@ -19,7 +19,7 @@ describe("Plugin: prometheus (unit)", function()
prometheus = require("kong.plugins.prometheus.prometheus")
end)

teardown(function()
lazy_teardown(function()
ngx.shared = orig_ngx_shared
ngx.get_phase = orig_ngx_get_phase -- luacheck: ignore
ngx.timer = orig_ngx_timer -- luacheck: ignore
Expand Down

0 comments on commit f97ab64

Please sign in to comment.