(cobra to statsd bot) add ability to extract a numerical value and send a gauge event to statsd

This commit is contained in:
Benjamin Sergeant 2020-03-29 19:31:49 -07:00
parent cfa5718e40
commit f8bf1fe7cd
6 changed files with 58 additions and 6 deletions

View File

@ -1,6 +1,10 @@
# Changelog # Changelog
All changes to this project will be documented in this file. All changes to this project will be documented in this file.
## [9.1.7] - 2020-03-29
(cobra to statsd bot) add ability to extract a numerical value and send a gauge event to statsd
## [9.1.6] - 2020-03-29 ## [9.1.6] - 2020-03-29
(ws cobra subscriber) use a Json::StreamWriter to write to std::cout, and save one std::string allocation for each message printed (ws cobra subscriber) use a Json::StreamWriter to write to std::cout, and save one std::string allocation for each message printed

View File

@ -40,7 +40,7 @@ namespace ix
// Extract an attribute from a Json Value. // Extract an attribute from a Json Value.
// extractAttr("foo.bar", {"foo": {"bar": "baz"}}) => baz // extractAttr("foo.bar", {"foo": {"bar": "baz"}}) => baz
// //
std::string extractAttr(const std::string& attr, const Json::Value& jsonValue) Json::Value extractAttr(const std::string& attr, const Json::Value& jsonValue)
{ {
// Split by . // Split by .
std::string token; std::string token;
@ -53,7 +53,7 @@ namespace ix
val = val[token]; val = val[token];
} }
return val.asString(); return val;
} }
int cobra_to_statsd_bot(const ix::CobraConfig& config, int cobra_to_statsd_bot(const ix::CobraConfig& config,
@ -62,6 +62,7 @@ namespace ix
const std::string& position, const std::string& position,
StatsdClient& statsdClient, StatsdClient& statsdClient,
const std::string& fields, const std::string& fields,
const std::string& gauge,
bool verbose, bool verbose,
size_t maxQueueSize, size_t maxQueueSize,
bool enableHeartbeat, bool enableHeartbeat,
@ -124,7 +125,7 @@ namespace ix
std::thread t2(heartbeat); std::thread t2(heartbeat);
auto statsdSender = [&statsdClient, &queueManager, &sentCount, &tokens, &stop] { auto statsdSender = [&statsdClient, &queueManager, &sentCount, &tokens, &stop, &gauge, &fatalCobraError] {
while (true) while (true)
{ {
Json::Value msg = queueManager.pop(); Json::Value msg = queueManager.pop();
@ -136,10 +137,51 @@ namespace ix
for (auto&& attr : tokens) for (auto&& attr : tokens)
{ {
id += "."; id += ".";
id += extractAttr(attr, msg); auto val = extractAttr(attr, msg);
id += val.asString();
}
if (gauge.empty())
{
statsdClient.count(id, 1);
}
else
{
// spdlog::info("{} - {} -> {}", id, gauge, x);
auto val = extractAttr(gauge, msg);
if (val.isInt())
{
auto x = val.asInt();
statsdClient.gauge(id, (size_t) x);
}
else if (val.isInt64())
{
auto x = val.asInt64();
statsdClient.gauge(id, (size_t) x);
}
else if (val.isUInt())
{
auto x = val.asUInt();
statsdClient.gauge(id, (size_t) x);
}
else if (val.isUInt64())
{
auto x = val.asUInt64();
statsdClient.gauge(id, (size_t) x);
}
else if (val.isDouble())
{
auto x = val.asUInt64();
statsdClient.gauge(id, (size_t) x);
}
else
{
spdlog::error("Gauge {} is not a numberic type", gauge);
fatalCobraError = true;
}
} }
statsdClient.count(id, 1);
sentCount += 1; sentCount += 1;
} }
}; };

View File

@ -18,6 +18,7 @@ namespace ix
const std::string& position, const std::string& position,
StatsdClient& statsdClient, StatsdClient& statsdClient,
const std::string& fields, const std::string& fields,
const std::string& gauge,
bool verbose, bool verbose,
size_t maxQueueSize, size_t maxQueueSize,
bool enableHeartbeat, bool enableHeartbeat,

View File

@ -6,4 +6,4 @@
#pragma once #pragma once
#define IX_WEBSOCKET_VERSION "9.1.6" #define IX_WEBSOCKET_VERSION "9.1.7"

View File

@ -111,6 +111,7 @@ TEST_CASE("Cobra_to_statsd_bot", "[cobra_bots]")
REQUIRE(initialized); REQUIRE(initialized);
std::string fields("device.game\ndevice.os_name"); std::string fields("device.game\ndevice.os_name");
std::string gauge;
int sentCount = ix::cobra_to_statsd_bot(config, int sentCount = ix::cobra_to_statsd_bot(config,
channel, channel,
@ -118,6 +119,7 @@ TEST_CASE("Cobra_to_statsd_bot", "[cobra_bots]")
position, position,
statsdClient, statsdClient,
fields, fields,
gauge,
verbose, verbose,
maxQueueSize, maxQueueSize,
enableHeartbeat, enableHeartbeat,

View File

@ -70,6 +70,7 @@ int main(int argc, char** argv)
std::string password; std::string password;
std::string prefix("ws.test.v0"); std::string prefix("ws.test.v0");
std::string fields; std::string fields;
std::string gauge;
std::string dsn; std::string dsn;
std::string redisHosts("127.0.0.1"); std::string redisHosts("127.0.0.1");
std::string redisPassword; std::string redisPassword;
@ -264,6 +265,7 @@ int main(int argc, char** argv)
cobra2statsd->add_option("--port", statsdPort, "Statsd port"); cobra2statsd->add_option("--port", statsdPort, "Statsd port");
cobra2statsd->add_option("--prefix", prefix, "Statsd prefix"); cobra2statsd->add_option("--prefix", prefix, "Statsd prefix");
cobra2statsd->add_option("--fields", fields, "Extract fields for naming the event")->join(); cobra2statsd->add_option("--fields", fields, "Extract fields for naming the event")->join();
cobra2statsd->add_option("--gauge", gauge, "Value to extract, and use as a statsd gauge")->join();
cobra2statsd->add_option("channel", channel, "Channel")->required(); cobra2statsd->add_option("channel", channel, "Channel")->required();
cobra2statsd->add_flag("-v", verbose, "Verbose"); cobra2statsd->add_flag("-v", verbose, "Verbose");
cobra2statsd->add_option("--pidfile", pidfile, "Pid file"); cobra2statsd->add_option("--pidfile", pidfile, "Pid file");
@ -463,6 +465,7 @@ int main(int argc, char** argv)
position, position,
statsdClient, statsdClient,
fields, fields,
gauge,
verbose, verbose,
maxQueueSize, maxQueueSize,
enableHeartbeat, enableHeartbeat,