Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::protobuf_wire Namespace Reference

Classes

class  reader
 Minimal protobuf wire reader used for round-trip tests. More...
 

Enumerations

enum class  wire_type : std::uint8_t { varint = 0 , fixed64 = 1 , length_delimited = 2 , fixed32 = 5 }
 

Functions

void encode_varint (std::vector< std::uint8_t > &out, std::uint64_t value)
 Encode an unsigned varint into the buffer.
 
void encode_tag (std::vector< std::uint8_t > &out, std::uint32_t field_number, wire_type wt)
 Encode a tag (field_number << 3 | wire_type) as a varint.
 
void encode_fixed64 (std::vector< std::uint8_t > &out, std::uint64_t value)
 Encode a fixed64 little-endian value (used for Zipkin timestamps).
 
void encode_length_delimited (std::vector< std::uint8_t > &out, const std::uint8_t *data, std::size_t size)
 Encode a length-delimited byte sequence.
 
void encode_string_field (std::vector< std::uint8_t > &out, std::uint32_t field_number, const std::string &value)
 Encode a string field.
 
void encode_bytes_field (std::vector< std::uint8_t > &out, std::uint32_t field_number, const std::vector< std::uint8_t > &value)
 Encode a bytes field.
 
void encode_uint64_field (std::vector< std::uint8_t > &out, std::uint32_t field_number, std::uint64_t value)
 Encode a uint32 / uint64 / int64 varint field (skips zero).
 
void encode_uint64_field_always (std::vector< std::uint8_t > &out, std::uint32_t field_number, std::uint64_t value)
 Encode a uint32 field that is not allowed to be skipped even if zero.
 
void encode_enum_field (std::vector< std::uint8_t > &out, std::uint32_t field_number, std::int32_t value)
 Encode an enum field (always written when nonzero).
 
void encode_bool_field (std::vector< std::uint8_t > &out, std::uint32_t field_number, bool value)
 Encode a bool field (proto3 skips false).
 
void encode_fixed64_field (std::vector< std::uint8_t > &out, std::uint32_t field_number, std::uint64_t value)
 Encode a fixed64 field.
 
void encode_message_field (std::vector< std::uint8_t > &out, std::uint32_t field_number, const std::vector< std::uint8_t > &serialized)
 Encode an embedded message field given its pre-serialized bytes.
 
bool decode_tag (reader &r, std::uint32_t &field_number, wire_type &wt)
 Decode a tag into (field_number, wire_type).
 
std::vector< std::uint8_t > hex_to_bytes (const std::string &hex)
 Decode a hexadecimal string into bytes. Odd-length strings are zero-padded on the left; non-hex characters yield an empty vector.
 
std::string bytes_to_hex (const std::vector< std::uint8_t > &bytes)
 Encode raw bytes as a lowercase hex string.
 
std::vector< std::uint8_t > left_pad (const std::vector< std::uint8_t > &in, std::size_t width)
 Left-pad bytes to a target width. Used to normalize 8-byte trace IDs to Jaeger's 16-byte on-wire width and similar cases.
 

Enumeration Type Documentation

◆ wire_type

enum class kcenon::monitoring::protobuf_wire::wire_type : std::uint8_t
strong
Enumerator
varint 

int32/int64/uint32/uint64/sint*‍/bool/enum

fixed64 

fixed64/sfixed64/double

length_delimited 

string/bytes/embedded messages/packed repeated

fixed32 

fixed32/sfixed32/float

Definition at line 29 of file protobuf_wire.h.

29 : std::uint8_t {
30 varint = 0,
31 fixed64 = 1,
33 fixed32 = 5
34};
@ varint
int32/int64/uint32/uint64/sint*‍/bool/enum
@ length_delimited
string/bytes/embedded messages/packed repeated

Function Documentation

◆ bytes_to_hex()

std::string kcenon::monitoring::protobuf_wire::bytes_to_hex ( const std::vector< std::uint8_t > & bytes)
inline

Encode raw bytes as a lowercase hex string.

Definition at line 321 of file protobuf_wire.h.

