Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 47 additions & 36 deletions flow/Tracing.actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <functional>
#include <iomanip>
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -651,11 +620,42 @@ std::string readMPString(uint8_t* index) {
return reinterpret_cast<char*>(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<uint8_t[]>(kTraceBufferSize),
.data_size = 0,
Expand Down Expand Up @@ -716,7 +716,7 @@ TEST_CASE("/flow/Tracing/FastUDPMessagePackEncoding") {
auto s3Arena = span3.arena;
SmallVectorRef<KeyValueRef> 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);
Expand Down Expand Up @@ -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
16 changes: 12 additions & 4 deletions flow/Tracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ struct SpanEventRef {
};

class Span {
friend class AccessAttrsUnitTest;

public:
// Construct a Span with a given context, location, parentContext and optional links.
//
Expand Down Expand Up @@ -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) {
Expand All @@ -250,6 +249,15 @@ class Span {
SmallVectorRef<KeyValueRef> attributes; // not necessarily sorted
SmallVectorRef<SpanEventRef> 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.
Expand Down