10#include <boost/crc.hpp>
26 if (
const char* env_p = std::getenv(
"DUNEDAQ_PARTITION"))
30 std::string brokers = param;
33 RdKafka::Conf* conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
34 conf->set(
"bootstrap.servers", brokers, errstr);
36 std::cout <<
"Bootstrap server error : " << errstr <<
'\n';
38 if (
const char* env_p = std::getenv(
"DUNEDAQ_APPLICATION_NAME"))
39 conf->set(
"client.id", env_p, errstr);
41 conf->set(
"client.id",
"erskafkaproducerdefault", errstr);
43 std::cout <<
"Producer configuration error : " << errstr <<
'\n';
46 m_producer = RdKafka::Producer::create(conf, errstr);
49 std::cout <<
"Producer creation error : " << errstr <<
'\n';
57 nlohmann::json message;
60 message[
"message"] = issue.
message().c_str();
62 message[
"usecs_since_epoch"] =
63 std::chrono::duration_cast<std::chrono::microseconds>(issue.
ptime().time_since_epoch()).count();
64 message[
"time"] = std::chrono::duration_cast<std::chrono::milliseconds>(issue.
ptime().time_since_epoch()).count();
67 message[
"params"] = nlohmann::json::array({});
69 message[
"params"].push_back(p.first +
": " + p.second);
82 message[
"chain"] = chain;
87 j_objs.push_back(message);
88 }
catch (
const std::exception& e) {
89 std::cout <<
"Conversion from json error : " << e.what() <<
'\n';
98 RdKafka::ErrorCode err =
m_producer->produce(topic,
99 RdKafka::Topic::PARTITION_UA,
100 RdKafka::Producer::RK_MSG_COPY,
101 const_cast<char*
>(input.c_str()),
108 if (err != RdKafka::ERR_NO_ERROR) {
109 std::cout <<
"% Failed to produce to topic " << topic <<
": " << RdKafka::err2str(err) << std::endl;
111 }
catch (
const std::exception& e) {
112 std::cout <<
"Producer error : " << e.what() <<
'\n';
123 std::vector<nlohmann::json> j_objs;
124 if (issue.
cause() ==
nullptr) {
131 std::ostringstream tmpstream(issue.
message());
134 std::string tmp = tmpstream.str();
135 boost::crc_32_type crc32;
136 crc32.process_bytes(tmp.c_str(), tmp.length());
138 for (
auto j : j_objs) {
139 j[
"group_hash"] = crc32.checksum();
144 }
catch (
const std::exception& e) {
145 std::cout <<
"Producer error : " << e.what() <<
'\n';
virtual int line_number() const =0
virtual const char * user_name() const =0
virtual int user_id() const =0
virtual pid_t thread_id() const =0
virtual const char * host_name() const =0
virtual pid_t process_id() const =0
virtual const char * package_name() const =0
virtual const char * application_name() const =0
virtual const char * file_name() const =0
virtual const char * cwd() const =0
virtual const char * function_name() const =0
Base class for any user define issue.
const Context & context() const
Context of the issue.
ers::Severity severity() const
severity of the issue
virtual const char * get_class_name() const =0
Get key for class (used for serialisation).
const system_clock::time_point & ptime() const
original time point of the issue
const std::vector< std::string > & qualifiers() const
return array of qualifiers
const std::string & message() const
General cause of the issue.
const string_map & parameters() const
return array of parameters
std::time_t time_t() const
seconds since 1 Jan 1970
const Issue * cause() const
return the cause Issue of this Issue
ES stream implementation.
RdKafka::Producer * m_producer
void write(const ers::Issue &issue) override
void kafka_exporter(std::string input, std::string topic)
KafkaStream(const std::string ¶m)
void ers_to_json(const ers::Issue &issue, size_t chain, std::vector< nlohmann::json > &j_objs)
#define ERS_REGISTER_OUTPUT_STREAM(class, name, param)
std::string to_string(severity s)