-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
added json output support for stats handler #1865
added json output support for stats handler #1865
Conversation
source/server/http/admin.cc
Outdated
response.add(fmt::format("{}: {}\n", stat.first, stat.second)); | ||
} | ||
} else { | ||
std::string format = params.begin()->first; |
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.
Tried to use Field, FieldPtr and create json but not able to include them here.
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.
Yeah, thanks for trying. See TODO request above.
@ramaraochavali before I dive into full review, a few things:
|
@mattklein123 I have updated the doc and added an integration test that validates the json. I have signed the DCO in my last commit. But still it shows as failed? Do I have to commit with a new branch? |
@ramaraochavali every commit has to have DCO. Please just squash/rebase and force push the entire PR on current master (to fix OSX tests) and then we can review. Thank you. |
Signed-off-by: Rama <[email protected]>
48d7ee0
to
698ead5
Compare
@mattklein123 done..did a force commit as single one |
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.
Looks good, thanks for the contribution. Few small comments.
docs/operations/admin.rst
Outdated
|
||
.. http:get:: /stats?format=json | ||
|
||
Same as /stats but outputs them in json format. This can be used for programmatic access of stats. |
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.
nit: "Same as /stats the output is in JSON format"
source/server/http/admin.cc
Outdated
@@ -31,8 +31,17 @@ | |||
#include "common/upstream/host_utility.h" | |||
|
|||
#include "fmt/format.h" | |||
#include "rapidjson/document.h" |
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 would prefer that we don't like rapidjson out into the rest of the code, but it's OK for now. Can you please add above the includes:
"TODO(mattklein123): Switch to JSON interface methods and remove rapidjson dependency"
source/server/http/admin.cc
Outdated
response.add(fmt::format("{}: {}\n", stat.first, stat.second)); | ||
} | ||
} else { | ||
std::string format = params.begin()->first; |
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.
Yeah, thanks for trying. See TODO request above.
source/server/http/admin.cc
Outdated
for (auto stat : all_stats) { | ||
response.add(fmt::format("{}: {}\n", stat.first, stat.second)); | ||
if (params.size() == 0) { | ||
// No Arguments so use the standard |
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.
nit: "No arguments so use the standard output." (period end of sentence)
source/server/http/admin.cc
Outdated
} | ||
} else { | ||
std::string format = params.begin()->first; | ||
std::string formatvalue = params.begin()->second; |
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.
Please use snake case for all vars (e.g., "format_value", "stats_array" below, etc.). Won't comment on each one.
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.
will change.. this habit from my lot of java coding :-)
source/server/http/admin.cc
Outdated
std::string format = params.begin()->first; | ||
std::string formatvalue = params.begin()->second; | ||
if (format == "format" && formatvalue == "json") { | ||
rapidjson::Document document; |
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.
nit: Can we move the actual JSON creation into a helper function. We will likely have other output types in the future.
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.
will do
Signed-off-by: Rama <[email protected]>
Signed-off-by: Rama <[email protected]>
@mattklein123 Thanks for your quick review. Addressed all the comments. Should be good to go. Let me know if further changes are required |
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.
Thanks, looks good. Few tiny nits then ready to go.
docs/operations/admin.rst
Outdated
|
||
.. http:get:: /stats?format=json | ||
|
||
Same as /stats the output is in JSON format. This can be used for programmatic access of stats. |
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.
Please switch first sentence to "Outputs /stats in JSON format."
source/server/http/admin.cc
Outdated
// We currently don't support timers locally (only via statsd) so just group all the counters | ||
// and gauges together, alpha sort them, and spit them out. | ||
Http::Code rc = Http::Code::OK; | ||
Http::Utility::QueryParams params = Http::Utility::parseQueryString(url); |
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.
nit: const
source/server/http/admin.cc
Outdated
response.add(fmt::format("{}: {}\n", stat.first, stat.second)); | ||
} | ||
} else { | ||
std::string format_key = params.begin()->first; |
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.
nit: const for this var and the the following
source/server/http/admin.cc
Outdated
std::string format_key = params.begin()->first; | ||
std::string format_value = params.begin()->second; | ||
if (format_key == "format" && format_value == "json") { | ||
std::string json_stats = statsAsJson(all_stats); |
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.
nit: don't need local var, just fold function call into the following line.
source/server/http/admin.h
Outdated
@@ -115,7 +115,7 @@ class AdminImpl : public Admin, | |||
void addOutlierInfo(const std::string& cluster_name, | |||
const Upstream::Outlier::Detector* outlier_detector, | |||
Buffer::Instance& response); | |||
|
|||
std::string statsAsJson(std::map<std::string, uint64_t> all_stats); |
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.
nit: newline after this line.
Signed-off-by: Rama <[email protected]>
Signed-off-by: Rama <[email protected]>
@mattklein123 I think have addressed all comments. Have a final look once. Sorry because of lack of my recent c++ coding exp, took more time for you to review I guess. :-) |
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.
Thanks! Looks good.
source/server/http/admin.h
Outdated
@@ -115,6 +115,7 @@ class AdminImpl : public Admin, | |||
void addOutlierInfo(const std::string& cluster_name, | |||
const Upstream::Outlier::Detector* outlier_detector, | |||
Buffer::Instance& response); | |||
std::string statsAsJson(std::map<std::string, uint64_t> all_stats); |
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.
oops sorry one more thing, can you make this const std::map<std::string, uint64_t>&
. Sorry! :)
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.
No issues. committed..please check
Signed-off-by: Rama <[email protected]>
Signed-off-by: Mike Schore <[email protected]> Signed-off-by: JP Simard <[email protected]>
Signed-off-by: Mike Schore <[email protected]> Signed-off-by: JP Simard <[email protected]>
PR for issue