Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

shashiranjan84
Copy link
Contributor

@shashiranjan84 shashiranjan84 commented May 24, 2017

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 and reachable status added.
    image

  • 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.

@subnetmarco
Copy link
Member

Shouldn't this PR also update the plugins that are currently leveraging on the counters?

Copy link
Contributor

@p0pr0ck5 p0pr0ck5 left a 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())
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@@ -497,6 +497,7 @@ function _M:current_migrations()
end
end


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another unnecessary change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

status_response.database.reachable, err = dao.db:is_reachable()

if err ~= nil then
ngx.log(ngx.ERR, err)
Copy link
Contributor

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

Copy link
Contributor Author

@shashiranjan84 shashiranjan84 May 24, 2017

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.

Copy link
Member

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)

prepared = false
}, nil, true)

if not rows then return false, err end
Copy link
Contributor

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?

Copy link
Contributor Author

@shashiranjan84 shashiranjan84 May 24, 2017

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.

Copy link
Member

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.

Copy link
Member

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.

@shashiranjan84
Copy link
Contributor Author

@thefosk no, counter method is still there and being referenced at many places. So it would need separate card/focus.

@subnetmarco
Copy link
Member

Don't those agents rely on the counters returned by the /status endpoint?

@shashiranjan84
Copy link
Contributor Author

@thefosk sorry, what agent you talking about? Only agent I am aware of is DataDog, which I already put in note.

Copy link
Member

@thibaultcha thibaultcha left a 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!

status_response.database.reachable, err = dao.db:is_reachable()

if err ~= nil then
ngx.log(ngx.ERR, err)
Copy link
Member

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)

prepared = false
}, nil, true)

if not rows then return false, err end
Copy link
Member

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.

prepared = false
}, nil, true)

if not rows then return false, err end
Copy link
Member

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.

@@ -516,4 +516,21 @@ function _M:record_migration(id, name)
return true
end

function _M:is_reachable()

Copy link
Member

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

local query = fmt(
[[
SELECT * FROM pg_stat_database where datname='%s'
]], self.query_options.database)
Copy link
Member

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)

]], self.query_options.database)

local rows, err = self:query(query)
if not rows then return false, err end
Copy link
Member

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)

status_response.database[k] = count
-- ping DB
local err
status_response.database.reachable, err = dao.db:is_reachable()
Copy link
Member

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

@@ -660,4 +660,36 @@ function _M:record_migration(id, name)
return true
end

function _M:is_reachable()
Copy link
Member

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.

- 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.
@shashiranjan84
Copy link
Contributor Author

Updated as advised.

@thibaultcha
Copy link
Member

LGTM, I'll merge manually with some minor leftover edits

@shashiranjan84
Copy link
Contributor Author

Thanks @thibaultcha.

@thibaultcha
Copy link
Member

On second thought, I do think the logic here is not totally aligned with what has been discussed... See #2567.

@thibaultcha
Copy link
Member

Done with #2567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants