Skip to content

Commit 9661f32

Browse files
committed
t/prometheus-metrics: test backend response metrics
1 parent 9b8454e commit 9661f32

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local PolicyChain = require('apicast.policy_chain')
2+
3+
local policies = {
4+
'apicast.policy.load_configuration',
5+
'apicast.policy.find_service',
6+
'apicast.policy.local_chain'
7+
}
8+
9+
local policy_chain = PolicyChain.build(policies)
10+
11+
return {
12+
policy_chain = policy_chain,
13+
port = { metrics = 9421 },
14+
}

t/prometheus-metrics.t

+65
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,68 @@ openresty_shdict_free_space{dict="prometheus_metrics"} 16662528
5454
--- error_code: 200
5555
--- no_error_log
5656
[error]
57+
58+
=== TEST 2: metric endpoints shows backend responses when the APIcast policy is in the chain
59+
We do a couple of authorized requests to backend (2xx) and a couple of
60+
unauthorized ones (4xx) and check that those metrics are shown correctly when
61+
calling the prometheus metrics endpoint.
62+
To simplify the output of the metrics endpoint, we use an environment config
63+
that does not include the nginx metrics (tested in the previous test).
64+
--- environment_file: t/fixtures/configs/without_nginx_metrics.lua
65+
--- configuration
66+
{
67+
"services": [
68+
{
69+
"id": 42,
70+
"backend_version": 1,
71+
"backend_authentication_type": "service_token",
72+
"backend_authentication_value": "token-value",
73+
"proxy": {
74+
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
75+
"proxy_rules": [
76+
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 1 }
77+
],
78+
"policy_chain": [
79+
{ "name": "apicast.policy.apicast" }
80+
]
81+
}
82+
}
83+
]
84+
}
85+
--- upstream
86+
location / {
87+
content_by_lua_block {
88+
ngx.say('yay, api backend');
89+
}
90+
}
91+
--- backend
92+
location /transactions/authrep.xml {
93+
content_by_lua_block {
94+
-- Check only the user key and assume the rest of params are OK
95+
if ngx.req.get_uri_args(0)['user_key'] == 'invalid' then
96+
ngx.exit(403)
97+
else
98+
ngx.exit(200)
99+
end
100+
}
101+
}
102+
--- request eval
103+
["GET /?user_key=valid", "GET /?user_key=valid", "GET /?user_key=invalid", "GET /?user_key=invalid", "GET /metrics"]
104+
--- more_headers eval
105+
["", "", "", "", "Host: metrics"]
106+
--- error_code eval
107+
[ 200, 200, 403, 403, 200 ]
108+
--- response_body eval
109+
[ "yay, api backend\x{0a}", "yay, api backend\x{0a}", "Authentication failed", "Authentication failed",
110+
<<'METRICS_OUTPUT'
111+
# HELP backend_response Response status codes from 3scale's backend
112+
# TYPE backend_response counter
113+
backend_response{status="2xx"} 2
114+
backend_response{status="4xx"} 2
115+
# HELP nginx_metric_errors_total Number of nginx-lua-prometheus errors
116+
# TYPE nginx_metric_errors_total counter
117+
nginx_metric_errors_total 0
118+
METRICS_OUTPUT
119+
]
120+
--- no_error_log
121+
[error]

0 commit comments

Comments
 (0)