Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Scope object instead of unique_ptr<Scope> from Tracer::WithActiveSpan() #788

Merged
merged 17 commits into from
May 25, 2021
Merged
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
5 changes: 1 addition & 4 deletions api/include/opentelemetry/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ class Tracer
* @param span the span that should be set as the new active span.
* @return a Scope that controls how long the span will be active.
*/
static nostd::unique_ptr<Scope> WithActiveSpan(nostd::shared_ptr<Span> &span) noexcept
{
return nostd::unique_ptr<Scope>(new Scope{span});
}
static Scope WithActiveSpan(nostd::shared_ptr<Span> &span) noexcept { return Scope{span}; }

/**
* Get the currently active span.
Expand Down
25 changes: 13 additions & 12 deletions api/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ TEST(TracerTest, GetCurrentSpan)
auto current = tracer->GetCurrentSpan();
ASSERT_FALSE(current->GetContext().IsValid());

auto scope_first = tracer->WithActiveSpan(span_first);
current = tracer->GetCurrentSpan();
ASSERT_EQ(current, span_first);
{
auto scope_first = tracer->WithActiveSpan(span_first);
current = tracer->GetCurrentSpan();
ASSERT_EQ(current, span_first);

{
auto scope_second = tracer->WithActiveSpan(span_second);
current = tracer->GetCurrentSpan();
ASSERT_EQ(current, span_second);
}
current = tracer->GetCurrentSpan();
ASSERT_EQ(current, span_first);
}

auto scope_second = tracer->WithActiveSpan(span_second);
current = tracer->GetCurrentSpan();
ASSERT_EQ(current, span_second);

scope_second.reset(nullptr);
current = tracer->GetCurrentSpan();
ASSERT_EQ(current, span_first);

scope_first.reset(nullptr);
current = tracer->GetCurrentSpan();
ASSERT_FALSE(current->GetContext().IsValid());
}
16 changes: 0 additions & 16 deletions exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,22 +477,12 @@ class Tracer : public trace::Tracer
UpdateStatus(currentSpan, evt);
etwProvider().write(provHandle, evt, ActivityIdPtr, RelatedActivityIdPtr, 0, encoding);
}

{
// Atomically remove the span from list of spans
const std::lock_guard<std::mutex> lock(scopes_mutex_);
auto spanId = ToLowerBase16(spanBase.GetContext().span_id());
scopes_.erase(spanId);
}
};

const trace::TraceId &trace_id() { return traceId_; };

friend class Span;

std::mutex scopes_mutex_; // protects scopes_
std::map<std::string, nostd::unique_ptr<trace::Scope>> scopes_;

/**
* @brief Init a reference to etw::ProviderHandle
* @return Provider Handle
Expand Down Expand Up @@ -648,12 +638,6 @@ class Tracer : public trace::Tracer
etwProvider().write(provHandle, evt, ActivityIdPtr, RelatedActivityIdPtr, 1, encoding);
};

{
const std::lock_guard<std::mutex> lock(scopes_mutex_);
// Use span_id as index
scopes_[ToLowerBase16(result->GetContext().span_id())] = WithActiveSpan(result);
}

return result;
};

Expand Down
144 changes: 86 additions & 58 deletions exporters/etw/test/etw_tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,55 +57,62 @@ TEST(ETWTracer, TracerCheck)
{"attrib1", 1},
{"attrib2", 2}
};
{
auto topSpan = tracer->StartSpan("MySpanTop");
auto topScope = tracer->WithActiveSpan(topSpan);
std::this_thread::sleep_for (std::chrono::seconds(1));
{
auto outerSpan = tracer->StartSpan("MySpanL2", attribs);
auto outerScope = tracer->WithActiveSpan(outerSpan);

auto topSpan = tracer->StartSpan("MySpanTop");
std::this_thread::sleep_for (std::chrono::seconds(1));

auto outerSpan = tracer->StartSpan("MySpanL2", attribs);

// Create nested span. Note how we share the attributes here.
// It is Okay to either reuse/share or have your own attributes.
auto innerSpan = tracer->StartSpan("MySpanL3", attribs);
// Create nested span. Note how we share the attributes here.
// It is Okay to either reuse/share or have your own attributes.
{
auto innerSpan = tracer->StartSpan("MySpanL3", attribs);
auto innerScope = tracer->WithActiveSpan(innerSpan);

// Add first event
std::string eventName1 = "MyEvent1";
Properties event1 =
{
{"uint32Key", (uint32_t)1234},
{"uint64Key", (uint64_t)1234567890},
{"strKey", "someValue"}
};
EXPECT_NO_THROW(outerSpan->AddEvent(eventName1, event1));
std::this_thread::sleep_for (std::chrono::seconds(1));
// Add first event
std::string eventName1 = "MyEvent1";
Properties event1 =
{
{"uint32Key", (uint32_t)1234},
{"uint64Key", (uint64_t)1234567890},
{"strKey", "someValue"}
};
EXPECT_NO_THROW(outerSpan->AddEvent(eventName1, event1));
std::this_thread::sleep_for (std::chrono::seconds(1));

// Add second event
std::string eventName2 = "MyEvent2";
Properties event2 =
{
{"uint32Key", (uint32_t)9876},
{"uint64Key", (uint64_t)987654321},
{"strKey", "anotherValue"}
};
EXPECT_NO_THROW(outerSpan->AddEvent(eventName2, event2));
std::this_thread::sleep_for (std::chrono::seconds(2));
// Add second event
std::string eventName2 = "MyEvent2";
Properties event2 =
{
{"uint32Key", (uint32_t)9876},
{"uint64Key", (uint64_t)987654321},
{"strKey", "anotherValue"}
};
EXPECT_NO_THROW(outerSpan->AddEvent(eventName2, event2));
std::this_thread::sleep_for (std::chrono::seconds(2));

std::string eventName3= "MyEvent3";
Properties event3 =
{
/* Extra metadata that allows event to flow to A.I. pipeline */
{"metadata", "ai_event"},
{"uint32Key", (uint32_t)9876},
{"uint64Key", (uint64_t)987654321},
// {"int32array", {{-1,0,1,2,3}} },
{"tempString", getTemporaryValue() }
};
EXPECT_NO_THROW(innerSpan->AddEvent(eventName3, event3));
std::this_thread::sleep_for (std::chrono::seconds(1));
std::string eventName3= "MyEvent3";
Properties event3 =
{
/* Extra metadata that allows event to flow to A.I. pipeline */
{"metadata", "ai_event"},
{"uint32Key", (uint32_t)9876},
{"uint64Key", (uint64_t)987654321},
// {"int32array", {{-1,0,1,2,3}} },
{"tempString", getTemporaryValue() }
};
EXPECT_NO_THROW(innerSpan->AddEvent(eventName3, event3));
std::this_thread::sleep_for (std::chrono::seconds(1));
EXPECT_NO_THROW(innerSpan->End());

