-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(admin-api/status) removed counters and added simple DB ping #2554
Conversation
Shouldn't this PR also update the plugins that are currently leveraging on the counters? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor style comments, but lgtm otherwise.
-- return time in UNIT millisecond, and PRECISION millisecond | ||
return math.floor(timestamp.get_utc_ms()) | ||
-- return time in UNIT millisecond, and PRECISION millisecond | ||
return math.floor(timestamp.get_utc_ms()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we avoid these whitespace cleanups. glad we're cleaning up, but it dirties the commit history. we can have a separate style clean commit later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unintentional fix done by IDE. I'll take care of this next time.
kong/dao/db/postgres.lua
Outdated
@@ -497,6 +497,7 @@ function _M:current_migrations() | |||
end | |||
end | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another unnecessary change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
kong/api/routes/kong.lua
Outdated
status_response.database.reachable, err = dao.db:is_reachable() | ||
|
||
if err ~= nil then | ||
ngx.log(ngx.ERR, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we report the error, or a summary of the error, as part of the response? my gut feeling is 'no', just bringing this up as a discussion point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had this question, but I guess its okay to just return status.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to at least give context to this error:
ngx.log(ngx.ERR, "failed to reach database as part of ",
"/status endpoint: ", err)
kong/dao/db/cassandra.lua
Outdated
prepared = false | ||
}, nil, true) | ||
|
||
if not rows then return false, err end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for style, i think we avoid one-line conditional blocks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, personally I would prefer indented conditional block, but I Kong is full of one-liner conditional statements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are legacy, please use indented conditional branches indeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, and by convention, we return nil, err
on error, and not false
. This is also valid later down this function.
@thefosk no, counter method is still there and being referenced at many places. So it would need separate card/focus. |
93cb2ef
to
d69f277
Compare
Don't those agents rely on the counters returned by the |
@thefosk sorry, what agent you talking about? Only agent I am aware of is DataDog, which I already put in note. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, the overall logic LGTM 👍 But this needs some more polishing.
Thank you!
kong/api/routes/kong.lua
Outdated
status_response.database.reachable, err = dao.db:is_reachable() | ||
|
||
if err ~= nil then | ||
ngx.log(ngx.ERR, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to at least give context to this error:
ngx.log(ngx.ERR, "failed to reach database as part of ",
"/status endpoint: ", err)
kong/dao/db/cassandra.lua
Outdated
prepared = false | ||
}, nil, true) | ||
|
||
if not rows then return false, err end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are legacy, please use indented conditional branches indeed.
kong/dao/db/cassandra.lua
Outdated
prepared = false | ||
}, nil, true) | ||
|
||
if not rows then return false, err end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, and by convention, we return nil, err
on error, and not false
. This is also valid later down this function.
kong/dao/db/postgres.lua
Outdated
@@ -516,4 +516,21 @@ function _M:record_migration(id, name) | |||
return true | |||
end | |||
|
|||
function _M:is_reachable() | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this blank line is undesired
kong/dao/db/postgres.lua
Outdated
local query = fmt( | ||
[[ | ||
SELECT * FROM pg_stat_database where datname='%s' | ||
]], self.query_options.database) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we need the multi-line string here:
local query = fmt("SELECT * FROM pg_stat_database where datname='%s'",
self.query_options.database)
kong/dao/db/postgres.lua
Outdated
]], self.query_options.database) | ||
|
||
local rows, err = self:query(query) | ||
if not rows then return false, err end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto: indented block vs single line and return failure value (same below)
kong/api/routes/kong.lua
Outdated
status_response.database[k] = count | ||
-- ping DB | ||
local err | ||
status_response.database.reachable, err = dao.db:is_reachable() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to the below changes with the return value (and because this reachable()
function might be used elsewhere and should not be tied to this endpoint for the sake of modularisation), this could become:
local ok, err = dao.db:reachable()
if not ok then
ngx.log(ngx.ERR, "msg...", err)
else
status_response.database.reachable = true
end
kong/dao/db/cassandra.lua
Outdated
@@ -660,4 +660,36 @@ function _M:record_migration(id, name) | |||
return true | |||
end | |||
|
|||
function _M:is_reachable() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefer less cumber-stone names, here, reachable()
should better suited.
d69f277
to
7ec50c3
Compare
- removed counter for each Kong tables being returned as part of `/status` api response. - reachablity check to DB is done by firing a light query to check if keyspace/database exists. Note: Existing version of DataDog agent will not work with above fix. A separate PR would be sent to DD repo to fix that too.
7ec50c3
to
356345d
Compare
Updated as advised. |
LGTM, I'll merge manually with some minor leftover edits |
Thanks @thibaultcha. |
On second thought, I do think the logic here is not totally aligned with what has been discussed... See #2567. |
Done with #2567 |
Summary
In favor to make
/status
admin API fast and constant time execution(short of), counters are removed and DB reachability status added.Full changelog
Added reachability check to DB is done by firing a light query
to check if keyspace/database exists in DB.
Removed counters from
/status
api response andreachable
status added.Updated related test.
Note: Existing version of DataDog agent will not work with above fix.
A separate PR would be sent to DD repo to fix that too.