321 {
322 static const char hex_chars[] = "0123456789abcdef";
323 std::string out;
324 out.reserve(bytes.size() * 2);
325 for (std::uint8_t b : bytes) {
326 out.push_back(hex_chars[(b >> 4) & 0x0F]);
327 out.push_back(hex_chars[b & 0x0F]);
328 }
329 return out;
330}

◆ decode_tag()

bool kcenon::monitoring::protobuf_wire::decode_tag ( reader & r,
std::uint32_t & field_number,
wire_type & wt )
inline

◆ encode_bool_field()

void kcenon::monitoring::protobuf_wire::encode_bool_field ( std::vector< std::uint8_t > & out,
std::uint32_t field_number,
bool value )
inline

Encode a bool field (proto3 skips false).

Definition at line 133 of file protobuf_wire.h.

135 {
136 if (!value) {
137 return;
138 }
139 encode_tag(out, field_number, wire_type::varint);
140 encode_varint(out, 1);
141}

References encode_tag(), encode_varint(), and varint.

Referenced by kcenon::monitoring::jaeger_proto::encode_key_value(), and kcenon::monitoring::zipkin_proto::encode_span().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_bytes_field()

void kcenon::monitoring::protobuf_wire::encode_bytes_field ( std::vector< std::uint8_t > & out,
std::uint32_t field_number,
const std::vector< std::uint8_t > & value )
inline

Encode a bytes field.

Definition at line 92 of file protobuf_wire.h.

94 {
95 if (value.empty()) {
96 return; // proto3 defaults: skip empty bytes
97 }
98 encode_tag(out, field_number, wire_type::length_delimited);
99 encode_length_delimited(out, value.data(), value.size());
100}

References encode_length_delimited(), encode_tag(), and length_delimited.

Referenced by kcenon::monitoring::zipkin_proto::encode_endpoint(), kcenon::monitoring::jaeger_proto::encode_key_value(), kcenon::monitoring::jaeger_proto::encode_span(), kcenon::monitoring::zipkin_proto::encode_span(), and kcenon::monitoring::jaeger_proto::encode_span_ref().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_enum_field()

void kcenon::monitoring::protobuf_wire::encode_enum_field ( std::vector< std::uint8_t > & out,
std::uint32_t field_number,
std::int32_t value )
inline

Encode an enum field (always written when nonzero).

Definition at line 122 of file protobuf_wire.h.

124 {
125 if (value == 0) {
126 return;
127 }
128 encode_tag(out, field_number, wire_type::varint);
129 encode_varint(out, static_cast<std::uint64_t>(value));
130}

References encode_tag(), encode_varint(), and varint.

Referenced by kcenon::monitoring::jaeger_proto::encode_key_value(), kcenon::monitoring::zipkin_proto::encode_span(), and kcenon::monitoring::jaeger_proto::encode_span_ref().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_fixed64()

void kcenon::monitoring::protobuf_wire::encode_fixed64 ( std::vector< std::uint8_t > & out,
std::uint64_t value )
inline

Encode a fixed64 little-endian value (used for Zipkin timestamps).

Definition at line 61 of file protobuf_wire.h.

61 {
62 for (int i = 0; i < 8; ++i) {
63 out.push_back(static_cast<std::uint8_t>((value >> (i * 8)) & 0xFF));
64 }
65}

Referenced by kcenon::monitoring::zipkin_proto::encode_annotation(), encode_fixed64_field(), and kcenon::monitoring::jaeger_proto::encode_key_value().

Here is the caller graph for this function:

◆ encode_fixed64_field()

void kcenon::monitoring::protobuf_wire::encode_fixed64_field ( std::vector< std::uint8_t > & out,
std::uint32_t field_number,
std::uint64_t value )
inline

Encode a fixed64 field.

Definition at line 144 of file protobuf_wire.h.

146 {
147 if (value == 0) {
148 return;
149 }
150 encode_tag(out, field_number, wire_type::fixed64);
151 encode_fixed64(out, value);
152}

References encode_fixed64(), encode_tag(), and fixed64.

Referenced by kcenon::monitoring::zipkin_proto::encode_span().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_length_delimited()

