kafka_sink.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. //
  5. // Custom sink for kafka
  6. // Building and using requires librdkafka library.
  7. // For building librdkafka library check the url below
  8. // https://github.com/confluentinc/librdkafka
  9. //
  10. #include "spdlog/async.h"
  11. #include "spdlog/details/log_msg.h"
  12. #include "spdlog/details/null_mutex.h"
  13. #include "spdlog/details/synchronous_factory.h"
  14. #include "spdlog/sinks/base_sink.h"
  15. #include <mutex>
  16. #include <spdlog/common.h>
  17. // kafka header
  18. #include <librdkafka/rdkafkacpp.h>
  19. namespace spdlog {
  20. namespace sinks {
  21. struct kafka_sink_config {
  22. std::string server_addr;
  23. std::string produce_topic;
  24. int32_t flush_timeout_ms = 1000;
  25. kafka_sink_config(std::string addr, std::string topic, int flush_timeout_ms = 1000)
  26. : server_addr{std::move(addr)},
  27. produce_topic{std::move(topic)},
  28. flush_timeout_ms(flush_timeout_ms) {}
  29. };
  30. template <typename Mutex>
  31. class kafka_sink : public base_sink<Mutex> {
  32. public:
  33. kafka_sink(kafka_sink_config config)
  34. : config_{std::move(config)} {
  35. try {
  36. std::string errstr;
  37. conf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL));
  38. RdKafka::Conf::ConfResult confRes =
  39. conf_->set("bootstrap.servers", config_.server_addr, errstr);
  40. if (confRes != RdKafka::Conf::CONF_OK) {
  41. throw_spdlog_ex(
  42. fmt_lib::format("conf set bootstrap.servers failed err:{}", errstr));
  43. }
  44. tconf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC));
  45. if (tconf_ == nullptr) {
  46. throw_spdlog_ex(fmt_lib::format("create topic config failed"));
  47. }
  48. producer_.reset(RdKafka::Producer::create(conf_.get(), errstr));
  49. if (producer_ == nullptr) {
  50. throw_spdlog_ex(fmt_lib::format("create producer failed err:{}", errstr));
  51. }
  52. topic_.reset(RdKafka::Topic::create(producer_.get(), config_.produce_topic,
  53. tconf_.get(), errstr));
  54. if (topic_ == nullptr) {
  55. throw_spdlog_ex(fmt_lib::format("create topic failed err:{}", errstr));
  56. }
  57. } catch (const std::exception &e) {
  58. throw_spdlog_ex(fmt_lib::format("error create kafka instance: {}", e.what()));
  59. }
  60. }
  61. ~kafka_sink() { producer_->flush(config_.flush_timeout_ms); }
  62. protected:
  63. void sink_it_(const details::log_msg &msg) override {
  64. producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY,
  65. (void *)msg.payload.data(), msg.payload.size(), NULL, NULL);
  66. }
  67. void flush_() override { producer_->flush(config_.flush_timeout_ms); }
  68. private:
  69. kafka_sink_config config_;
  70. std::unique_ptr<RdKafka::Producer> producer_ = nullptr;
  71. std::unique_ptr<RdKafka::Conf> conf_ = nullptr;
  72. std::unique_ptr<RdKafka::Conf> tconf_ = nullptr;
  73. std::unique_ptr<RdKafka::Topic> topic_ = nullptr;
  74. };
  75. using kafka_sink_mt = kafka_sink<std::mutex>;
  76. using kafka_sink_st = kafka_sink<spdlog::details::null_mutex>;
  77. } // namespace sinks
  78. template <typename Factory = spdlog::synchronous_factory>
  79. inline std::shared_ptr<logger> kafka_logger_mt(const std::string &logger_name,
  80. spdlog::sinks::kafka_sink_config config) {
  81. return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
  82. }
  83. template <typename Factory = spdlog::synchronous_factory>
  84. inline std::shared_ptr<logger> kafka_logger_st(const std::string &logger_name,
  85. spdlog::sinks::kafka_sink_config config) {
  86. return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
  87. }
  88. template <typename Factory = spdlog::async_factory>
  89. inline std::shared_ptr<spdlog::logger> kafka_logger_async_mt(
  90. std::string logger_name, spdlog::sinks::kafka_sink_config config) {
  91. return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
  92. }
  93. template <typename Factory = spdlog::async_factory>
  94. inline std::shared_ptr<spdlog::logger> kafka_logger_async_st(
  95. std::string logger_name, spdlog::sinks::kafka_sink_config config) {
  96. return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
  97. }
  98. } // namespace spdlog