diff --git a/flow/Tracing.actor.cpp b/flow/Tracing.actor.cpp index a709cfadfe1..b84f1da381b 100644 --- a/flow/Tracing.actor.cpp +++ b/flow/Tracing.actor.cpp @@ -22,7 +22,6 @@ #include "flow/IRandom.h" #include "flow/UnitTest.h" #include "flow/Knobs.h" -#include "fdbclient/IKnobCollection.h" #include "flow/network.h" #include #include @@ -529,36 +528,6 @@ TEST_CASE("/flow/Tracing/AddEvents") { return Void(); }; -TEST_CASE("/flow/Tracing/AddAttributes") { - Span span1("span_with_attrs"_loc, - SpanContext(deterministicRandom()->randomUniqueID(), - deterministicRandom()->randomUInt64(), - TraceFlags::sampled)); - IKnobCollection::getMutableGlobalKnobCollection().setKnob("tracing_span_attributes_enabled", - KnobValueRef::create(bool{ true })); - auto arena = span1.arena; - span1.addAttribute(StringRef(arena, LiteralStringRef("foo")), StringRef(arena, LiteralStringRef("bar"))); - span1.addAttribute(StringRef(arena, LiteralStringRef("operation")), StringRef(arena, LiteralStringRef("grv"))); - ASSERT_EQ(span1.attributes.size(), 3); // Includes default attribute of "address" - ASSERT(span1.attributes[1] == KeyValueRef("foo"_sr, "bar"_sr)); - ASSERT(span1.attributes[2] == KeyValueRef("operation"_sr, "grv"_sr)); - - Span span2("span_with_attrs"_loc, - SpanContext(deterministicRandom()->randomUniqueID(), - deterministicRandom()->randomUInt64(), - TraceFlags::sampled)); - auto s2Arena = span2.arena; - span2.addAttribute(StringRef(s2Arena, LiteralStringRef("a")), StringRef(s2Arena, LiteralStringRef("1"))) - .addAttribute(StringRef(s2Arena, LiteralStringRef("b")), LiteralStringRef("2")) - .addAttribute(StringRef(s2Arena, LiteralStringRef("c")), LiteralStringRef("3")); - - ASSERT_EQ(span2.attributes.size(), 4); // Includes default attribute of "address" - ASSERT(span2.attributes[1] == KeyValueRef("a"_sr, "1"_sr)); - ASSERT(span2.attributes[2] == KeyValueRef("b"_sr, "2"_sr)); - ASSERT(span2.attributes[3] == KeyValueRef("c"_sr, "3"_sr)); - return Void(); -}; - TEST_CASE("/flow/Tracing/AddLinks") { Span span1("span_with_links"_loc); ASSERT(!span1.context.isSampled()); @@ -651,11 +620,42 @@ std::string readMPString(uint8_t* index) { return reinterpret_cast(data); } +class AccessAttrsUnitTest { +public: + void RunAddAttributes(); + void RunFastUDPMessagePackEncoding(); +}; +void AccessAttrsUnitTest::RunAddAttributes() { + Span span1("span_with_attrs"_loc, + SpanContext(deterministicRandom()->randomUniqueID(), + deterministicRandom()->randomUInt64(), + TraceFlags::sampled)); + auto arena = span1.arena; + span1.addAttribute(StringRef(arena, LiteralStringRef("foo")), StringRef(arena, LiteralStringRef("bar")), true); + span1.addAttribute( + StringRef(arena, LiteralStringRef("operation")), StringRef(arena, LiteralStringRef("grv")), true); + ASSERT_EQ(span1.attributes.size(), 3); // Includes default attribute of "address" + ASSERT(span1.attributes[1] == KeyValueRef("foo"_sr, "bar"_sr)); + ASSERT(span1.attributes[2] == KeyValueRef("operation"_sr, "grv"_sr)); + + Span span2("span_with_attrs"_loc, + SpanContext(deterministicRandom()->randomUniqueID(), + deterministicRandom()->randomUInt64(), + TraceFlags::sampled)); + auto s2Arena = span2.arena; + span2.addAttribute(StringRef(s2Arena, LiteralStringRef("a")), StringRef(s2Arena, LiteralStringRef("1")), true) + .addAttribute(StringRef(s2Arena, LiteralStringRef("b")), LiteralStringRef("2"), true) + .addAttribute(StringRef(s2Arena, LiteralStringRef("c")), LiteralStringRef("3"), true); + + ASSERT_EQ(span2.attributes.size(), 4); // Includes default attribute of "address" + ASSERT(span2.attributes[1] == KeyValueRef("a"_sr, "1"_sr)); + ASSERT(span2.attributes[2] == KeyValueRef("b"_sr, "2"_sr)); + ASSERT(span2.attributes[3] == KeyValueRef("c"_sr, "3"_sr)); +} + +void AccessAttrsUnitTest::RunFastUDPMessagePackEncoding() { // Windows doesn't like lack of header and declaration of constructor for FastUDPTracer #ifndef WIN32 -TEST_CASE("/flow/Tracing/FastUDPMessagePackEncoding") { - IKnobCollection::getMutableGlobalKnobCollection().setKnob("tracing_span_attributes_enabled", - KnobValueRef::create(bool{ true })); Span span1("encoded_span"_loc); auto request = TraceRequest{ .buffer = std::make_unique(kTraceBufferSize), .data_size = 0, @@ -716,7 +716,7 @@ TEST_CASE("/flow/Tracing/FastUDPMessagePackEncoding") { auto s3Arena = span3.arena; SmallVectorRef attrs; attrs.push_back(s3Arena, KeyValueRef("foo"_sr, "bar"_sr)); - span3.addAttribute("operation"_sr, "grv"_sr) + span3.addAttribute("operation"_sr, "grv"_sr, true) .addLink(SpanContext(UID(300, 301), 400, TraceFlags::sampled)) .addEvent(StringRef(s3Arena, LiteralStringRef("event1")), 100.101, attrs); tracer.serialize_span(span3, request); @@ -792,6 +792,17 @@ TEST_CASE("/flow/Tracing/FastUDPMessagePackEncoding") { // Read and verify span name ASSERT(data[37] == 0xda); ASSERT(readMPString(&data[37]) == longString); +#endif +}; + +TEST_CASE("/flow/Tracing/AddAttributes") { + AccessAttrsUnitTest test; + test.RunAddAttributes(); + return Void(); +}; + +TEST_CASE("/flow/Tracing/FastUDPMessagePackEncoding") { + AccessAttrsUnitTest test; + test.RunFastUDPMessagePackEncoding(); return Void(); }; -#endif diff --git a/flow/Tracing.h b/flow/Tracing.h index 30c2161b1a1..c33cdc52451 100644 --- a/flow/Tracing.h +++ b/flow/Tracing.h @@ -114,6 +114,8 @@ struct SpanEventRef { }; class Span { + friend class AccessAttrsUnitTest; + public: // Construct a Span with a given context, location, parentContext and optional links. // @@ -226,10 +228,7 @@ class Span { } Span& addAttribute(const StringRef& key, const StringRef& value) { - if (FLOW_KNOBS->TRACING_SPAN_ATTRIBUTES_ENABLED) { - attributes.push_back_deep(arena, KeyValueRef(key, value)); - } - return *this; + return addAttribute(key, value, FLOW_KNOBS->TRACING_SPAN_ATTRIBUTES_ENABLED); } Span& setParent(const SpanContext& parent) { @@ -250,6 +249,15 @@ class Span { SmallVectorRef attributes; // not necessarily sorted SmallVectorRef events; SpanStatus status; + +private: + // For unit testing only. + Span& addAttribute(const StringRef& key, const StringRef& value, bool tracingAttrsEnabled) { + if (tracingAttrsEnabled) { + attributes.push_back_deep(arena, KeyValueRef(key, value)); + } + return *this; + } }; // The user selects a tracer using a string passed to fdbserver on boot.