Skip to content

Commit

Permalink
Added unit test, CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Oct 18, 2023
1 parent fe36034 commit ecf6db7
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ Increment the:

* [BUILD] Remove WITH_REMOVE_METER_PREVIEW, use WITH_ABI_VERSION_2 instead
[#2370](https://github.com/open-telemetry/opentelemetry-cpp/pull/2370)
* [API] Add InstrumentationScope attributes in TracerProvider::GetTracer()
[#2371](https://github.com/open-telemetry/opentelemetry-cpp/pull/2371)

Important changes:

* [API] Add InstrumentationScope attributes in TracerProvider::GetTracer()
[#2371](https://github.com/open-telemetry/opentelemetry-cpp/pull/2371)
* TracerProvider::GetTracer() now accepts InstrumentationScope attributes.
* 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.

Breaking changes:

Expand Down
137 changes: 137 additions & 0 deletions sdk/test/trace/tracer_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,143 @@ TEST(TracerProvider, GetTracer)
ASSERT_EQ(instrumentation_scope3.GetVersion(), "1.0.0");
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
TEST(TracerProvider, GetTracerAbiv2)
{
std::unique_ptr<SpanProcessor> processor(new SimpleSpanProcessor(nullptr));
std::vector<std::unique_ptr<SpanProcessor>> processors;
processors.push_back(std::move(processor));
std::unique_ptr<TracerContext> context1(
new TracerContext(std::move(processors), Resource::Create({})));
TracerProvider tp(std::move(context1));

auto t1 = tp.GetTracer("name1", "version1", "url1");
ASSERT_NE(nullptr, t1);

auto t2 = tp.GetTracer("name2", "version2", "url2", nullptr);
ASSERT_NE(nullptr, t2);

auto t3 = tp.GetTracer("name3", "version3", "url3", {{"accept_single_attr", true}});
ASSERT_NE(nullptr, t3);
{
auto tracer = static_cast<opentelemetry::sdk::trace::Tracer *>(t3.get());
auto scope = tracer->GetInstrumentationScope();
auto attrs = scope.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
auto attr = attrs.find("accept_single_attr");
ASSERT_FALSE(attr == attrs.end());
ASSERT_TRUE(opentelemetry::nostd::holds_alternative<bool>(attr->second));
EXPECT_EQ(opentelemetry::nostd::get<bool>(attr->second), true);
}

std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attr4 = {
"accept_single_attr", true};
auto t4 = tp.GetTracer("name4", "version4", "url4", {attr4});
ASSERT_NE(nullptr, t4);
{
auto tracer = static_cast<opentelemetry::sdk::trace::Tracer *>(t4.get());
auto scope = tracer->GetInstrumentationScope();
auto attrs = scope.GetAttributes();
ASSERT_EQ(attrs.size(), 1);
auto attr = attrs.find("accept_single_attr");
ASSERT_FALSE(attr == attrs.end());
ASSERT_TRUE(opentelemetry::nostd::holds_alternative<bool>(attr->second));
EXPECT_EQ(opentelemetry::nostd::get<bool>(attr->second), true);
}

auto t5 = tp.GetTracer("name5", "version5", "url5", {{"foo", "1"}, {"bar", "2"}});
ASSERT_NE(nullptr, t5);
{
auto tracer = static_cast<opentelemetry::sdk::trace::Tracer *>(t5.get());
auto scope = tracer->GetInstrumentationScope();
auto attrs = scope.GetAttributes();
ASSERT_EQ(attrs.size(), 2);
auto attr = attrs.find("bar");
ASSERT_FALSE(attr == attrs.end());
ASSERT_TRUE(opentelemetry::nostd::holds_alternative<std::string>(attr->second));
EXPECT_EQ(opentelemetry::nostd::get<std::string>(attr->second), "2");
}

std::initializer_list<
std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue>>
attrs6 = {{"foo", "1"}, {"bar", 42}};

auto t6 = tp.GetTracer("name6", "version6", "url6", attrs6);
ASSERT_NE(nullptr, t6);
{
auto tracer = static_cast<opentelemetry::sdk::trace::Tracer *>(t6.get());
auto scope = tracer->GetInstrumentationScope();
auto attrs = scope.GetAttributes();
ASSERT_EQ(attrs.size(), 2);
auto attr = attrs.find("bar");
ASSERT_FALSE(attr == attrs.end());
ASSERT_TRUE(opentelemetry::nostd::holds_alternative<int>(attr->second));
EXPECT_EQ(opentelemetry::nostd::get<int>(attr->second), 42);
}

typedef std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> KV;

std::initializer_list<KV> attrs7 = {{"foo", 3.14}, {"bar", "2"}};
auto t7 = tp.GetTracer("name7", "version7", "url7", attrs7);
ASSERT_NE(nullptr, t7);
{
auto tracer = static_cast<opentelemetry::sdk::trace::Tracer *>(t7.get());
auto scope = tracer->GetInstrumentationScope();
auto attrs = scope.GetAttributes();
ASSERT_EQ(attrs.size(), 2);
auto attr = attrs.find("foo");
ASSERT_FALSE(attr == attrs.end());
ASSERT_TRUE(opentelemetry::nostd::holds_alternative<double>(attr->second));
EXPECT_EQ(opentelemetry::nostd::get<double>(attr->second), 3.14);
}

auto t8 = tp.GetTracer("name8", "version8", "url8",
{{"a", "string"},
{"b", false},
{"c", 314159},
{"d", (unsigned int)314159},
{"e", (int32_t)-20},
{"f", (uint32_t)20},
{"g", (int64_t)-20},
{"h", (uint64_t)20},
{"i", 3.1},
{"j", "string"}});
ASSERT_NE(nullptr, t8);
{
auto tracer = static_cast<opentelemetry::sdk::trace::Tracer *>(t8.get());
auto scope = tracer->GetInstrumentationScope();
auto attrs = scope.GetAttributes();
ASSERT_EQ(attrs.size(), 10);
auto attr = attrs.find("e");
ASSERT_FALSE(attr == attrs.end());
ASSERT_TRUE(opentelemetry::nostd::holds_alternative<int32_t>(attr->second));
EXPECT_EQ(opentelemetry::nostd::get<int32_t>(attr->second), -20);
}

std::map<std::string, opentelemetry::common::AttributeValue> attr9{
{"a", "string"}, {"b", false}, {"c", 314159}, {"d", (unsigned int)314159},
{"e", (int32_t)-20}, {"f", (uint32_t)20}, {"g", (int64_t)-20}, {"h", (uint64_t)20},
{"i", 3.1}, {"j", "string"}};

auto t9 = tp.GetTracer("name9", "version9", "url9", attr9);
ASSERT_NE(nullptr, t9);
{
auto tracer = static_cast<opentelemetry::sdk::trace::Tracer *>(t9.get());
auto scope = tracer->GetInstrumentationScope();
auto attrs = scope.GetAttributes();
ASSERT_EQ(attrs.size(), 10);
auto attr = attrs.find("h");
ASSERT_FALSE(attr == attrs.end());
ASSERT_TRUE(opentelemetry::nostd::holds_alternative<uint64_t>(attr->second));
EXPECT_EQ(opentelemetry::nostd::get<uint64_t>(attr->second), 20);
}

// cleanup properly without crash
tp.ForceFlush();
tp.Shutdown();
}
#endif /* OPENTELEMETRY_ABI_VERSION_NO >= 2 */

TEST(TracerProvider, Shutdown)
{
std::unique_ptr<SpanProcessor> processor(new SimpleSpanProcessor(nullptr));
Expand Down

0 comments on commit ecf6db7

Please sign in to comment.