void kcenon::monitoring::protobuf_wire::encode_length_delimited ( std::vector< std::uint8_t > & out,
const std::uint8_t * data,
std::size_t size )
inline

Encode a length-delimited byte sequence.

Definition at line 70 of file protobuf_wire.h.

72 {
73 encode_varint(out, static_cast<std::uint64_t>(size));
74 out.insert(out.end(), data, data + size);
75}
void encode_varint(std::vector< std::uint8_t > &out, std::uint64_t value)
Encode an unsigned varint into the buffer.

References encode_varint().

Referenced by encode_bytes_field(), encode_message_field(), and encode_string_field().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_message_field()

void kcenon::monitoring::protobuf_wire::encode_message_field ( std::vector< std::uint8_t > & out,
std::uint32_t field_number,
const std::vector< std::uint8_t > & serialized )
inline

Encode an embedded message field given its pre-serialized bytes.

Definition at line 155 of file protobuf_wire.h.

157 {
158 encode_tag(out, field_number, wire_type::length_delimited);
159 encode_length_delimited(out, serialized.data(), serialized.size());
160}
void encode_tag(std::vector< std::uint8_t > &out, std::uint32_t field_number, wire_type wt)
Encode a tag (field_number << 3 | wire_type) as a varint.
void encode_length_delimited(std::vector< std::uint8_t > &out, const std::uint8_t *data, std::size_t size)
Encode a length-delimited byte sequence.

References encode_length_delimited(), encode_tag(), and length_delimited.

Referenced by kcenon::monitoring::jaeger_proto::encode_batch(), kcenon::monitoring::zipkin_proto::encode_list_of_spans(), kcenon::monitoring::jaeger_proto::encode_process(), kcenon::monitoring::jaeger_proto::encode_span(), and kcenon::monitoring::zipkin_proto::encode_span().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_string_field()

void kcenon::monitoring::protobuf_wire::encode_string_field ( std::vector< std::uint8_t > & out,
std::uint32_t field_number,
const std::string & value )
inline

Encode a string field.

Definition at line 78 of file protobuf_wire.h.

80 {
81 if (value.empty()) {
82 return; // proto3 defaults: skip empty string
83 }
84 encode_tag(out, field_number, wire_type::length_delimited);
85 encode_length_delimited(
86 out,
87 reinterpret_cast<const std::uint8_t*>(value.data()),
88 value.size());
89}

References encode_length_delimited(), encode_tag(), and length_delimited.

Referenced by kcenon::monitoring::zipkin_proto::encode_annotation(), kcenon::monitoring::zipkin_proto::encode_endpoint(), kcenon::monitoring::jaeger_proto::encode_key_value(), kcenon::monitoring::jaeger_proto::encode_process(), kcenon::monitoring::jaeger_proto::encode_span(), kcenon::monitoring::zipkin_proto::encode_span(), and kcenon::monitoring::zipkin_proto::encode_string_map_entry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_tag()

void kcenon::monitoring::protobuf_wire::encode_tag ( std::vector< std::uint8_t > & out,
std::uint32_t field_number,
wire_type wt )
inline

Encode a tag (field_number << 3 | wire_type) as a varint.

Definition at line 50 of file protobuf_wire.h.

52 {
53 encode_varint(out,
54 (static_cast<std::uint64_t>(field_number) << 3) |
55 static_cast<std::uint64_t>(wt));
56}

References encode_varint().

Referenced by kcenon::monitoring::zipkin_proto::encode_annotation(), encode_bool_field(), encode_bytes_field(), encode_enum_field(), encode_fixed64_field(), kcenon::monitoring::jaeger_proto::encode_key_value(), encode_message_field(), encode_string_field(), encode_uint64_field(), and encode_uint64_field_always().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_uint64_field()

void kcenon::monitoring::protobuf_wire::encode_uint64_field ( std::vector< std::uint8_t > & out,
std::uint32_t field_number,
std::uint64_t value )
inline

Encode a uint32 / uint64 / int64 varint field (skips zero).

Definition at line 103 of file protobuf_wire.h.