EXPECT_NO_THROW(innerSpan->End()); // end innerSpan
}
EXPECT_NO_THROW(outerSpan->End());

EXPECT_NO_THROW(outerSpan->End()); // end outerSpan
EXPECT_NO_THROW(topSpan->End()); // end topSpan
}
EXPECT_NO_THROW(topSpan->End());
}

EXPECT_NO_THROW(tracer->CloseWithMicroseconds(0));
}
Expand Down Expand Up @@ -141,12 +148,21 @@ TEST(ETWTracer, TracerCheckMinDecoration)
{"enableAutoParent", false}
});
auto tracer = tp.GetTracer(providerName, "TLD");
auto aSpan = tracer->StartSpan("A.min");
auto bSpan = tracer->StartSpan("B.min");
auto cSpan = tracer->StartSpan("C.min");
cSpan->End();
bSpan->End();
aSpan->End();
{
auto aSpan = tracer->StartSpan("A.min");
auto aScope = tracer->WithActiveSpan(aSpan);
{
auto bSpan = tracer->StartSpan("B.min");
auto bScope = tracer->WithActiveSpan(bSpan);
{
auto cSpan = tracer->StartSpan("C.min");
auto cScope = tracer->WithActiveSpan(cSpan);
EXPECT_NO_THROW(cSpan->End());
}
EXPECT_NO_THROW(bSpan->End());
}
EXPECT_NO_THROW(aSpan->End());
}
tracer->CloseWithMicroseconds(0);
}

Expand Down Expand Up @@ -183,12 +199,21 @@ TEST(ETWTracer, TracerCheckMaxDecoration)
{"enableAutoParent", true}
});
auto tracer = tp.GetTracer(providerName, "TLD" );
auto aSpan = tracer->StartSpan("A.max");
auto bSpan = tracer->StartSpan("B.max");
auto cSpan = tracer->StartSpan("C.max");
cSpan->End();
bSpan->End();
aSpan->End();
{
auto aSpan = tracer->StartSpan("A.max");
auto aScope = tracer->WithActiveSpan(aSpan);
{
auto bSpan = tracer->StartSpan("B.max");
auto bScope = tracer->WithActiveSpan(bSpan);
{
auto cSpan = tracer->StartSpan("C.max");
auto cScope = tracer->WithActiveSpan(cSpan);
EXPECT_NO_THROW(cSpan->End());
}
EXPECT_NO_THROW(bSpan->End());
}
EXPECT_NO_THROW(aSpan->End());
}
tracer->CloseWithMicroseconds(0);
}

Expand All @@ -206,10 +231,13 @@ TEST(ETWTracer, TracerCheckMsgPack)
auto tracer = tp.GetTracer(providerName, "MsgPack" );
{
auto aSpan = tracer->StartSpan("A.max");
auto aScope = tracer->WithActiveSpan(aSpan);
{
auto bSpan = tracer->StartSpan("B.max");
auto bScope = tracer->WithActiveSpan(bSpan);
{
auto cSpan = tracer->StartSpan("C.max");
auto cScope = tracer->WithActiveSpan(cSpan);
std::string eventName = "MyMsgPackEvent";
Properties event =
{
Expand All @@ -218,11 +246,11 @@ TEST(ETWTracer, TracerCheckMsgPack)
{"strKey", "someValue"}
};
cSpan->AddEvent(eventName, event);
cSpan->End();
EXPECT_NO_THROW(cSpan->End());
}
bSpan->End();
EXPECT_NO_THROW(bSpan->End());
}
aSpan->End();
EXPECT_NO_THROW(aSpan->End());
}
tracer->CloseWithMicroseconds(0);
}
Expand Down