Added trigger statistics to Prometheus-style outputs, fixed executable triggers not correctly responding with the default on execv fail

This commit is contained in:
Thulinma 2018-12-13 14:56:52 +01:00
parent e18f2f0b47
commit 8b7757c8e9
5 changed files with 89 additions and 4 deletions

View file

@ -433,6 +433,23 @@ static void removeDuplicateProtocols(){
}
void Controller::handleAPICommands(JSON::Value & Request, JSON::Value & Response){
/*LTS-START*/
//These are only used internally. We abort further processing if encountered.
if (Request.isMember("trigger_stat")){
JSON::Value & tStat = Request["trigger_stat"];
if (tStat.isMember("name") && tStat.isMember("ms")){
Controller::triggerLog & tLog = Controller::triggerStats[tStat["name"].asStringRef()];
tLog.totalCount++;
tLog.ms += tStat["ms"].asInt();
if (!tStat.isMember("ok") || !tStat["ok"].asBool()){tLog.failCount++;}
}
return;
}
if (Request.isMember("trigger_fail")){
Controller::triggerStats[Request["trigger_fail"].asStringRef()].failCount++;
return;
}
/*LTS-END*/
//Parse config and streams from the request.
if (Request.isMember("config") && Request["config"].isObject()){
const JSON::Value & in = Request["config"];

View file

@ -43,6 +43,8 @@
std::map<Controller::sessIndex, Controller::statSession> Controller::sessions; ///< list of sessions that have statistics data available
std::map<unsigned long, Controller::sessIndex> Controller::connToSession; ///< Map of socket IDs to session info.
std::map<std::string, Controller::triggerLog> Controller::triggerStats; ///< Holds prometheus stats for trigger executions
bool Controller::killOnExit = KILL_ON_EXIT;
tthread::mutex Controller::statsMutex;
unsigned int Controller::maxConnsPerIP = 0;
@ -1611,6 +1613,18 @@ void Controller::handlePrometheus(HTTP::Parser & H, Socket::Connection & conn, i
response << "# TYPE mist_shm_used gauge\n";
response << "mist_shm_used " << (shm_total - shm_free) << "\n\n";
if (Controller::triggerStats.size()){
response << "# HELP mist_trigger_count Total executions for the given trigger\n";
response << "# HELP mist_trigger_time Total execution time in millis for the given trigger\n";
response << "# HELP mist_trigger_fails Total failed executions for the given trigger\n";
for (std::map<std::string, Controller::triggerLog>::iterator it = Controller::triggerStats.begin(); it != Controller::triggerStats.end(); it++){
response << "mist_trigger_count{trigger=\"" << it->first << "\"} " << it->second.totalCount << "\n";
response << "mist_trigger_time{trigger=\"" << it->first << "\"} " << it->second.ms << "\n";
response << "mist_trigger_fails{trigger=\"" << it->first << "\"} " << it->second.failCount << "\n";
}
response << "\n";
}
{//Scope for shortest possible blocking of statsMutex
tthread::lock_guard<tthread::mutex> guard(statsMutex);
//collect the data first
@ -1699,6 +1713,14 @@ void Controller::handlePrometheus(HTTP::Parser & H, Socket::Connection & conn, i
resp["shm_total"] = shm_total;
resp["shm_used"] = (shm_total - shm_free);
resp["logs"] = Controller::logCounter;
if (Controller::triggerStats.size()){
for (std::map<std::string, Controller::triggerLog>::iterator it = Controller::triggerStats.begin(); it != Controller::triggerStats.end(); it++){
JSON::Value & tVal = resp["triggers"][it->first];
tVal["count"] = it->second.totalCount;
tVal["ms"] = it->second.ms;
tVal["fails"] = it->second.failCount;
}
}
{//Scope for shortest possible blocking of statsMutex
tthread::lock_guard<tthread::mutex> guard(statsMutex);
//collect the data first

View file

@ -119,6 +119,15 @@ namespace Controller {
extern std::map<unsigned long, sessIndex> connToSession;
extern tthread::mutex statsMutex;
struct triggerLog {
uint64_t totalCount;
uint64_t failCount;
uint64_t ms;
};
extern std::map<std::string, triggerLog> triggerStats;
std::set<std::string> getActiveStreams(const std::string & prefix = "");
void parseStatistics(char * data, size_t len, unsigned int id);
void killStatistics(char * data, size_t len, unsigned int id);