105 {
106 if (value == 0) {
107 return;
108 }
109 encode_tag(out, field_number, wire_type::varint);
110 encode_varint(out, value);
111}

References encode_tag(), encode_varint(), and varint.

Referenced by kcenon::monitoring::jaeger_proto::encode_duration(), kcenon::monitoring::zipkin_proto::encode_endpoint(), kcenon::monitoring::jaeger_proto::encode_span(), kcenon::monitoring::zipkin_proto::encode_span(), and kcenon::monitoring::jaeger_proto::encode_timestamp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_uint64_field_always()

void kcenon::monitoring::protobuf_wire::encode_uint64_field_always ( std::vector< std::uint8_t > & out,
std::uint32_t field_number,
std::uint64_t value )
inline

Encode a uint32 field that is not allowed to be skipped even if zero.

Definition at line 114 of file protobuf_wire.h.

116 {
117 encode_tag(out, field_number, wire_type::varint);
118 encode_varint(out, value);
119}

References encode_tag(), encode_varint(), and varint.

Here is the call graph for this function:

◆ encode_varint()

void kcenon::monitoring::protobuf_wire::encode_varint ( std::vector< std::uint8_t > & out,
std::uint64_t value )
inline

Encode an unsigned varint into the buffer.

Definition at line 39 of file protobuf_wire.h.

39 {
40 while (value >= 0x80) {
41 out.push_back(static_cast<std::uint8_t>((value & 0x7F) | 0x80));
42 value >>= 7;
43 }
44 out.push_back(static_cast<std::uint8_t>(value));
45}

Referenced by encode_bool_field(), encode_enum_field(), kcenon::monitoring::jaeger_proto::encode_key_value(), encode_length_delimited(), encode_tag(), encode_uint64_field(), and encode_uint64_field_always().

Here is the caller graph for this function:

◆ hex_to_bytes()

std::vector< std::uint8_t > kcenon::monitoring::protobuf_wire::hex_to_bytes ( const std::string & hex)
inline

Decode a hexadecimal string into bytes. Odd-length strings are zero-padded on the left; non-hex characters yield an empty vector.

Definition at line 294 of file protobuf_wire.h.

294 {
295 auto nibble = [](char c) -> int {
296 if (c >= '0' && c <= '9') return c - '0';
297 if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
298 if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
299 return -1;
300 };
301 std::string s = hex;
302 if (s.size() % 2 == 1) {
303 s.insert(s.begin(), '0');
304 }
305 std::vector<std::uint8_t> out;
306 out.reserve(s.size() / 2);
307 for (std::size_t i = 0; i < s.size(); i += 2) {
308 int hi = nibble(s[i]);
309 int lo = nibble(s[i + 1]);
310 if (hi < 0 || lo < 0) {
311 return {}; // invalid; caller treats empty as absent
312 }
313 out.push_back(static_cast<std::uint8_t>((hi << 4) | lo));
314 }
315 return out;
316}

Referenced by kcenon::monitoring::encode_jaeger_batch(), kcenon::monitoring::encode_zipkin_list_of_spans(), kcenon::monitoring::jaeger_span_data::to_protobuf(), and kcenon::monitoring::zipkin_span_data::to_protobuf().

Here is the caller graph for this function:

◆ left_pad()

std::vector< std::uint8_t > kcenon::monitoring::protobuf_wire::left_pad ( const std::vector< std::uint8_t > & in,
std::size_t width )
inline

Left-pad bytes to a target width. Used to normalize 8-byte trace IDs to Jaeger's 16-byte on-wire width and similar cases.

Definition at line 336 of file protobuf_wire.h.

337 {
338 if (in.size() >= width) return in;
339 std::vector<std::uint8_t> out(width, 0);
340 std::memcpy(out.data() + (width - in.size()), in.data(), in.size());
341 return out;
342}

Referenced by kcenon::monitoring::encode_jaeger_batch(), kcenon::monitoring::encode_zipkin_list_of_spans(), kcenon::monitoring::jaeger_span_data::to_protobuf(), and kcenon::monitoring::zipkin_span_data::to_protobuf().

Here is the caller graph for this function: