kafka_sink.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/common.h>
  11. #include "spdlog/details/log_msg.h"
  12. #include "spdlog/sinks/base_sink.h"
  13. #include "spdlog/details/synchronous_factory.h"
  14. #include "spdlog/details/null_mutex.h"
  15. #include "spdlog/async.h"
  16. #include <mutex>
  17. // kafka header
  18. #include <librdkafka/rdkafkacpp.h>
  19. namespace spdlog {
  20. namespace sinks {
  21. struct kafka_sink_config
  22. {
  23. std::string server_addr;
  24. std::string produce_topic;
  25. int32_t flush_timeout_ms = 1000;
  26. kafka_sink_config(std::string addr, std::string topic, int flush_timeout_ms = 1000)
  27. : server_addr{std::move(addr)}
  28. , produce_topic{std::move(topic)}
  29. , flush_timeout_ms(flush_timeout_ms)
  30. {}
  31. };
  32. template<typename Mutex>
  33. class kafka_sink : public base_sink<Mutex>
  34. {
  35. public:
  36. kafka_sink(kafka_sink_config config)
  37. : config_{std::move(config)}
  38. {
  39. try
  40. {
  41. std::string errstr;
  42. conf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL));
  43. RdKafka::Conf::ConfResult confRes = conf_->set("bootstrap.servers", config_.server_addr, errstr);
  44. if (confRes != RdKafka::Conf::CONF_OK)
  45. {
  46. throw_spdlog_ex(fmt_lib::format("conf set bootstrap.servers failed err:{}", errstr));
  47. }
  48. tconf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC));
  49. if (tconf_ == nullptr)
  50. {
  51. throw_spdlog_ex(fmt_lib::format("create topic config failed"));
  52. }
  53. producer_.reset(RdKafka::Producer::create(conf_.get(), errstr));
  54. if (producer_ == nullptr)
  55. {
  56. throw_spdlog_ex(fmt_lib::format("create producer failed err:{}", errstr));
  57. }
  58. topic_.reset(RdKafka::Topic::create(producer_.get(), config_.produce_topic, tconf_.get(), errstr));
  59. if (topic_ == nullptr)
  60. {
  61. throw_spdlog_ex(fmt_lib::format("create topic failed err:{}", errstr));
  62. }
  63. }
  64. catch (const std::exception &e)
  65. {
  66. throw_spdlog_ex(fmt_lib::format("error create kafka instance: {}", e.what()));
  67. }
  68. }
  69. ~kafka_sink()
  70. {
  71. producer_->flush(config_.flush_timeout_ms);
  72. }
  73. protected:
  74. void sink_it_(const details::log_msg &msg) override
  75. {
  76. producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, (void *)msg.payload.data(), msg.payload.size(), NULL, NULL);
  77. }
  78. void flush_() override
  79. {
  80. producer_->flush(config_.flush_timeout_ms);
  81. }
  82. private:
  83. kafka_sink_config config_;
  84. std::unique_ptr<RdKafka::Producer> producer_ = nullptr;
  85. std::unique_ptr<RdKafka::Conf> conf_ = nullptr;
  86. std::unique_ptr<RdKafka::Conf> tconf_ = nullptr;
  87. std::unique_ptr<RdKafka::Topic> topic_ = nullptr;
  88. };
  89. using kafka_sink_mt = kafka_sink<std::mutex>;
  90. using kafka_sink_st = kafka_sink<spdlog::details::null_mutex>;
  91. } // namespace sinks
  92. template<typename Factory = spdlog::synchronous_factory>
  93. inline std::shared_ptr<logger> kafka_logger_mt(const std::string &logger_name, spdlog::sinks::kafka_sink_config config)
  94. {
  95. return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
  96. }
  97. template<typename Factory = spdlog::synchronous_factory>
  98. inline std::shared_ptr<logger> kafka_logger_st(const std::string &logger_name, spdlog::sinks::kafka_sink_config config)
  99. {
  100. return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
  101. }
  102. template<typename Factory = spdlog::async_factory>
  103. inline std::shared_ptr<spdlog::logger> kafka_logger_async_mt(std::string logger_name, spdlog::sinks::kafka_sink_config config)
  104. {
  105. return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
  106. }
  107. template<typename Factory = spdlog::async_factory>
  108. inline std::shared_ptr<spdlog::logger> kafka_logger_async_st(std::string logger_name, spdlog::sinks::kafka_sink_config config)
  109. {
  110. return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
  111. }
  112. } // namespace spdlog