|
| 1 | +/// |
| 2 | +/// \file 11-KafkaToHttp.cxx |
| 3 | +/// \author Adam Wegrzynek <[email protected]> |
| 4 | +/// |
| 5 | + |
| 6 | +#include "../src/Transports/KafkaConsumer.h" |
| 7 | +#include "../src/Transports/HTTP.h" |
| 8 | +#include "../src/Transports/WebSocket.h" |
| 9 | + |
| 10 | +#include <iostream> |
| 11 | +#include <memory> |
| 12 | +#include <thread> |
| 13 | +#include <boost/algorithm/string/join.hpp> |
| 14 | +#include <boost/program_options.hpp> |
| 15 | + |
| 16 | +using namespace o2::monitoring; |
| 17 | + |
| 18 | +int main(int argc, char* argv[]) |
| 19 | +{ |
| 20 | + boost::program_options::options_description desc("Program options"); |
| 21 | + desc.add_options() |
| 22 | + ("kafka-host", boost::program_options::value<std::string>()->required(), "Kafka broker hostname") |
| 23 | + ("kafka-topic", boost::program_options::value<std::string>()->required(), "Kafka topic") |
| 24 | + ("grafana-host", boost::program_options::value<std::string>()->required(), "Grafana hostname") |
| 25 | + ("grafana-key", boost::program_options::value<std::string>()->required(), "Grafana API key"); |
| 26 | + boost::program_options::variables_map vm; |
| 27 | + boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); |
| 28 | + boost::program_options::notify(vm); |
| 29 | + |
| 30 | + auto kafkaConsumer = std::make_unique<transports::KafkaConsumer>(vm["kafka-host"].as<std::string>(), 9092, vm["kafka-topic"].as<std::string>()); |
| 31 | + auto outTransport = std::make_unique<transports::WebSocket>(vm["grafana-host"].as<std::string>(), 3000, vm["grafana-key"].as<std::string>(), "alice_o2"); |
| 32 | + std::thread readThread([&outTransport](){ |
| 33 | + for (;;) { |
| 34 | + outTransport->read(); |
| 35 | + std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 36 | + } |
| 37 | + }); |
| 38 | + for (;;) { |
| 39 | + auto metrics = kafkaConsumer->receive(); |
| 40 | + if (!metrics.empty()) { |
| 41 | + outTransport->send(boost::algorithm::join(metrics, "\n")); |
| 42 | + } |
| 43 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 44 | + } |
| 45 | +} |
0 commit comments