Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
zipkin.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <fc/log/logger.hpp>
4#include <string>
5#include <type_traits>
6
7namespace fc {
21
22class zipkin;
23
24class sha256;
25
27public:
29 static bool is_enabled() { return get_zipkin_() != nullptr; }
30
35 static void init( const std::string& url, const std::string& service_name, uint32_t timeout_us );
36
39 static zipkin& get_zipkin();
40
43 static void shutdown();
44
47
48private:
52 static zipkin* get_zipkin_() { return get().zip.get(); };
53
54 static zipkin_config& get();
55
56private:
57 std::unique_ptr<zipkin> zip;
58};
59
61 explicit zipkin_span( std::string name, uint64_t parent_id = 0 )
62 : data( std::move( name ), parent_id ) {}
63
64 explicit zipkin_span( uint64_t id, std::string name, uint64_t parent_id = 0 )
65 : data( id, std::move( name ), parent_id ) {}
66
67 zipkin_span( const zipkin_span& ) = delete;
68 zipkin_span& operator=( const zipkin_span& ) = delete;
70
71 zipkin_span( zipkin_span&& rhs ) noexcept
72 : data( std::move( rhs.data ) ) {
73 rhs.data.id = 0;
74 }
75
77
78 void add_tag( const std::string& key, const std::string& var ) {
79 // zipkin tags are required to be strings
80 data.tags( key, var );
81 }
82
83 void add_tag( const std::string& key, const char* var ) {
84 // zipkin tags are required to be strings
85 data.tags( key, var );
86 }
87
88 void add_tag( const std::string& key, bool v ) {
89 // zipkin tags are required to be strings
90 data.tags( key, v ? "true" : "false" );
91 }
92
93 template<typename T>
94 std::enable_if_t<std::is_arithmetic_v<std::remove_reference_t<T>>, void>
95 add_tag( const std::string& key, T&& var ) {
96 data.tags( key, std::to_string( std::forward<T>( var ) ) );
97 }
98
99 template<typename T>
100 std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>, void>
101 add_tag( const std::string& key, T&& var ) {
102 data.tags( key, (std::string) var );
103 }
104
105 struct token {
107 friend struct zipkin_trace;
108 friend struct optional_trace;
109 constexpr explicit operator bool() const noexcept { return id != 0; }
110 private:
111 explicit token( uint64_t id )
112 : id( id ) {}
113 uint64_t id;
114 };
115
116 token get_token() const { return token{data.id}; };
117
118 static uint64_t to_id( const fc::sha256& id );
119
120 template<typename T>
121 static uint64_t to_id( const T& id ) {
122 static_assert( std::is_same_v<decltype( id.data() ), const uint64_t*>, "expected uint64_t" );
123 return id.data()[3];
124 }
125
126 struct span_data {
127 explicit span_data( std::string name, uint64_t parent_id = 0 )
128 : id( zipkin_config::get_next_unique_id() ), parent_id( parent_id ),
129 start( time_point::now() ), name( std::move( name ) ) {}
130
131 explicit span_data( uint64_t id, std::string name, uint64_t parent_id = 0 )
132 : id( id ), parent_id( parent_id ), start( time_point::now() ), name( std::move( name ) ) {}
133
134 span_data( const span_data& ) = delete;
135 span_data& operator=( const span_data& ) = delete;
137 span_data( span_data&& rhs ) = default;
138
140 // zipkin traceId and parentId are same (when parent_id set) since only allowing trace with span children.
141 // Not currently supporting spans with children, only trace with children spans.
145 std::string name;
147 };
148
150};
151
152struct zipkin_trace : public zipkin_span {
154
155 [[nodiscard]] std::optional<zipkin_span> create_span( std::string name ) const {
156 return zipkin_span{std::move( name ), get_token().id};
157 }
158
159 [[nodiscard]] static std::optional<zipkin_span>
161 return zipkin_span{std::move( name ), token.id};
162 }
163};
164
166 std::optional<zipkin_trace> opt;
167
168 constexpr explicit operator bool() const noexcept { return opt.has_value(); }
169
170 [[nodiscard]] zipkin_span::token get_token() const {
171 return opt ? opt->get_token() : zipkin_span::token( 0 );
172 }
173};
174
175class zipkin {
176public:
177 zipkin( const std::string& url, const std::string& service_name, uint32_t timeout_us );
178
180 ~zipkin() = default;
181
184
185 // finish logging all queued up spans, not restartable
186 void shutdown();
187
188 // Logs zipkin json via http on separate thread
189 void log( zipkin_span::span_data&& span );
190
191private:
192 class impl;
193 std::unique_ptr<impl> my;
194};
195
196} // namespace fc
197
std::string name
An order-preserving dictionary of variants.
static void shutdown()
Definition zipkin.cpp:33
static uint64_t get_next_unique_id()
Starts with a random id and increments on each call, will not return 0.
Definition zipkin.cpp:39
static bool is_enabled()
Thread safe only if init() called from main thread before spawning of any threads.
Definition zipkin.hpp:29
static zipkin & get_zipkin()
Definition zipkin.cpp:26
void log(zipkin_span::span_data &&span)
Definition zipkin.cpp:158
uint64_t get_next_unique_id()
Starts with a random id and increments on each call, will not return 0.
Definition zipkin.cpp:107
~zipkin()=default
finishes logging all queued up spans
void shutdown()
Definition zipkin.cpp:117
zipkin(const std::string &url, const std::string &service_name, uint32_t timeout_us)
Definition zipkin.cpp:102
uint64_t id
Definition code_cache.cpp:0
void init()
Definition lib_test.cpp:3
namespace sysio::chain
Definition authority.cpp:3
Definition name.hpp:106
constexpr const char sha256[]
#define T(meth, val, expected)
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
zipkin_span::token get_token() const
Definition zipkin.hpp:170
std::optional< zipkin_trace > opt
Definition zipkin.hpp:166
fc::mutable_variant_object tags
Definition zipkin.hpp:146
span_data(const span_data &)=delete
span_data(span_data &&rhs)=default
const uint64_t parent_id
Definition zipkin.hpp:142
span_data & operator=(span_data &&)=delete
span_data(uint64_t id, std::string name, uint64_t parent_id=0)
Definition zipkin.hpp:131
span_data & operator=(const span_data &)=delete
span_data(std::string name, uint64_t parent_id=0)
Definition zipkin.hpp:127
const fc::time_point start
Definition zipkin.hpp:143
zipkin_span & operator=(zipkin_span &&)=delete
static uint64_t to_id(const fc::sha256 &id)
Definition zipkin.cpp:192
zipkin_span(uint64_t id, std::string name, uint64_t parent_id=0)
Definition zipkin.hpp:64
zipkin_span(zipkin_span &&rhs) noexcept
Definition zipkin.hpp:71
std::enable_if_t<!std::is_arithmetic_v< std::remove_reference_t< T > >, void > add_tag(const std::string &key, T &&var)
Definition zipkin.hpp:101
void add_tag(const std::string &key, const char *var)
Definition zipkin.hpp:83
void add_tag(const std::string &key, bool v)
Definition zipkin.hpp:88
static uint64_t to_id(const T &id)
Definition zipkin.hpp:121
span_data data
Definition zipkin.hpp:149
token get_token() const
Definition zipkin.hpp:116
zipkin_span & operator=(const zipkin_span &)=delete
std::enable_if_t< std::is_arithmetic_v< std::remove_reference_t< T > >, void > add_tag(const std::string &key, T &&var)
Definition zipkin.hpp:95
void add_tag(const std::string &key, const std::string &var)
Definition zipkin.hpp:78
zipkin_span(const zipkin_span &)=delete
zipkin_span(std::string name, uint64_t parent_id=0)
Definition zipkin.hpp:61
std::optional< zipkin_span > create_span(std::string name) const
Definition zipkin.hpp:155
static std::optional< zipkin_span > create_span_from_token(zipkin_span::token token, std::string name)
Definition zipkin.hpp:160