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

added json output support for stats handler #1865

Merged
merged 6 commits into from
Oct 19, 2017

Conversation

ramaraochavali
Copy link
Contributor

@ramaraochavali ramaraochavali commented Oct 16, 2017

PR for issue

response.add(fmt::format("{}: {}\n", stat.first, stat.second));
}
} else {
std::string format = params.begin()->first;
Copy link
Contributor Author

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.

Copy link
Member

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.

@mattklein123
Copy link
Member

@ramaraochavali before I dive into full review, a few things:

  1. Will need DCO (see contributing guide).
  2. Please get the PR compiling/passing tests.
  3. Will need docs here: https://envoyproxy.github.io/envoy/operations/admin.html
  4. Will need a test here https://github.com/envoyproxy/envoy/blob/master/test/integration/integration_admin_test.cc that the body returned is actually valid JSON.

@ramaraochavali
Copy link
Contributor Author

@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?
All tests are passing. Can you please review now?

@mattklein123
Copy link
Member

@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]>
@ramaraochavali
Copy link
Contributor Author

@mattklein123 done..did a force commit as single one

Copy link
Member

@mattklein123 mattklein123 left a 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.


.. http:get:: /stats?format=json

Same as /stats but outputs them in json format. This can be used for programmatic access of stats.
Copy link
Member

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"

@@ -31,8 +31,17 @@
#include "common/upstream/host_utility.h"

#include "fmt/format.h"
#include "rapidjson/document.h"
Copy link
Member

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"

response.add(fmt::format("{}: {}\n", stat.first, stat.second));
}
} else {
std::string format = params.begin()->first;
Copy link
Member

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.

for (auto stat : all_stats) {
response.add(fmt::format("{}: {}\n", stat.first, stat.second));
if (params.size() == 0) {
// No Arguments so use the standard
Copy link
Member

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)

}
} else {
std::string format = params.begin()->first;
std::string formatvalue = params.begin()->second;
Copy link
Member

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.

Copy link
Contributor Author

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 :-)

std::string format = params.begin()->first;
std::string formatvalue = params.begin()->second;
if (format == "format" && formatvalue == "json") {
rapidjson::Document document;
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

@ramaraochavali
Copy link
Contributor Author

@mattklein123 Thanks for your quick review. Addressed all the comments. Should be good to go. Let me know if further changes are required

Copy link
Member

@mattklein123 mattklein123 left a 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.


.. http:get:: /stats?format=json

Same as /stats the output is in JSON format. This can be used for programmatic access of stats.
Copy link
Member

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

// 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);
Copy link
Member

Choose a reason for hiding this comment

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

nit: const

response.add(fmt::format("{}: {}\n", stat.first, stat.second));
}
} else {
std::string format_key = params.begin()->first;
Copy link
Member

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

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);
Copy link
Member

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.

@@ -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);
Copy link
Member

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.

@ramaraochavali
Copy link
Contributor Author

@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. :-)

mattklein123
mattklein123 previously approved these changes Oct 19, 2017
Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good.

@@ -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);
Copy link
Member

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! :)

Copy link
Contributor Author

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

@mattklein123 mattklein123 dismissed their stale review October 19, 2017 04:09

one more thing

@mattklein123 mattklein123 merged commit d5695aa into envoyproxy:master Oct 19, 2017
@ramaraochavali ramaraochavali deleted the feature/stats-json branch October 20, 2017 04:12
jpsim pushed a commit that referenced this pull request Nov 28, 2022
jpsim pushed a commit that referenced this pull request Nov 29, 2022
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.

2 participants