10#include <boost/asio.hpp>
17zipkin_config& zipkin_config::get() {
18 static zipkin_config the_one;
23 get().zip = std::make_unique<zipkin>(
url, service_name, timeout_us );
34 if(
zipkin* z = get_zipkin_() ) {
60 boost::asio::io_context
ctx;
62 boost::asio::executor_work_guard<boost::asio::io_context::executor_type>
work_guard = boost::asio::make_work_guard(
ctx);
79 thread = std::thread( [
this]() {
97 if( stopped ^= 1 )
return;
103 my( new
impl(
url, service_name, timeout_us ) ) {
108 std::scoped_lock g( my->mtx );
109 if( my->next_id == 0 ) {
110 std::mt19937_64 engine( std::random_device{}() );
111 std::uniform_int_distribution<uint64_t> distribution(1);
112 my->next_id = distribution( engine );
114 return my->next_id++;
131 if( span.parent_id != 0 ) {
132 trace_id = span.parent_id;
138 mvo(
"id",
fc::to_hex(
reinterpret_cast<const char*
>(&span.id),
sizeof( span.id ) ) );
139 mvo(
"traceId",
fc::to_hex(
reinterpret_cast<const char*
>(&trace_id),
sizeof( trace_id ) ) );
140 if( span.parent_id != 0 ) {
141 mvo(
"parentId",
fc::to_hex(
reinterpret_cast<const char*
>(&span.parent_id),
sizeof( span.parent_id ) ) );
143 mvo(
"name", std::move( span.name ) );
144 mvo(
"timestamp", span.start.time_since_epoch().count() );
145 mvo(
"duration", (span.stop - span.start).count() );
148 mvo(
"tags", std::move( span.tags ) );
153 result.emplace_back( std::move( mvo ) );
159 if( my->consecutive_errors > my->max_consecutive_errors || my->stopped )
162 boost::asio::post(my->work_strand, [
this, span{std::move(span)}]()
mutable {
163 my->log( std::move( span ) );
184 }
catch(
const std::exception& e ) {
185 wlog(
"unable to connect to zipkin: ${u}, error: ${e}", (
"u",
zipkin_url)(
"e", e.
what()) );
187 wlog(
"unable to connect to zipkin: ${u}, error: unknown", (
"u",
zipkin_url) );
194 return id._hash[3] == 0 ? 1 :
id._hash[3];
Used to generate a useful error report when an exception is thrown.
std::string to_detail_string(log_level ll=log_level::all) const
const char * what() const noexcept override
An order-preserving dictionary of variants.
An order-preserving dictionary of variants.
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
std::optional< url > endpoint
const uint32_t timeout_us
void log(zipkin_span::span_data &&span)
boost::asio::io_context ctx
impl(std::string url, std::string service_name, uint32_t timeout_us)
const std::string service_name
std::atomic< unsigned char > stopped
static constexpr uint32_t max_consecutive_errors
std::atomic< uint32_t > consecutive_errors
boost::asio::executor_work_guard< boost::asio::io_context::executor_type > work_guard
const std::string zipkin_url
boost::asio::io_context::strand work_strand
static uint64_t get_next_unique_id()
Starts with a random id and increments on each call, will not return 0.
static bool is_enabled()
Thread safe only if init() called from main thread before spawning of any threads.
static zipkin & get_zipkin()
static void init(const std::string &url, const std::string &service_name, uint32_t timeout_us)
void log(zipkin_span::span_data &&span)
uint64_t get_next_unique_id()
Starts with a random id and increments on each call, will not return 0.
zipkin(const std::string &url, const std::string &service_name, uint32_t timeout_us)
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_LOG_AND_DROP(...)
fc::variant create_zipkin_variant(zipkin_span::span_data &&span, const std::string &service_name)
std::vector< fc::variant > variants
fc::string to_hex(const char *d, uint32_t s)
void set_os_thread_name(const string &name)
unsigned __int64 uint64_t
static uint64_t to_id(const fc::sha256 &id)