(cobra to statsd bot) add ability to extract a numerical value and send a timer event to statsd, with the --timer option

This commit is contained in:
Benjamin Sergeant 2020-03-30 16:08:47 -07:00
parent 296762ce06
commit 1d3db5f75b
7 changed files with 70 additions and 40 deletions

View File

@ -1,13 +1,17 @@
# 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.9] - 2020-03-30
(cobra to statsd bot) add ability to extract a numerical value and send a timer event to statsd, with the --timer option
## [9.1.8] - 2020-03-29 ## [9.1.8] - 2020-03-29
(cobra to statsd bot) bot init was missing + capture socket error (cobra to statsd bot) bot init was missing + capture socket error
## [9.1.7] - 2020-03-29 ## [9.1.7] - 2020-03-29
(cobra to statsd bot) add ability to extract a numerical value and send a gauge event to statsd (cobra to statsd bot) add ability to extract a numerical value and send a gauge event to statsd, with the --gauge option
## [9.1.6] - 2020-03-29 ## [9.1.6] - 2020-03-29

View File

@ -63,6 +63,7 @@ namespace ix
StatsdClient& statsdClient, StatsdClient& statsdClient,
const std::string& fields, const std::string& fields,
const std::string& gauge, const std::string& gauge,
const std::string& timer,
bool verbose, bool verbose,
size_t maxQueueSize, size_t maxQueueSize,
bool enableHeartbeat, bool enableHeartbeat,
@ -82,7 +83,7 @@ namespace ix
QueueManager queueManager(maxQueueSize); QueueManager queueManager(maxQueueSize);
auto timer = [&sentCount, &receivedCount, &stop] { auto progress = [&sentCount, &receivedCount, &stop] {
while (!stop) while (!stop)
{ {
spdlog::info("messages received {} sent {}", receivedCount, sentCount); spdlog::info("messages received {} sent {}", receivedCount, sentCount);
@ -94,7 +95,7 @@ namespace ix
spdlog::info("timer thread done"); spdlog::info("timer thread done");
}; };
std::thread t1(timer); std::thread t1(progress);
auto heartbeat = [&sentCount, &receivedCount, &stop, &enableHeartbeat] { auto heartbeat = [&sentCount, &receivedCount, &stop, &enableHeartbeat] {
std::string state("na"); std::string state("na");
@ -125,7 +126,7 @@ namespace ix
std::thread t2(heartbeat); std::thread t2(heartbeat);
auto statsdSender = [&statsdClient, &queueManager, &sentCount, &tokens, &stop, &gauge, &fatalCobraError] { auto statsdSender = [&statsdClient, &queueManager, &sentCount, &tokens, &stop, &gauge, &timer, &fatalCobraError, &verbose] {
while (true) while (true)
{ {
Json::Value msg = queueManager.pop(); Json::Value msg = queueManager.pop();
@ -141,44 +142,55 @@ namespace ix
id += val.asString(); id += val.asString();
} }
if (gauge.empty()) if (gauge.empty() && timer.empty())
{ {
statsdClient.count(id, 1); statsdClient.count(id, 1);
} }
else else
{ {
// spdlog::info("{} - {} -> {}", id, gauge, x); std::string attrName = (!gauge.empty()) ? gauge : timer;
auto val = extractAttr(gauge, msg); auto val = extractAttr(attrName, msg);
size_t x;
if (val.isInt()) if (val.isInt())
{ {
auto x = val.asInt(); x = (size_t) val.asInt();
statsdClient.gauge(id, (size_t) x);
} }
else if (val.isInt64()) else if (val.isInt64())
{ {
auto x = val.asInt64(); x = (size_t) val.asInt64();
statsdClient.gauge(id, (size_t) x);
} }
else if (val.isUInt()) else if (val.isUInt())
{ {
auto x = val.asUInt(); x = (size_t) val.asUInt();
statsdClient.gauge(id, (size_t) x);
} }
else if (val.isUInt64()) else if (val.isUInt64())
{ {
auto x = val.asUInt64(); x = (size_t) val.asUInt64();
statsdClient.gauge(id, (size_t) x);
} }
else if (val.isDouble()) else if (val.isDouble())
{ {
auto x = val.asUInt64(); x = (size_t) val.asUInt64();
statsdClient.gauge(id, (size_t) x);
} }
else else
{ {
spdlog::error("Gauge {} is not a numberic type", gauge); spdlog::error("Gauge {} is not a numberic type", gauge);
fatalCobraError = true; fatalCobraError = true;
break;
}
if (verbose)
{
spdlog::info("{} - {} -> {}", id, attrName, x);
}
if (!gauge.empty())
{
statsdClient.gauge(id, x);
}
else
{
statsdClient.timing(id, x);
} }
} }

View File

@ -19,6 +19,7 @@ namespace ix
StatsdClient& statsdClient, StatsdClient& statsdClient,
const std::string& fields, const std::string& fields,
const std::string& gauge, const std::string& gauge,
const std::string& timer,
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.8" #define IX_WEBSOCKET_VERSION "9.1.9"

View File

@ -82,8 +82,7 @@ docker_push:
docker push ${LATEST} docker push ${LATEST}
docker push ${IMG} docker push ${IMG}
deploy: deploy: docker docker_push
docker docker_push
run: run:
docker run --cap-add sys_ptrace --entrypoint=sh -it bsergean/ws:build docker run --cap-add sys_ptrace --entrypoint=sh -it bsergean/ws:build

View File

@ -112,6 +112,7 @@ TEST_CASE("Cobra_to_statsd_bot", "[cobra_bots]")
std::string fields("device.game\ndevice.os_name"); std::string fields("device.game\ndevice.os_name");
std::string gauge; std::string gauge;
std::string timer;
int sentCount = ix::cobra_to_statsd_bot(config, int sentCount = ix::cobra_to_statsd_bot(config,
channel, channel,
@ -120,6 +121,7 @@ TEST_CASE("Cobra_to_statsd_bot", "[cobra_bots]")
statsdClient, statsdClient,
fields, fields,
gauge, gauge,
timer,
verbose, verbose,
maxQueueSize, maxQueueSize,
enableHeartbeat, enableHeartbeat,

View File

@ -71,6 +71,7 @@ int main(int argc, char** argv)
std::string prefix("ws.test.v0"); std::string prefix("ws.test.v0");
std::string fields; std::string fields;
std::string gauge; std::string gauge;
std::string timer;
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;
@ -266,6 +267,7 @@ int main(int argc, char** argv)
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("--gauge", gauge, "Value to extract, and use as a statsd gauge")->join();
cobra2statsd->add_option("--timer", timer, "Value to extract, and use as a statsd timer")->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");
@ -454,9 +456,17 @@ int main(int argc, char** argv)
ret = ix::ws_cobra_metrics_publish_main(cobraConfig, channel, path, stress); ret = ix::ws_cobra_metrics_publish_main(cobraConfig, channel, path, stress);
} }
else if (app.got_subcommand("cobra_to_statsd")) else if (app.got_subcommand("cobra_to_statsd"))
{
if (!timer.empty() && !gauge.empty())
{
spdlog::error("--gauge and --timer options are exclusive. " \
"you can only supply one");
ret = 1;
}
else
{ {
bool enableHeartbeat = true; bool enableHeartbeat = true;
int runtime = -1; int runtime = -1; // run indefinitely
ix::StatsdClient statsdClient(hostname, statsdPort, prefix); ix::StatsdClient statsdClient(hostname, statsdPort, prefix);
std::string errMsg; std::string errMsg;
@ -464,7 +474,7 @@ int main(int argc, char** argv)
if (!initialized) if (!initialized)
{ {
spdlog::error(errMsg); spdlog::error(errMsg);
ret = 0; ret = 1;
} }
else else
{ {
@ -475,12 +485,14 @@ int main(int argc, char** argv)
statsdClient, statsdClient,
fields, fields,
gauge, gauge,
timer,
verbose, verbose,
maxQueueSize, maxQueueSize,
enableHeartbeat, enableHeartbeat,
runtime); runtime);
} }
} }
}
else if (app.got_subcommand("cobra_to_sentry")) else if (app.got_subcommand("cobra_to_sentry"))
{ {
bool enableHeartbeat = true; bool enableHeartbeat = true;