Skip to content

Commit

Permalink
Add health check to ensure trafficserver is responding.
Browse files Browse the repository at this point in the history
  • Loading branch information
GUI committed Jul 19, 2024
1 parent 2543b6d commit 6b6f4e3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/api-umbrella/http-api/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local function status_response(quick)
status = "red",
details = {
apis_config = "red",
cache_server = "red"
},
}

Expand All @@ -21,8 +22,22 @@ local function status_response(quick)
response["details"]["apis_config"] = "green"
end

local httpc = http.new()
httpc:set_timeout(3000)

local res, err = httpc:request_uri("http://127.0.0.1:" .. config["trafficserver"]["port"] .. "/_trafficserver-health/nocache/1", {
headers = {
["Host"] = "api-umbrella-trafficserver-health.internal",
},
})
if err then
ngx.log(ngx.ERR, "failed to fetch web app: ", err)
elseif res.status == 200 then
response["details"]["cache_server"] = "green"
end

if quick then
if response["details"]["apis_config"] == "green" then
if response["details"]["apis_config"] == "green" and response["details"]["cache_server"] == "green" then
response["status"] = "green"
end

Expand All @@ -33,11 +48,8 @@ local function status_response(quick)
response["details"]["analytics_db_setup"] = "red"
response["details"]["web_app"] = "red"

local httpc = http.new()
httpc:set_timeout(3000)

-- Check the health of the OpenSearch cluster
local res, err = opensearch_query("/_cluster/health")
res, err = opensearch_query("/_cluster/health")
if err then
ngx.log(ngx.ERR, "failed to fetch cluster health from opensearch: ", err)
elseif res.body_json then
Expand Down Expand Up @@ -65,9 +77,9 @@ local function status_response(quick)
-- content), OpenSearch seems to get stuck in the yellow status, even though
-- everything appears operational (but then it becomes green once content
-- starts indexing).
if response["details"]["apis_config"] == "green" and (response["details"]["analytics_db"] == "yellow" or response["details"]["analytics_db"] == "green") and response["details"]["analytics_db_setup"] == "green" and response["details"]["web_app"] == "green" then
if response["details"]["apis_config"] == "green" and response["details"]["cache_server"] == "green" and (response["details"]["analytics_db"] == "yellow" or response["details"]["analytics_db"] == "green") and response["details"]["analytics_db_setup"] == "green" and response["details"]["web_app"] == "green" then
response["status"] = "green"
elseif response["details"]["apis_config"] == "green" then
elseif response["details"]["apis_config"] == "green" and response["details"]["cache_server"] == "green" then
response["status"] = "yellow"
end

Expand Down
6 changes: 6 additions & 0 deletions templates/etc/trafficserver/remap.config.etlua
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# A fake endpoint that just returns a 200 OK response. We'll use this to check
# if Trafficserver is up and responding (since Envoy may not be up if that's
# hosted separately).
map http://api-umbrella-trafficserver-health.internal/_trafficserver-health/ http://127.0.0.1 @plugin=generator.so

# Route all requests to Envoy for proxying to underlying API backends.
map / <%- config["envoy"]["scheme"] %>://<%- config["envoy"]["host"] %>:<%- config["envoy"]["port"] %>

0 comments on commit 6b6f4e3

Please sign in to comment.