Added JSON instrumentation output.

This commit is contained in:
Thulinma 2016-04-21 17:59:29 +02:00
parent dd46788d37
commit b6d879369f
2 changed files with 88 additions and 52 deletions

View file

@ -173,7 +173,7 @@ int Controller::handleAPIConnection(Socket::Connection & conn){
handlePrometheus(H, conn, PROMETHEUS_TEXT);
break;
}
if (H.url == "/"+Controller::conf.getString("prometheus")){
if (H.url == "/"+Controller::conf.getString("prometheus")+".json"){
handlePrometheus(H, conn, PROMETHEUS_JSON);
break;
}

View file

@ -985,8 +985,9 @@ void Controller::handlePrometheus(HTTP::Parser & H, Socket::Connection & conn, i
}
H.SetHeader("Server", "MistServer/" PACKAGE_VERSION);
H.StartResponse("200", "OK", H, conn);
std::stringstream response;
if (mode == PROMETHEUS_TEXT){
std::stringstream response;
{//Scope for shortest possible blocking of statsMutex
tthread::lock_guard<tthread::mutex> guard(statsMutex);
response << "# HELP mist_sessions_cached Number of sessions active in the last ~10 minutes.\n";
@ -1041,12 +1042,47 @@ void Controller::handlePrometheus(HTTP::Parser & H, Socket::Connection & conn, i
response << "mist_upload{stream=\"" << it->first << "\"} " << it->second.upBytes << "\n";
response << "mist_download{stream=\"" << it->first << "\"} " << it->second.downBytes << "\n";
}
}
H.Chunkify(response.str(), conn);
}
if (mode == PROMETHEUS_JSON){
JSON::Value resp;
{//Scope for shortest possible blocking of statsMutex
tthread::lock_guard<tthread::mutex> guard(statsMutex);
//collect the data first
std::map<std::string, unsigned long> clients;
unsigned long totClients = 0;
unsigned int t = Util::epoch() - 15;
//check all sessions
if (sessions.size()){
for (std::map<sessIndex, statSession>::iterator it = sessions.begin(); it != sessions.end(); it++){
if (it->second.hasDataFor(t) && it->second.isViewerOn(t)){
clients[it->first.streamName]++;
totClients++;
}
}
}
resp["sess_cached"] = (long long)sessions.size();
resp["sess_current"] = (long long)totClients;
resp["sess_total"] = (long long)servClients;
resp["upload"] = (long long)servUpBytes;
resp["download"] = (long long)servDownBytes;
for (std::map<std::string, unsigned long>::iterator it = clients.begin(); it != clients.end(); ++it){
resp["streams"][it->first]["sess_current"] = (long long)it->second;
}
std::set<std::string> mustWipe;
for (std::map<std::string, struct streamTotals>::iterator it = streamStats.begin(); it != streamStats.end(); ++it){
resp["streams"][it->first]["sess_total"] = (long long)it->second.clients;
resp["streams"][it->first]["upload"] = (long long)it->second.upBytes;
resp["streams"][it->first]["download"] = (long long)it->second.downBytes;
}
}
H.Chunkify(resp.toString(), conn);
}
H.Chunkify(response.str(), conn);
H.Chunkify("", conn);
H.Clean();
}