Skip to content

Commit

Permalink
Merge 50f9aba into 8fbd0ac
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-chen committed Jul 23, 2020
2 parents 8fbd0ac + 50f9aba commit 47a18ea
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
9 changes: 9 additions & 0 deletions apisix/consumer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ function _M.plugin(plugin_name)
end


function _M.consumers()
if not consumers then
return nil, nil
end

return consumers.values, consumers.conf_version
end


function _M.init_worker()
local err
consumers, err = core.config.new("/consumers", {
Expand Down
9 changes: 9 additions & 0 deletions apisix/http/router/radixtree_sni.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ function _M.match_and_set(api_ctx)
end


function _M.ssls()
if not ssl_certificates then
return nil, nil
end

return ssl_certificates.values, ssl_certificates.conf_version
end


function _M.init_worker()
local err
ssl_certificates, err = core.config.new("/ssl", {
Expand Down
77 changes: 77 additions & 0 deletions apisix/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ local ngx_capture = ngx.location.capture
local re_gmatch = ngx.re.gmatch
local tonumber = tonumber
local select = select
local type = type
local prometheus
local router = require("apisix.router")
local get_routes = router.http_routes
local get_ssls = router.ssls
local get_services = require("apisix.http.service").services
local get_consumers = require("apisix.consumer").consumers
local get_upstreams = require("apisix.upstream").upstreams



-- Default set of latency buckets, 1ms to 60s:
local DEFAULT_BUCKETS = { 1, 2, 5, 7, 10, 15, 20, 25, 30, 40, 50, 60, 70,
Expand Down Expand Up @@ -67,6 +76,10 @@ function _M.init()
metrics.etcd_reachable = prometheus:gauge("etcd_reachable",
"Config server etcd reachable from APISIX, 0 is unreachable")

metrics.etcd_modify_indexes = prometheus:gauge("etcd_modify_indexes",
"Etcd modify index for APISIX keys",
{"key"})

-- per service
metrics.status = prometheus:counter("http_status",
"HTTP status codes per service in APISIX",
Expand Down Expand Up @@ -152,9 +165,70 @@ local function nginx_status()

label_values[1] = name
metrics.connections:set(val[0], label_values)

end
end

local key_values = {}
local function set_modify_index(key, items, items_ver, global_max_index)
local max_idx = 0

if items_ver and items then
for _, item in ipairs(items) do
if type(item) == "table" and item.modifiedIndex > max_idx then
max_idx = item.modifiedIndex
end
end
end

key_values[1] = key
metrics.etcd_modify_indexes:set(max_idx, key_values)


global_max_index = max_idx > global_max_index and max_idx or global_max_index

return global_max_index
end
local function etcd_modify_index()
local global_max_idx = 0

-- routes
local routes, routes_ver = get_routes()
global_max_idx = set_modify_index("routes", routes, routes_ver, global_max_idx)

-- services
local services, services_ver = get_services()
global_max_idx = set_modify_index("services", services, services_ver, global_max_idx)

-- ssls
local ssls, ssls_ver = get_ssls()
global_max_idx = set_modify_index("ssls", ssls, ssls_ver, global_max_idx)

-- consumers
local consumers, consumers_ver = get_consumers()
global_max_idx = set_modify_index("consumers", consumers, consumers_ver, global_max_idx)

-- consumers
local consumers, consumers_ver = get_consumers()
global_max_idx = set_modify_index("consumers", consumers, consumers_ver, global_max_idx)

-- global_rules
local global_rules = router.global_rules
if global_rules then
global_max_idx = set_modify_index("global_rules", global_rules.values,
global_rules.conf_version, global_max_idx)
end

-- upstreams
local upstreams, upstreams_ver = get_upstreams()
global_max_idx = set_modify_index("upstreams", upstreams, upstreams_ver, global_max_idx)

-- global max
key_values[1] = "max_modify_index"
metrics.etcd_modify_indexes:set(global_max_idx, key_values)

end


function _M.collect()
if not prometheus or not metrics then
Expand All @@ -166,6 +240,9 @@ function _M.collect()
-- across all services
nginx_status()

-- etcd modify index
etcd_modify_index()

-- config server status
local config = core.config.new()
local version, err = config:server_version()
Expand Down
4 changes: 4 additions & 0 deletions apisix/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ function _M.stream_init_worker()
end


function _M.ssls()
return _M.router_ssl.ssls()
end

function _M.http_routes()
return _M.router_http.routes()
end
Expand Down
1 change: 1 addition & 0 deletions apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ _M.service = {
_M.consumer = {
type = "object",
properties = {
id = id_schema,
username = {
type = "string", minLength = 1, maxLength = 32,
pattern = [[^[a-zA-Z0-9_]+$]]
Expand Down
14 changes: 12 additions & 2 deletions t/plugin/prometheus.t
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,17 @@ qr/apisix_http_overhead_bucket.*service=\"3\".*le=\"00500.0.*/
=== TEST 30: delete route 4
=== TEST 30: fetch the prometheus metric data with `modify_indexes`
--- request
GET /apisix/prometheus/metrics
--- response_body_like
TYPE apisix_etcd_modify_indexes gauge
--- no_error_log
[error]
=== TEST 31: delete route 4
--- config
location /t {
content_by_lua_block {
Expand All @@ -643,7 +653,7 @@ passed
=== TEST 31: delete service 3
=== TEST 32: delete service 3
--- config
location /t {
content_by_lua_block {
Expand Down

0 comments on commit 47a18ea

Please sign in to comment.