(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
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
(cobra to statsd bot) bot init was missing + capture socket error
## [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

View File

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

View File

@ -6,4 +6,4 @@
#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 ${IMG}
deploy:
docker docker_push
deploy: docker docker_push
run:
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 gauge;
std::string timer;
int sentCount = ix::cobra_to_statsd_bot(config,
channel,
@ -120,6 +121,7 @@ TEST_CASE("Cobra_to_statsd_bot", "[cobra_bots]")
statsdClient,
fields,
gauge,
timer,
verbose,
maxQueueSize,
enableHeartbeat,

View File

@ -71,6 +71,7 @@ int main(int argc, char** argv)
std::string prefix("ws.test.v0");
std::string fields;
std::string gauge;
std::string timer;
std::string dsn;
std::string redisHosts("127.0.0.1");
std::string redisPassword;
@ -266,6 +267,7 @@ int main(int argc, char** argv)
cobra2statsd->add_option("--prefix", prefix, "Statsd prefix");
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("--timer", timer, "Value to extract, and use as a statsd timer")->join();
cobra2statsd->add_option("channel", channel, "Channel")->required();
cobra2statsd->add_flag("-v", verbose, "Verbose");
cobra2statsd->add_option("--pidfile", pidfile, "Pid file");
@ -455,30 +457,40 @@ int main(int argc, char** argv)
}
else if (app.got_subcommand("cobra_to_statsd"))
{
bool enableHeartbeat = true;
int runtime = -1;
ix::StatsdClient statsdClient(hostname, statsdPort, prefix);
std::string errMsg;
bool initialized = statsdClient.init(errMsg);
if (!initialized)
if (!timer.empty() && !gauge.empty())
{
spdlog::error(errMsg);
ret = 0;
spdlog::error("--gauge and --timer options are exclusive. " \
"you can only supply one");
ret = 1;
}
else
{
ret = ix::cobra_to_statsd_bot(cobraConfig,
channel,
filter,
position,
statsdClient,
fields,
gauge,
verbose,
maxQueueSize,
enableHeartbeat,
runtime);
bool enableHeartbeat = true;
int runtime = -1; // run indefinitely
ix::StatsdClient statsdClient(hostname, statsdPort, prefix);
std::string errMsg;
bool initialized = statsdClient.init(errMsg);
if (!initialized)
{
spdlog::error(errMsg);
ret = 1;
}
else
{
ret = ix::cobra_to_statsd_bot(cobraConfig,
channel,
filter,
position,
statsdClient,
fields,
gauge,
timer,
verbose,
maxQueueSize,
enableHeartbeat,
runtime);
}
}
}
else if (app.got_subcommand("cobra_to_sentry"))