-
Notifications
You must be signed in to change notification settings - Fork 13.8k
fix: /metrics endpoint returning JSON-escaped Prometheus format #17386
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
base: master
Are you sure you want to change the base?
Conversation
tools/server/server.cpp
Outdated
| void ok(const std::string & response_data) { | ||
| status = 200; | ||
| data = response_data; | ||
| } |
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 don't think it's a safe solution as std::string can also be a valid json.
Instead, you can simply set the status and data directly in the handle_metrics without using ok() macro
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.
Hi, @ngxson Thank you for the guidance!
After thinking about it carefully, the situation you described with I don't think it's a safe solution as std::string can also be a valid json. does indeed exist.
I noticed that all handlers return by calling either error or ok to set the HTTP response. To make the code look more consistent, I avoided using this approach in the handler functions:
res->status = 200;
res->data = prometheus.str();My original thinking was:
For JSON type return values, call this signature: ok(const json & response_data)
For string type return values, call this signature: ok(const std::string & response_data)
So I'm wondering which approach would be better - should I simply use:
res->status = 200;
res->data = prometheus.str();Or should I define specific methods like:
void ok_json(const json & response_data);
void ok_text(const std::string & response_data);
void ok_html(const std::string & response_data);Or is there another approach you'd recommend?
I want to maintain consistency with the existing codebase while properly handling different response content types.
|
Hi, I chose the simplest approach: res->status = 200;
res->data = prometheus.str();When requesting via curl command, the output is as follows: curl http://127.0.0.1:8080/metrics |
The
/metricsendpoint returns Prometheus-format text that is incorrectly JSON-escaped, causing the output to be wrapped in double quotes, which prevents Prometheus from parsing it correctly.Related code snippets:
llama.cpp/tools/server/server.cpp
Line 4567 in 2eba631
llama.cpp/tools/server/server.cpp
Lines 4441 to 4444 in 2eba631
I added an
ok()overload with astd::stringparameter to fix this issue.Related issue:
Note: I've checked other calls to
ok()and they all pass JSON types, so this issue won't occur elsewhere.