kvrocks
kvrocks copied to clipboard
Add option to INFO to return a json payload
Search before asking
- [X] I had searched in the issues and found no similar issues.
Motivation
try to monitor some specs from my service and would be better to be able to consume them in a format that's easier to consume rather than parsing strings like the kvrocks_exporter does.
Solution
would be nice to expose somenthing like:
INFO `section` JSON/TXT
Are you willing to submit a PR?
- [ ] I'm willing to submit a PR!
do we want INFO command to return output in json fomat when INFO JSON (JSON specified in command) otherwise plain text?
I think for compatibility, it should be INFO [<section_name>] [FORMAT (TXT | JSON)].
im struggling with parsing the output to json, this line of code outputs string which looks like
"# Clients\r\nmaxclients:10000\r\nconnected_clients:1\r\nmonitor_clients:0\r\nblocked_clients:0\r\n"
using jsoncons::json::parse throws ser_error (unsupported format) and I should not directly append the string as is to the json.
should i create a custom parsing function to convert it something like?
{"clients":{"blocked_clients":"0\r","connected_clients":"1\r","maxclients":"10000\r","monitor_clients":"0\r"}}
TBH I cannot get your point. Why do you want to parse it as JSON? It's just not JSON.
TBH I cannot get your point. Why do you want to parse it as JSON? It's just not JSON.
Maybe i misunderstood, please confirm.
The issue wants the support of commands INFO [<section_name>] [FORMAT (TXT | JSON)]:
INFO: returns info in default plain text format.
INFO <section>: section info in plain text format.
INFO all FORMAT JSON: returns the entire info in JSON format.
INFO <section> FORMAT JSON: returns just section info in JSON.
INFO <section> FORMAT TXT: section info in plain text format.
Yeah, and you don't need to parse anything for implementing such behavior.
Yeah, and you don't need to parse anything for implementing such behavior.
so is this approach not desirable https://github.com/apache/kvrocks/compare/unstable...VasuDevrani:kvrocks:init_INFO
Yeah, and you don't need to parse anything for implementing such behavior.
so is this approach not desirable unstable...VasuDevrani:kvrocks:init_INFO
Yeah, it looks a bit ugly. We can have a more intuitive design for this feature.
Yeah, it looks a bit ugly. We can have a more intuitive design for this feature.
i feel confused about the more intuitive design is this close to that https://github.com/apache/kvrocks/commit/292bdbe7d36f54ada89623006c1597cb74eed3ef? otherwise its out of scope for me (at least for now, will have to wait for someone else's PR or if someone can guide me around the better approach).
Yeah, it looks a bit ugly. We can have a more intuitive design for this feature.
i feel confused about the more intuitive design is this close to that 292bdbe? otherwise its out of scope for me (at least for now, will have to wait for someone else's PR or if someone can guide me around the better approach).
https://github.com/apache/kvrocks/blob/292bdbe7d36f54ada89623006c1597cb74eed3ef/src/server/server.cc#L1211-L1229
I hope you can notice that there is a lot of repetition in your code between if and else, we should avoid this copy-and-paste programming. For me, preventing such code from entering kvrocks codebase is my top priority.
In fact, we can see that the result returned by INFO is a format similar to
std::map<SectionNameTy, std::map<KeyTy, ValueTy>>
(where ValueTy = something like std::variant<int, std::string>)
So we can return this format and transform it into JSON or TEXT at the end.
In details, we can have such small functions, like:
auto GetPersistenceInfo() -> std::map<KeyTy, ValueTy>;
auto GetServerInfo() -> std::map<KeyTy, ValueTy>;
auto GetPWhateverInfo() -> std::map<KeyTy, ValueTy>;
And then at the function that can generate all info (not real C++ code):
std::map<..> func_map = {{"persistence", GetPersistenceInfo}, ...}
auto all_info = func_map.filter(selected_info_sections).map(x -> x.call())
if (JSON) generateJsonOutput(all_info)
else (TXT) generateTxtOutput(all_info)
auto GetPersistenceInfo() -> std::map<KeyTy, ValueTy>; auto GetServerInfo() -> std::map<KeyTy, ValueTy>; auto GetPWhateverInfo() -> std::map<KeyTy, ValueTy>;
we already does have some function like GetServerInfo that uses string stream and returns string, should i overwrite them to return std::map<KeyTy, ValueTy>
auto GetPersistenceInfo() -> std::map<KeyTy, ValueTy>; auto GetServerInfo() -> std::map<KeyTy, ValueTy>; auto GetPWhateverInfo() -> std::map<KeyTy, ValueTy>;we already does have some function like GetServerInfo that uses string stream and returns string, should i overwrite them to return
std::map<KeyTy, ValueTy>
Yeah you can update these methods.