You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/01/20 01:20:08 UTC

[GitHub] [apisix] tokers commented on a change in pull request #3345: feat: allow getting health check status via control API

tokers commented on a change in pull request #3345:
URL: https://github.com/apache/apisix/pull/3345#discussion_r560609236



##########
File path: apisix/control/v1.lua
##########
@@ -49,11 +54,62 @@ function _M.schema()
 end
 
 
+local function iter_and_add_checker(infos, values, src)
+    if not values then
+        return
+    end
+
+    for _, value in core.config_util.iterate_values(values) do
+        if value.checker then
+            local checker = value.checker
+            local upstream = value.checker_upstream
+            local host = upstream.checks and upstream.checks.active and upstream.checks.active.host
+            local port = upstream.checks and upstream.checks.active and upstream.checks.active.port
+            local nodes = upstream.nodes
+            local health_nodes = core.table.new(#nodes, 0)
+            for _, node in ipairs(nodes) do
+                local ok = checker:get_target_status(node.host, port or node.port, host)
+                if ok then
+                    core.table.insert(health_nodes, node)
+                end
+            end
+
+            local conf = value.value
+            core.table.insert(infos, {
+                name = upstream_mod.get_healthchecker_name(value),
+                src_id = conf.id,
+                src_type = src,
+                nodes = nodes,
+                health_nodes = health_nodes,
+            })
+        end
+    end
+end
+
+
+function _M.healthcheck()
+    local infos = {}
+    local routes = get_routes()
+    iter_and_add_checker(infos, routes, "routes")
+    local services = get_services()
+    iter_and_add_checker(infos, services, "services")
+    local upstreams = get_upstreams()
+    iter_and_add_checker(infos, upstreams, "upstreams")
+    return 200, infos
+end
+
+
 return {
     -- /v1/schema
     {
         methods = {"GET"},
         uris = {"/schema"},
         handler = _M.schema,
+    },
+    -- /v1/healthcheck
+    {
+        methods = {"GET"},

Review comment:
       Just a proposal the output maybe too large if number of upstreams are large, while sometimes users will want to focus on a single upstream, so API `/v1/healthcheck/{id}` maybe useful.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org