Added support for JSONP mode and CORS headers to JSON Prometheus output

This commit is contained in:
Thulinma 2018-07-10 09:44:50 +02:00
parent 93fd6d290d
commit 52b24757c9
2 changed files with 11 additions and 1 deletions

View file

@ -317,7 +317,7 @@ int Controller::handleAPIConnection(Socket::Connection & conn){
H.Clean(); H.Clean();
continue; continue;
} }
if (H.url == "/"+Controller::prometheus+".json"){ if (H.url.substr(0, Controller::prometheus.size()+6) == "/"+Controller::prometheus+".json"){
handlePrometheus(H, conn, PROMETHEUS_JSON); handlePrometheus(H, conn, PROMETHEUS_JSON);
H.Clean(); H.Clean();
continue; continue;

View file

@ -1479,12 +1479,16 @@ void Controller::fillTotals(JSON::Value & req, JSON::Value & rep){
} }
void Controller::handlePrometheus(HTTP::Parser & H, Socket::Connection & conn, int mode){ void Controller::handlePrometheus(HTTP::Parser & H, Socket::Connection & conn, int mode){
std::string jsonp;
switch (mode){ switch (mode){
case PROMETHEUS_TEXT: case PROMETHEUS_TEXT:
H.SetHeader("Content-Type", "text/plain; version=0.0.4"); H.SetHeader("Content-Type", "text/plain; version=0.0.4");
break; break;
case PROMETHEUS_JSON: case PROMETHEUS_JSON:
H.SetHeader("Content-Type", "text/json"); H.SetHeader("Content-Type", "text/json");
H.setCORSHeaders();
if (H.GetVar("callback") != ""){jsonp = H.GetVar("callback");}
if (H.GetVar("jsonp") != ""){jsonp = H.GetVar("jsonp");}
break; break;
} }
H.SetHeader("Server", "MistServer/" PACKAGE_VERSION); H.SetHeader("Server", "MistServer/" PACKAGE_VERSION);
@ -1805,7 +1809,13 @@ void Controller::handlePrometheus(HTTP::Parser & H, Socket::Connection & conn, i
} }
} }
if (jsonp.size()){
H.Chunkify(jsonp + "(", conn);
}
H.Chunkify(resp.toString(), conn); H.Chunkify(resp.toString(), conn);
if (jsonp.size()){
H.Chunkify(");\n", conn);
}
} }
H.Chunkify("", conn); H.Chunkify("", conn);