cobra subscriber in fluentd mode insert a created_at timestamp entry

This commit is contained in:
Benjamin Sergeant
2020-04-14 15:30:30 -07:00
parent 783d1d92dd
commit bea582c208
6 changed files with 41 additions and 19 deletions

View File

@ -268,8 +268,10 @@ int main(int argc, char** argv)
cobra2statsd->add_option("--port", statsdPort, "Statsd port");
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("--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");
@ -449,7 +451,8 @@ int main(int argc, char** argv)
}
else if (app.got_subcommand("cobra_subscribe"))
{
ret = ix::ws_cobra_subscribe_main(cobraConfig, channel, filter, position, quiet, fluentd, runtime);
ret = ix::ws_cobra_subscribe_main(
cobraConfig, channel, filter, position, quiet, fluentd, runtime);
}
else if (app.got_subcommand("cobra_publish"))
{
@ -463,7 +466,7 @@ int main(int argc, char** argv)
{
if (!timer.empty() && !gauge.empty())
{
spdlog::error("--gauge and --timer options are exclusive. " \
spdlog::error("--gauge and --timer options are exclusive. "
"you can only supply one");
ret = 1;
}

View File

@ -25,6 +25,17 @@ namespace ix
return jsonWriter;
}
std::string timeSinceEpoch()
{
std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
std::chrono::system_clock::duration dtn = tp.time_since_epoch();
std::stringstream ss;
ss << dtn.count() * std::chrono::system_clock::period::num /
std::chrono::system_clock::period::den;
return ss.str();
}
void writeToStdout(bool fluentd,
const StreamWriterPtr& jsonWriter,
const Json::Value& msg,
@ -36,12 +47,13 @@ namespace ix
enveloppe["producer"] = "cobra";
enveloppe["consumer"] = "fluentd";
Json::Value msgWithPosition(msg);
msgWithPosition["position"] = position;
enveloppe["message"] = msgWithPosition;
Json::Value nestedMessage(msg);
nestedMessage["position"] = position;
nestedMessage["created_at"] = timeSinceEpoch();
enveloppe["message"] = nestedMessage;
jsonWriter->write(enveloppe, &std::cout);
std::cout << std::endl; // add lf and flush
std::cout << std::endl; // add lf and flush
}
else
{
@ -184,7 +196,7 @@ namespace ix
// Run for a duration, used by unittesting now
else
{
for (int i = 0 ; i < runtime; ++i)
for (int i = 0; i < runtime; ++i)
{
auto duration = std::chrono::seconds(1);
std::this_thread::sleep_for(duration);