Skip to content

Commit

Permalink
Added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Oct 22, 2023
1 parent 1f371a6 commit 76ce23b
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Increment the:
[#2378](https://github.com/open-telemetry/opentelemetry-cpp/pull/2378)
* [API] Add InstrumentationScope attributes in TracerProvider::GetTracer()
[#2371](https://github.com/open-telemetry/opentelemetry-cpp/pull/2371)
* [API] Add a new AddLink() operation to Span
[#2380](https://github.com/open-telemetry/opentelemetry-cpp/pull/2380)

Important changes:

Expand All @@ -32,6 +34,14 @@ Important changes:
* When building with `CMake` option `WITH_ABI_VERSION_1=ON` (by default)
the `ABI` is unchanged, and the fix is not available.

* [API] Add a new AddLink() operation to Span
[#2380](https://github.com/open-telemetry/opentelemetry-cpp/pull/2380)
* new `API` Span::AddLink() adds links to a span.
* Because this is an `ABI` breaking change, the fix is only available
with the `CMake` option `WITH_ABI_VERSION_2=ON`.
* When building with `CMake` option `WITH_ABI_VERSION_1=ON` (by default)
the `ABI` is unchanged, and the fix is not available.

* [BUILD] Make WITH_OTLP_HTTP_SSL_PREVIEW mainstream
[#2378](https://github.com/open-telemetry/opentelemetry-cpp/pull/2378)
* The experimental `CMake` option `WITH_OTLP_HTTP_SSL_PREVIEW`
Expand Down
232 changes: 232 additions & 0 deletions sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,238 @@ TEST(Tracer, SpanSetLinks)
}
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
TEST(Tracer, SpanAddLinksAbiv2)
{
InMemorySpanExporter *exporter = new InMemorySpanExporter();
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
auto tracer = initTracer(std::unique_ptr<SpanExporter>{exporter});

{
auto span = tracer->StartSpan("span 1");
// Single span link passed through Initialization list
span->AddLink({{SpanContext(false, false), {{"attr2", 2}}}});
span->End();

auto spans = span_data->GetSpans();
ASSERT_EQ(1, spans.size());

auto &span_data_links = spans.at(0)->GetLinks();
ASSERT_EQ(1, span_data_links.size());
auto link = span_data_links.at(0);
ASSERT_EQ(nostd::get<int>(link.GetAttributes().at("attr2")), 2);
}

{
auto span = tracer->StartSpan("span 2");
// Multiple span links passed through Initialization list
span->AddLink(
{{SpanContext(false, false), {{"attr2", 2}}}, {SpanContext(false, false), {{"attr3", 3}}}});
span->End();

auto spans = span_data->GetSpans();
ASSERT_EQ(1, spans.size());

auto &span_data_links = spans.at(0)->GetLinks();
ASSERT_EQ(2, span_data_links.size());
auto link1 = span_data_links.at(0);
ASSERT_EQ(nostd::get<int>(link1.GetAttributes().at("attr2")), 2);
auto link2 = span_data_links.at(1);
ASSERT_EQ(nostd::get<int>(link2.GetAttributes().at("attr3")), 3);
}

{
auto span = tracer->StartSpan("span 3");
// Multiple links, each with multiple attributes passed through Initialization list
span->AddLink({{SpanContext(false, false), {{"attr2", 2}, {"attr3", 3}}},
{SpanContext(false, false), {{"attr4", 4}}}});
span->End();

auto spans = span_data->GetSpans();
ASSERT_EQ(1, spans.size());

auto &span_data_links = spans.at(0)->GetLinks();
ASSERT_EQ(2, span_data_links.size());
auto link1 = span_data_links.at(0);
ASSERT_EQ(nostd::get<int>(link1.GetAttributes().at("attr2")), 2);
ASSERT_EQ(nostd::get<int>(link1.GetAttributes().at("attr3")), 3);
auto link2 = span_data_links.at(1);
ASSERT_EQ(nostd::get<int>(link2.GetAttributes().at("attr4")), 4);
}

{
std::map<std::string, std::string> attrs1 = {{"attr1", "1"}, {"attr2", "2"}};
std::map<std::string, std::string> attrs2 = {{"attr3", "3"}, {"attr4", "4"}};

std::vector<std::pair<SpanContext, std::map<std::string, std::string>>> links = {
{SpanContext(false, false), attrs1}, {SpanContext(false, false), attrs2}};

auto span = tracer->StartSpan("span 4");
span->AddLink(links);
span->End();

auto spans = span_data->GetSpans();

auto &span_data_links = spans.at(0)->GetLinks();
ASSERT_EQ(2, span_data_links.size());
auto link1 = span_data_links.at(0);
ASSERT_EQ(nostd::get<std::string>(link1.GetAttributes().at("attr1")), "1");
ASSERT_EQ(nostd::get<std::string>(link1.GetAttributes().at("attr2")), "2");
auto link2 = span_data_links.at(1);
ASSERT_EQ(nostd::get<std::string>(link2.GetAttributes().at("attr3")), "3");
ASSERT_EQ(nostd::get<std::string>(link2.GetAttributes().at("attr4")), "4");
}

{
auto span = tracer->StartSpan("span 5");

// Single span link passed through Initialization list
span->AddLink({{SpanContext(false, false), {{"attr10", 10}}}});
span->AddLink({{SpanContext(false, false), {{"attr11", 11}}}});

// Multiple span links passed through Initialization list
span->AddLink({{SpanContext(false, false), {{"attr12", 12}}},
{SpanContext(false, false), {{"attr13", 13}}}});
span->AddLink({{SpanContext(false, false), {{"attr14", 14}}},
{SpanContext(false, false), {{"attr15", 15}}}});

// Multiple links, each with multiple attributes passed through Initialization list
span->AddLink({{SpanContext(false, false), {{"attr16", 16}, {"attr17", 17}}},
{SpanContext(false, false), {{"attr18", 18}}}});
span->AddLink({{SpanContext(false, false), {{"attr19", 19}, {"attr20", 20}}},
{SpanContext(false, false), {{"attr21", 21}}}});

std::map<std::string, std::string> attrsa1 = {{"attra1", "1"}, {"attra2", "2"}};
std::map<std::string, std::string> attrsa2 = {{"attra3", "3"}, {"attra4", "4"}};

std::vector<std::pair<SpanContext, std::map<std::string, std::string>>> linksa = {
{SpanContext(false, false), attrsa1}, {SpanContext(false, false), attrsa2}};

span->AddLink(linksa);

std::map<std::string, std::string> attrsb1 = {{"attrb1", "1"}, {"attrb2", "2"}};
std::map<std::string, std::string> attrsb2 = {{"attrb3", "3"}, {"attrb4", "4"}};

std::vector<std::pair<SpanContext, std::map<std::string, std::string>>> linksb = {
{SpanContext(false, false), attrsb1}, {SpanContext(false, false), attrsb2}};

span->AddLink(linksb);

span->End();

auto spans = span_data->GetSpans();
ASSERT_EQ(1, spans.size());

auto &span_data_links = spans.at(0)->GetLinks();
ASSERT_EQ(14, span_data_links.size());

{
auto link = span_data_links.at(0);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
ASSERT_EQ(nostd::get<int>(attrs.at("attr10")), 10);
}

{
auto link = span_data_links.at(1);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
ASSERT_EQ(nostd::get<int>(attrs.at("attr11")), 11);
}

{
auto link = span_data_links.at(2);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
ASSERT_EQ(nostd::get<int>(attrs.at("attr12")), 12);
}

{
auto link = span_data_links.at(3);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
ASSERT_EQ(nostd::get<int>(attrs.at("attr13")), 13);
}

{
auto link = span_data_links.at(4);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
ASSERT_EQ(nostd::get<int>(attrs.at("attr14")), 14);
}

{
auto link = span_data_links.at(5);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
ASSERT_EQ(nostd::get<int>(attrs.at("attr15")), 15);
}

{
auto link = span_data_links.at(6);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 2);
ASSERT_EQ(nostd::get<int>(attrs.at("attr16")), 16);
ASSERT_EQ(nostd::get<int>(attrs.at("attr17")), 17);
}

{
auto link = span_data_links.at(7);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
ASSERT_EQ(nostd::get<int>(attrs.at("attr18")), 18);
}

{
auto link = span_data_links.at(8);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 2);
ASSERT_EQ(nostd::get<int>(attrs.at("attr19")), 19);
ASSERT_EQ(nostd::get<int>(attrs.at("attr20")), 20);
}

{
auto link = span_data_links.at(9);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
ASSERT_EQ(nostd::get<int>(attrs.at("attr21")), 21);
}

{
auto link = span_data_links.at(10);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 2);
ASSERT_EQ(nostd::get<std::string>(attrs.at("attra1")), "1");
ASSERT_EQ(nostd::get<std::string>(attrs.at("attra2")), "2");
}

{
auto link = span_data_links.at(11);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 2);
ASSERT_EQ(nostd::get<std::string>(attrs.at("attra3")), "3");
ASSERT_EQ(nostd::get<std::string>(attrs.at("attra4")), "4");
}

{
auto link = span_data_links.at(12);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 2);
ASSERT_EQ(nostd::get<std::string>(attrs.at("attrb1")), "1");
ASSERT_EQ(nostd::get<std::string>(attrs.at("attrb2")), "2");
}

{
auto link = span_data_links.at(13);
auto attrs = link.GetAttributes();
ASSERT_EQ(attrs.size(), 2);
ASSERT_EQ(nostd::get<std::string>(attrs.at("attrb3")), "3");
ASSERT_EQ(nostd::get<std::string>(attrs.at("attrb4")), "4");
}
}
}
#endif /* OPENTELEMETRY_ABI_VERSION_NO >= 2 */

TEST(Tracer, TestAlwaysOnSampler)
{
InMemorySpanExporter *exporter = new InMemorySpanExporter();
Expand Down

0 comments on commit 76ce23b

Please sign in to comment.