|
| 1 | +local helpers = require "spec.helpers" -- hard dependency |
| 2 | + |
| 3 | + |
| 4 | +local ngx = ngx |
| 5 | + |
| 6 | +local fixtures = { |
| 7 | + dns_mock = helpers.dns_mock.new({ |
| 8 | + mocks_only = true |
| 9 | + }), |
| 10 | + http_mock = {}, |
| 11 | + stream_mock = {} |
| 12 | +} |
| 13 | + |
| 14 | +fixtures.dns_mock:A{ |
| 15 | + name = "mock.example.com", |
| 16 | + address = "127.0.0.1" |
| 17 | +} |
| 18 | + |
| 19 | +fixtures.dns_mock:A{ |
| 20 | + name = "status.example.com", |
| 21 | + address = "127.0.0.1" |
| 22 | +} |
| 23 | + |
| 24 | +local status_api_port = helpers.get_available_port() |
| 25 | + |
| 26 | + |
| 27 | +for _, strategy in helpers.each_strategy() do |
| 28 | + describe("Plugin: prometheus (metrics)", function() |
| 29 | + local bp |
| 30 | + local admin_ssl_client -- admin_ssl_client (lua-resty-http) does not support h2 |
| 31 | + local proxy_ssl_client -- proxy_ssl_client (lua-resty-http) does not support h2 |
| 32 | + |
| 33 | + setup(function() |
| 34 | + bp = helpers.get_db_utils(strategy, {"services", "routes", "plugins"}) |
| 35 | + |
| 36 | + local mock_ssl_service = bp.services:insert{ |
| 37 | + name = "mock-ssl-service", |
| 38 | + host = helpers.mock_upstream_ssl_host, |
| 39 | + port = helpers.mock_upstream_ssl_port, |
| 40 | + protocol = helpers.mock_upstream_ssl_protocol |
| 41 | + } |
| 42 | + bp.routes:insert{ |
| 43 | + name = "mock-ssl-route", |
| 44 | + protocols = {"https"}, |
| 45 | + hosts = {"mock.example.com"}, |
| 46 | + paths = {"/"}, |
| 47 | + service = { |
| 48 | + id = mock_ssl_service.id |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + local status_api_ssl_service = bp.services:insert{ |
| 53 | + name = "status-api-ssl-service", |
| 54 | + url = "https://127.0.0.1:" .. status_api_port .. "/metrics" |
| 55 | + } |
| 56 | + bp.routes:insert{ |
| 57 | + name = "status-api-ssl-route", |
| 58 | + protocols = {"https"}, |
| 59 | + hosts = {"status.example.com"}, |
| 60 | + paths = {"/metrics"}, |
| 61 | + service = { |
| 62 | + id = status_api_ssl_service.id |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + bp.plugins:insert{ |
| 67 | + name = "prometheus" -- globally enabled |
| 68 | + } |
| 69 | + |
| 70 | + assert(helpers.start_kong({ |
| 71 | + nginx_conf = "spec/fixtures/custom_nginx.template", |
| 72 | + plugins = "bundled,prometheus", |
| 73 | + status_listen = '127.0.0.1:' .. status_api_port .. ' ssl', -- status api does not support h2 |
| 74 | + status_access_log = "logs/status_access.log", |
| 75 | + status_error_log = "logs/status_error.log" |
| 76 | + }, nil, nil, fixtures)) |
| 77 | + |
| 78 | + end) |
| 79 | + |
| 80 | + teardown(function() |
| 81 | + if admin_ssl_client then |
| 82 | + admin_ssl_client:close() |
| 83 | + end |
| 84 | + if proxy_ssl_client then |
| 85 | + proxy_ssl_client:close() |
| 86 | + end |
| 87 | + |
| 88 | + helpers.stop_kong() |
| 89 | + end) |
| 90 | + |
| 91 | + before_each(function() |
| 92 | + admin_ssl_client = helpers.admin_client() |
| 93 | + proxy_ssl_client = helpers.proxy_ssl_client() |
| 94 | + end) |
| 95 | + |
| 96 | + after_each(function() |
| 97 | + if admin_ssl_client then |
| 98 | + admin_ssl_client:close() |
| 99 | + end |
| 100 | + if proxy_ssl_client then |
| 101 | + proxy_ssl_client:close() |
| 102 | + end |
| 103 | + end) |
| 104 | + |
| 105 | + it("expose Nginx connection metrics by admin API #a1.1", function() |
| 106 | + local res = assert(admin_ssl_client:send{ |
| 107 | + method = "GET", |
| 108 | + path = "/metrics" |
| 109 | + }) |
| 110 | + local body = assert.res_status(200, res) |
| 111 | + |
| 112 | + assert.matches('kong_nginx_metric_errors_total 0', body, nil, true) |
| 113 | + assert.matches('kong_nginx_' .. ngx.config.subsystem .. '_current_connections{state="%w+"} %d+', body) |
| 114 | + end) |
| 115 | + |
| 116 | + it("increments the count of proxied requests #p1.1", function() |
| 117 | + local res = assert(proxy_ssl_client:send{ |
| 118 | + method = "GET", |
| 119 | + path = "/status/400", |
| 120 | + headers = { |
| 121 | + ["Host"] = "mock.example.com" |
| 122 | + } |
| 123 | + }) |
| 124 | + assert.res_status(400, res) |
| 125 | + |
| 126 | + helpers.wait_until(function() |
| 127 | + local res = assert(admin_ssl_client:send{ |
| 128 | + method = "GET", |
| 129 | + path = "/metrics" |
| 130 | + }) |
| 131 | + local body = assert.res_status(200, res) |
| 132 | + |
| 133 | + assert.matches('kong_nginx_metric_errors_total 0', body, nil, true) |
| 134 | + |
| 135 | + return body:find('kong_http_status{service="mock-ssl-service",route="mock-ssl-route",code="400"} 1', |
| 136 | + nil, true) |
| 137 | + end) |
| 138 | + end) |
| 139 | + |
| 140 | + it("expose Nginx connection metrics by status API #s1.1", function() |
| 141 | + local res = assert(proxy_ssl_client:send{ |
| 142 | + method = "GET", |
| 143 | + path = "/metrics", |
| 144 | + headers = { |
| 145 | + ["Host"] = "status.example.com" |
| 146 | + } |
| 147 | + }) |
| 148 | + local body = assert.res_status(200, res) |
| 149 | + |
| 150 | + assert.matches('kong_nginx_metric_errors_total 0', body, nil, true) |
| 151 | + assert.matches('kong_nginx_' .. ngx.config.subsystem .. '_current_connections{state="%w+"} %d+', body) |
| 152 | + end) |
| 153 | + |
| 154 | + end) |
| 155 | +end |
0 commit comments