diff --git a/t/prometheus-metrics.t b/t/prometheus-metrics.t index 53bf4d102..f4f7c3b39 100644 --- a/t/prometheus-metrics.t +++ b/t/prometheus-metrics.t @@ -1,6 +1,28 @@ use lib 't'; use Test::APIcast::Blackbox 'no_plan'; +add_response_body_check(sub { + my ($block, $body, $req_idx, $repeated_req_idx, $dry_run) = @_; + + my @asserts = @{$block->{expected_multiple_values}}; + if (@asserts == 0) { + return ""; + } + my $index = 0; + my $assertValues = $asserts[0][$req_idx]; + if (ref($assertValues) eq 'ARRAY') { + foreach my $regexp(@{$assertValues}){ + if (!($body =~ m/$regexp/)) { + fail(sprintf("Regular expression: '%s' does not match with the body: \n %s",$regexp, $body)); + } + } + }else{ + if (!($body =~ m/$assertValues/)) { + fail(sprintf("Regular expression: '%s' does not match with the body: \n %s",$assertValues, $body)); + } + } +}); + # The output varies between requests, so run only once repeat_each(1); @@ -231,7 +253,7 @@ In particular, it shows the status codes and the response times --- response_body_like eval [ "", -qr/upstream_response_time_seconds(.|\n)*upstream_response_time_seconds_bucket\{le=".*"\} 1(.|\n)*upstream_status\{status="200"\} 1/ +qr/upstream_response_time_seconds(.|\n)*upstream_response_time_seconds_bucket\{service="all",le=".*"\} 1(.|\n)*upstream_status\{status="200",service="all"\} 1/ ] --- no_error_log [error] @@ -275,7 +297,144 @@ qr/upstream_response_time_seconds(.|\n)*upstream_response_time_seconds_bucket\{l --- response_body_like eval [ "", -qr/total_response_time_seconds(.|\n)*total_response_time_seconds_bucket\{le=".*"\} 1/ +qr/total_response_time_seconds(.|\n)*total_response_time_seconds_bucket\{service="all",le=".*"\} 1/ ] --- no_error_log [error] + + + +=== TEST 6: extended metrics show services id +--- env eval +("APICAST_EXTENDED_METRICS", "true") +--- configuration +{ + "services": [ + { + "id": 42, + "proxy": { + "policy_chain": [ + { + "name": "apicast.policy.upstream", + "configuration": { + "rules": [ + { + "regex": "/", + "url": "http://test:$TEST_NGINX_SERVER_PORT" + } + ] + } + } + ] + } + } + ] +} +--- upstream + location / { + content_by_lua_block { + ngx.exit(200); + } + } + +--- request eval +["GET /", "GET /metrics"] +--- more_headers eval +["", "Host: metrics"] +--- error_code eval +[ 200, 200 ] +--- expected_multiple_values eval +[ +"", +[ + qr/total_response_time_seconds(.|\n)*total_response_time_seconds_bucket\{service="42",le=".*"\} 1/, + qr/upstream_response_time_seconds(.|\n)*upstream_response_time_seconds_bucket\{service="42",le=".*"\} 1(.|\n)*upstream_status\{status="200",service="42"\} 1/ +] +] +--- no_error_log +[error] + +=== TEST 7: extended metrics with multiple services +--- env eval +("APICAST_EXTENDED_METRICS", "true") +--- configuration +{ + "services": [ + { + "id": 42, + "proxy": { + "hosts": [ + "one" + ], + "policy_chain": [ + { + "name": "apicast.policy.upstream", + "configuration": { + "rules": [ + { + "regex": "/", + "url": "http://test:$TEST_NGINX_SERVER_PORT" + } + ] + } + } + ] + } + }, + { + "id": 21, + "proxy": { + "hosts": [ + "two" + ], + "policy_chain": [ + { + "name": "apicast.policy.upstream", + "configuration": { + "rules": [ + { + "regex": "/", + "url": "http://test:$TEST_NGINX_SERVER_PORT/two" + } + ] + } + } + ] + } + } + ] +} +--- upstream + location / { + content_by_lua_block { + ngx.exit(200); + } + } + + location /two { + content_by_lua_block { + ngx.exit(200); + } + } +--- request eval +["GET /", "GET /two", "GET /metrics"] +--- more_headers eval +["Host: one", "Host: two", "Host: metrics"] +--- error_code eval +[ 200, 200, 200 ] +--- expected_multiple_values eval +[ +"", +"", +[ +qr/total_response_time_seconds_bucket\{service="42",le=".*"\} 1/, +qr/upstream_response_time_seconds_bucket\{service="42",le=".*"\} 1/, +qr/upstream_status\{status="200",service="42"\}/, +qr/total_response_time_seconds_bucket\{service="21",le=".*"\} 1/, +qr/upstream_response_time_seconds_bucket\{service="21",le=".*"\} 1/, +qr/upstream_status\{status="200",service="21"\}/, + +]] +--- no_error_log +[error] +