From 0f88969b77e00a4573d6b115e3425ffe0aa149ea Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Thu, 4 Jun 2020 09:36:28 -0700 Subject: [PATCH] add metrics statsd files --- ixbots/ixbots/IXCobraMetricsToStatsdBot.cpp | 108 ++++++++++++++++++++ ixbots/ixbots/IXCobraMetricsToStatsdBot.h | 19 ++++ 2 files changed, 127 insertions(+) create mode 100644 ixbots/ixbots/IXCobraMetricsToStatsdBot.cpp create mode 100644 ixbots/ixbots/IXCobraMetricsToStatsdBot.h diff --git a/ixbots/ixbots/IXCobraMetricsToStatsdBot.cpp b/ixbots/ixbots/IXCobraMetricsToStatsdBot.cpp new file mode 100644 index 00000000..91b78ba9 --- /dev/null +++ b/ixbots/ixbots/IXCobraMetricsToStatsdBot.cpp @@ -0,0 +1,108 @@ +/* + * IXCobraMetricsToStatsdBot.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2020 Machine Zone, Inc. All rights reserved. + */ + +#include "IXCobraMetricsToStatsdBot.h" + +#include "IXCobraBot.h" +#include "IXStatsdClient.h" +#include +#include +#include +#include +#include + +namespace +{ + // + // Extract an attribute from a Json Value. + // extractAttr("foo.bar", {"foo": {"bar": "baz"}}) => baz + // + Json::Value extractAttr(const std::string& attr, const Json::Value& jsonValue) + { + // Split by . + std::string token; + std::stringstream tokenStream(attr); + + Json::Value val(jsonValue); + + while (std::getline(tokenStream, token, '.')) + { + val = val[token]; + } + + return val; + } + +} + +namespace ix +{ + bool processPerfMetricsEvent(const Json::Value& msg, + StatsdClient& statsdClient) + { + auto frameRateHistogram = msg["data"]["FrameRateHistogram"]; + auto frameTimeTotal = msg["data"]["FrameTimeTotal"].asFloat(); + + auto gt_60 = 100 * frameRateHistogram[0].asFloat() / frameTimeTotal; + auto lt_60 = 100 * frameRateHistogram[1].asFloat() / frameTimeTotal; + auto lt_30 = 100 * frameRateHistogram[2].asFloat() / frameTimeTotal; + auto lt_20 = 100 * frameRateHistogram[3].asFloat() / frameTimeTotal; + auto lt_15 = 100 * frameRateHistogram[4].asFloat() / frameTimeTotal; + auto lt_12 = 100 * frameRateHistogram[5].asFloat() / frameTimeTotal; + auto lt_10 = 100 * frameRateHistogram[6].asFloat() / frameTimeTotal; + auto lt_08 = 100 * frameRateHistogram[7].asFloat() / frameTimeTotal; + + std::stringstream ss; + ss << msg["id"].asString() << "." + << msg["device"]["game"].asString() << "." + << msg["device"]["os_name"].asString() << "." + << msg["data"]["Tag"].asString(); + + std::string id = ss.str(); + + statsdClient.gauge(id + ".gt_60", gt_60); + statsdClient.gauge(id + ".lt_60", lt_60); + statsdClient.gauge(id + ".lt_30", lt_30); + statsdClient.gauge(id + ".lt_20", lt_20); + statsdClient.gauge(id + ".lt_15", lt_15); + statsdClient.gauge(id + ".lt_12", lt_12); + statsdClient.gauge(id + ".lt_10", lt_10); + statsdClient.gauge(id + ".lt_08", lt_08); + } + + int64_t cobra_metrics_to_statsd_bot(const ix::CobraBotConfig& config, + StatsdClient& statsdClient, + bool verbose) + { + CobraBot bot; + bot.setOnBotMessageCallback( + [&statsdClient, &verbose](const Json::Value& msg, + const std::string& /*position*/, + std::atomic& /*throttled*/, + std::atomic& fatalCobraError, + std::atomic& sentCount) -> void { + if (msg["device"].isNull() || msg["id"].isNull()) + { + CoreLogger::info("no device or id entry, skipping event"); + return; + } + + // + // Display full message with + // CoreLogger::info(msg.toStyledString()); + // + bool success = false; + if (msg["id"].asString() == "engine_performance_metrics_id") + { + success = processPerfMetricsEvent(msg, statsdClient); + } + + if (success) sentCount++; + }); + + return bot.run(config); + } +} diff --git a/ixbots/ixbots/IXCobraMetricsToStatsdBot.h b/ixbots/ixbots/IXCobraMetricsToStatsdBot.h new file mode 100644 index 00000000..6c9b980b --- /dev/null +++ b/ixbots/ixbots/IXCobraMetricsToStatsdBot.h @@ -0,0 +1,19 @@ +/* + * IXCobraMetricsToStatsdBot.h + * Author: Benjamin Sergeant + * Copyright (c) 2020 Machine Zone, Inc. All rights reserved. + */ +#pragma once + +#include +#include +#include "IXCobraBotConfig.h" +#include +#include + +namespace ix +{ + int64_t cobra_metrics_to_statsd_bot(const ix::CobraBotConfig& config, + StatsdClient& statsdClient, + bool verbose); +} // namespace ix