@@ -2,6 +2,7 @@ use lib 't';
22use Test::APIcast::Blackbox ' no_plan' ;
33
44require (" policies.pl" );
5+ require (" http_proxy.pl" );
56# The output varies between requests, so run only once
67repeat_each(1);
78
@@ -492,3 +493,226 @@ qr/apicast_status\{status="404"\} 1/,
492493]]
493494--- no_error_log
494495[error]
496+
497+
498+ === TEST 9: HTTPS proxy reports upstream metrics via ngx.ctx fallback
499+ When the request goes through the HTTPS proxy path, ngx.var.upstream_status is
500+ not set by nginx. The http_proxy module populates ngx.ctx.proxy_upstream_status
501+ and ngx.ctx.proxy_upstream_response_time, and the nginx_metrics policy falls
502+ back to those values. Verify the upstream_status metric is reported.
503+ --- env eval
504+ (
505+ "https_proxy" => $ENV{TEST_NGINX_HTTPS_PROXY},
506+ 'BACKEND_ENDPOINT_OVERRIDE' => "http://test_backend.lvh.me:$ENV{TEST_NGINX_SERVER_PORT}"
507+ )
508+ --- configuration random_port env
509+ {
510+ "services": [
511+ {
512+ "id": 42,
513+ "backend_version": 1,
514+ "backend_authentication_type": "service_token",
515+ "backend_authentication_value": "token-value",
516+ "proxy": {
517+ "hosts": ["one"],
518+ "api_backend": "https://test-upstream.lvh.me:$TEST_NGINX_RANDOM_PORT",
519+ "proxy_rules": [
520+ { "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 1 }
521+ ]
522+ }
523+ }
524+ ]
525+ }
526+ --- backend
527+ server_name test_backend.lvh.me;
528+ location /transactions/authrep.xml {
529+ content_by_lua_block {
530+ ngx.exit(ngx.OK)
531+ }
532+ }
533+ --- upstream env
534+ server_name test-upstream.lvh.me;
535+ listen $TEST_NGINX_RANDOM_PORT ssl;
536+ ssl_certificate $TEST_NGINX_SERVER_ROOT/html/server.crt;
537+ ssl_certificate_key $TEST_NGINX_SERVER_ROOT/html/server.key;
538+ location / {
539+ content_by_lua_block {
540+ ngx.exit(200);
541+ }
542+ }
543+ --- request eval
544+ ["GET /?user_key=value", "GET /metrics/"]
545+ --- more_headers eval
546+ ["Host: one", "Host: metrics"]
547+ --- error_code eval
548+ [ 200, 200 ]
549+ --- expected_response_body_like_multiple eval
550+ [
551+ "",
552+ [
553+ qr/upstream_response_time_seconds(.|\n)/,
554+ qr/upstream_response_time_seconds_bucket\{service_id="",service_system_name="",le=".*"\} 1/,
555+ qr/upstream_status\{status="200",service_id="",service_system_name=""\} 1/
556+ ]]
557+ --- no_error_log
558+ [error]
559+ --- user_files fixture=tls.pl eval
560+
561+
562+ === TEST 10: Report upstream metrics when using http_proxy policy
563+ --- env eval
564+ ('BACKEND_ENDPOINT_OVERRIDE' => "http://test_backend.lvh.me:$ENV{TEST_NGINX_SERVER_PORT}")
565+ --- configuration random_port env
566+ {
567+ "services": [
568+ {
569+ "id": 42,
570+ "backend_version": 1,
571+ "backend_authentication_type": "service_token",
572+ "backend_authentication_value": "token-value",
573+ "proxy": {
574+ "hosts": ["one"],
575+ "api_backend": "https://test-upstream.lvh.me:$TEST_NGINX_RANDOM_PORT",
576+ "proxy_rules": [
577+ { "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 1 }
578+ ],
579+ "policy_chain": [
580+ {
581+ "name": "apicast.policy.apicast"
582+ },
583+ {
584+ "name": "apicast.policy.http_proxy",
585+ "configuration": {
586+ "https_proxy": "$TEST_NGINX_HTTPS_PROXY"
587+ }
588+ }
589+ ]
590+ }
591+ }
592+ ]
593+ }
594+ --- backend
595+ server_name test_backend.lvh.me;
596+ location /transactions/authrep.xml {
597+ content_by_lua_block {
598+ ngx.exit(ngx.OK)
599+ }
600+ }
601+ --- upstream env
602+ server_name test-upstream.lvh.me;
603+ listen $TEST_NGINX_RANDOM_PORT ssl;
604+ ssl_certificate $TEST_NGINX_SERVER_ROOT/html/server.crt;
605+ ssl_certificate_key $TEST_NGINX_SERVER_ROOT/html/server.key;
606+ location / {
607+ content_by_lua_block {
608+ ngx.exit(200);
609+ }
610+ }
611+ --- request eval
612+ ["GET /?user_key=value", "GET /metrics/"]
613+ --- more_headers eval
614+ ["Host: one", "Host: metrics"]
615+ --- error_code eval
616+ [ 200, 200 ]
617+ --- expected_response_body_like_multiple eval
618+ [
619+ "",
620+ [
621+ qr/upstream_response_time_seconds(.|\n)/,
622+ qr/upstream_response_time_seconds_bucket\{service_id="",service_system_name="",le=".*"\} 1/,
623+ qr/upstream_status\{status="200",service_id="",service_system_name=""\} 1/
624+ ]]
625+ --- no_error_log
626+ [error]
627+ --- user_files fixture=tls.pl eval
628+
629+
630+ === TEST 11: Report upstream metrics when using camel policy
631+ --- init eval
632+ $Test::Nginx::Util::PROXY_SSL_PORT = Test::APIcast::get_random_port();
633+ $Test::Nginx::Util::ENDPOINT_SSL_PORT = Test::APIcast::get_random_port();
634+ --- configuration random_port env eval
635+ <<EOF
636+ {
637+ "services": [
638+ {
639+ "id": 42,
640+ "backend_version": 1,
641+ "backend_authentication_type": "service_token",
642+ "backend_authentication_value": "token-value",
643+ "proxy": {
644+ "hosts": ["one"],
645+ "secret_token": "token",
646+ "api_backend": "https://localhost:$Test::Nginx::Util::ENDPOINT_SSL_PORT",
647+ "proxy_rules": [
648+ { "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 1 }
649+ ],
650+ "policy_chain": [
651+ {
652+ "name": "apicast.policy.apicast"
653+ },
654+ {
655+ "name": "apicast.policy.camel",
656+ "configuration": {
657+ "https_proxy": "http://127.0.0.1:$Test::Nginx::Util::PROXY_SSL_PORT"
658+ }
659+ }
660+ ]
661+ }
662+ }
663+ ]
664+ }
665+ EOF
666+ --- backend
667+ location /transactions/authrep.xml {
668+ content_by_lua_block {
669+ ngx.exit(ngx.OK)
670+ }
671+ }
672+ --- upstream eval
673+ <<EOF
674+ # Endpoint config
675+ listen $Test::Nginx::Util::ENDPOINT_SSL_PORT ssl;
676+
677+ ssl_certificate $Test::Nginx::Util::ServRoot/html/server.crt;
678+ ssl_certificate_key $Test::Nginx::Util::ServRoot/html/server.key;
679+
680+ server_name _ default_server;
681+
682+ location / {
683+ content_by_lua_block {
684+ ngx.exit(200)
685+ }
686+ }
687+ }
688+ server {
689+ # Proxy config
690+ listen $Test::Nginx::Util::PROXY_SSL_PORT ssl;
691+
692+ ssl_certificate $Test::Nginx::Util::ServRoot/html/server.crt;
693+ ssl_certificate_key $Test::Nginx::Util::ServRoot/html/server.key;
694+
695+ server_name _ default_server;
696+
697+ location ~ /.* {
698+ proxy_http_version 1.1;
699+ proxy_pass https://\$http_host;
700+ }
701+ EOF
702+ --- request eval
703+ ["GET /?user_key=value", "GET /metrics/"]
704+ --- more_headers eval
705+ ["Host: one", "Host: metrics"]
706+ --- error_code eval
707+ [ 200, 200 ]
708+ --- expected_response_body_like_multiple eval
709+ [
710+ "",
711+ [
712+ qr/upstream_response_time_seconds(.|\n)/,
713+ qr/upstream_response_time_seconds_bucket\{service_id="",service_system_name="",le=".*"\} 1/,
714+ qr/upstream_status\{status="200",service_id="",service_system_name=""\} 1/
715+ ]]
716+ --- no_error_log
717+ [error]
718+ --- user_files fixture=tls.pl eval
0 commit comments