From dd2504efd659acc5f59580fbcf32f940926b3161 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Wed, 30 Apr 2025 13:12:33 +0200 Subject: [PATCH] Added more extensive test, fixed a few small things Signed-off-by: Otto Moerbeek --- pdns/recursordist/protozero-trace.cc | 14 ++--- pdns/recursordist/protozero-trace.hh | 14 +++-- pdns/recursordist/test-protozero-trace.cc | 62 ++++++++++++++++++++++- 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/pdns/recursordist/protozero-trace.cc b/pdns/recursordist/protozero-trace.cc index 054885e136..d391e9c82a 100644 --- a/pdns/recursordist/protozero-trace.cc +++ b/pdns/recursordist/protozero-trace.cc @@ -236,7 +236,7 @@ void Span::Event::encode(protozero::pbf_writer& writer) const pdns::trace::encodeFixed(writer, 1, time_unix_nano); pdns::trace::encode(writer, 2, name); pdns::trace::encode(writer, 3, attributes); - pdns::trace::encode(writer, 4, dropped_attribute_count); + pdns::trace::encode(writer, 4, dropped_attributes_count); } Span::Event Span::Event::decode(protozero::pbf_reader& reader) @@ -256,7 +256,7 @@ Span::Event Span::Event::decode(protozero::pbf_reader& reader) break; } case 4: - ret.dropped_attribute_count = reader.get_uint32(); + ret.dropped_attributes_count = reader.get_uint32(); default: break; } @@ -270,7 +270,7 @@ void Span::Link::encode(protozero::pbf_writer& writer) const pdns::trace::encode(writer, 2, span_id); pdns::trace::encode(writer, 3, trace_state); pdns::trace::encode(writer, 4, attributes); - pdns::trace::encode(writer, 5, dropped_attribute_count); + pdns::trace::encode(writer, 5, dropped_attributes_count); pdns::trace::encodeFixed(writer, 6, flags); } @@ -294,10 +294,10 @@ Span::Link Span::Link::decode(protozero::pbf_reader& reader) break; } case 5: - ret.dropped_attribute_count = reader.get_uint32(); + ret.dropped_attributes_count = reader.get_uint32(); break; case 6: - ret.flags = reader.get_uint32(); + ret.flags = reader.get_fixed32(); default: break; } @@ -316,7 +316,7 @@ void Span::encode(protozero::pbf_writer& writer) const pdns::trace::encodeFixed(writer, 7, start_time_unix_nano); pdns::trace::encodeFixed(writer, 8, end_time_unix_nano); pdns::trace::encode(writer, 9, attributes); - pdns::trace::encode(writer, 10, dropped_attribute_count); + pdns::trace::encode(writer, 10, dropped_attributes_count); pdns::trace::encode(writer, 11, events); pdns::trace::encode(writer, 12, dropped_events_count); pdns::trace::encode(writer, 13, links); @@ -362,7 +362,7 @@ Span Span::decode(protozero::pbf_reader& reader) break; } case 10: - ret.dropped_attribute_count = reader.get_uint32(); + ret.dropped_attributes_count = reader.get_uint32(); break; case 11: { auto sub = reader.get_message(); diff --git a/pdns/recursordist/protozero-trace.hh b/pdns/recursordist/protozero-trace.hh index 695635df4a..2bb168ebdd 100644 --- a/pdns/recursordist/protozero-trace.hh +++ b/pdns/recursordist/protozero-trace.hh @@ -282,13 +282,13 @@ struct Span uint64_t start_time_unix_nano{0}; // = 7 uint64_t end_time_unix_nano{0}; // = 8 std::vector attributes; // = 9 - uint32_t dropped_attribute_count{0}; // = 10 + uint32_t dropped_attributes_count{0}; // = 10 struct Event { uint64_t time_unix_nano; // = 1 std::string name; // = 2 std::vector attributes; // = 3 - uint32_t dropped_attribute_count{0}; // = 4 + uint32_t dropped_attributes_count{0}; // = 4 void encode(protozero::pbf_writer& writer) const; static Event decode(protozero::pbf_reader& reader); @@ -301,7 +301,7 @@ struct Span SpanID span_id; // = 2 std::string trace_state; // = 3 std::vector attributes; // = 4 - uint32_t dropped_attribute_count{0}; // = 5 + uint32_t dropped_attributes_count{0}; // = 5 uint32_t flags{0}; // = 6 void encode(protozero::pbf_writer& writer) const; @@ -315,6 +315,14 @@ struct Span static Span decode(protozero::pbf_reader& reader); }; +enum class SpanFlags : uint16_t +{ + SPAN_FLAGS_DO_NOT_USE = 0, + SPAN_FLAGS_TRACE_FLAGS_MASK = 0x000000FF, + SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK = 0x00000100, + SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK = 0x00000200 +}; + struct ScopeSpans { InstrumentationScope scope; // = 1 diff --git a/pdns/recursordist/test-protozero-trace.cc b/pdns/recursordist/test-protozero-trace.cc index 4b40374396..2346111b00 100644 --- a/pdns/recursordist/test-protozero-trace.cc +++ b/pdns/recursordist/test-protozero-trace.cc @@ -109,7 +109,7 @@ BOOST_AUTO_TEST_CASE(any) testAny(bytes); } -BOOST_AUTO_TEST_CASE(traces) +BOOST_AUTO_TEST_CASE(traces1) { pdns::trace::Span span = { .trace_id = {0x5B, 0x8E, 0xFF, 0xF7, 0x98, 0x03, 0x81, 0x03, 0xD2, 0x69, 0xB6, 0x33, 0x81, 0x3F, 0xC6, 0x0C}, @@ -157,4 +157,64 @@ BOOST_AUTO_TEST_CASE(traces) copy.encode(copyWriter); BOOST_CHECK_EQUAL(makeHexDump(data, " "), expected); } + +BOOST_AUTO_TEST_CASE(traces2) +{ + pdns::trace::Span span1{ + .trace_id = {0x5B, 0x8E, 0xFF, 0xF7, 0x98, 0x03, 0x81, 0x03, 0xD2, 0x69, 0xB6, 0x33, 0x81, 0x3F, 0xC6, 0x0C}, + .span_id = {0xEE, 0xE1, 0x9B, 0x7E, 0xC3, 0xC1, 0xB1, 0x74}, + .parent_span_id = {0xEE, 0xE1, 0x9B, 0x7E, 0xC3, 0xC1, 0xB1, 0x73}, + .name = "I'm a server span", + .kind = pdns::trace::Span::SpanKind::SPAN_KINSERVER, + .start_time_unix_nano = 1544712660000000000UL, + .end_time_unix_nano = 1544712661000000000UL, + .attributes = {{"my.span.attr", {"some value"}}, {"attr2", {1.0}}}, + .dropped_attributes_count = 1, + .events = { + {.time_unix_nano = 100, + .name = "event1", + .attributes = {{"a", {"b"}}, {"c", {"d"}}}, + .dropped_attributes_count = 101}, + {.time_unix_nano = 200, + .name = "event2", + .attributes = {{"c", {"d"}}, {"e", {""}}}, + .dropped_attributes_count = 201}, + }, + .dropped_events_count = 2, + .links = { + {.trace_id = {0x1}, .span_id = {0x4}, .trace_state = "state1", .attributes = {{"foo", {"bar"}}, {"bar", {true}}}, .dropped_attributes_count = 1000, .flags = static_cast(pdns::trace::SpanFlags::SPAN_FLAGS_TRACE_FLAGS_MASK)}, + {.trace_id = {0x2}, .span_id = {0x3}, .trace_state = "state2", .attributes = {{"foo", {"bar"}}, {"bar", {true}}}, .dropped_attributes_count = 1001, .flags = static_cast(pdns::trace::SpanFlags::SPAN_FLAGS_TRACE_FLAGS_MASK)}, + }, + .dropped_links_count = 3, + .status = {"hi", pdns::trace::Status::StatusCode::STATUS_CODE_OK}, + }; + pdns::trace::Span span2{span1}; + pdns::trace::Span span3{span1}; + pdns::trace::Span span4{span1}; + pdns::trace::InstrumentationScope scope{"name", "version", {{"key1", {0.1}}, {"key2", {false}}}, 100}; + pdns::trace::ScopeSpans scopespans1{scope, {span1, span2}, "url"}; + pdns::trace::ScopeSpans scopespans2{scope, {span3, span4}, "url"}; + pdns::trace::Resource res{.attributes = {{"key1", {"foo"}}, {"key2", {"bar"}}}, + .dropped_attributes_count = 99, + .entity_refs = {{"url1", "type1", {"id_key1", "id_key2"}, {"desc_key1", "desc_key2"}}, + {"url2", "type2", {"id_key3", "id_key4"}, {"desc_key3", "desc_key4"}}}}; + pdns::trace::ResourceSpans resspans1{res, {scopespans1, scopespans2}, "url"}; + pdns::trace::ResourceSpans resspans2{res, {scopespans1, scopespans2}, "url"}; + pdns::trace::TracesData traces{{resspans1, resspans2}}; + + std::string data; + protozero::pbf_writer writer{data}; + traces.encode(writer); +#if 1 + std::ofstream x("x"); + x << data; +#endif + protozero::pbf_reader reader{data}; + auto copy = pdns::trace::TracesData::decode(reader); + string copyData; + protozero::pbf_writer copyWriter{copyData}; + copy.encode(copyWriter); + BOOST_CHECK_EQUAL(data, copyData); +} + BOOST_AUTO_TEST_SUITE_END() -- 2.47.2