Skip to content

Commit 355550a

Browse files
committed
Add tests for TracerConfig
1 parent f22f9ec commit 355550a

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

sdk/test/trace/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ cc_test(
143143
],
144144
)
145145

146+
cc_test(
147+
name = "tracer_config_test",
148+
srcs = [
149+
"tracer_config_test.cc",
150+
],
151+
tags = [
152+
"test",
153+
"trace",
154+
],
155+
deps = [
156+
"//sdk/src/trace",
157+
"@com_google_googletest//:gtest_main",
158+
],
159+
)
160+
146161
otel_cc_benchmark(
147162
name = "sampler_benchmark",
148163
srcs = ["sampler_benchmark.cc"],

sdk/test/trace/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ foreach(
1111
always_on_sampler_test
1212
parent_sampler_test
1313
trace_id_ratio_sampler_test
14-
batch_span_processor_test)
14+
batch_span_processor_test
15+
tracer_config_test)
1516
add_executable(${testname} "${testname}.cc")
1617
target_link_libraries(
1718
${testname}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include <gtest/gtest.h>
5+
#include <opentelemetry/sdk/trace/tracer_config.h>
6+
7+
namespace trace_sdk = opentelemetry::sdk::trace;
8+
namespace instrumentation_scope = opentelemetry::sdk::instrumentationscope;
9+
10+
/** Tests to verify the basic behavior of trace_sdk::TracerConfig */
11+
12+
TEST(TracerConfig, CheckDisabledWorksAsExpected)
13+
{
14+
trace_sdk::TracerConfig disabled_config = trace_sdk::TracerConfig::Disabled();
15+
ASSERT_FALSE(disabled_config.IsEnabled());
16+
}
17+
18+
TEST(TracerConfig, CheckEnabledWorksAsExpected)
19+
{
20+
trace_sdk::TracerConfig enabled_config = trace_sdk::TracerConfig::Enabled();
21+
ASSERT_TRUE(enabled_config.IsEnabled());
22+
}
23+
24+
TEST(TracerConfig, CheckDefaultConfigWorksAccToSpec)
25+
{
26+
trace_sdk::TracerConfig default_config = trace_sdk::TracerConfig::Default();
27+
ASSERT_TRUE(default_config.IsEnabled());
28+
}
29+
30+
/** Tests to verify the behavior of trace_sdk::TracerConfig::DefaultConfigurator */
31+
32+
std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attr1 = {
33+
"accept_single_attr", true};
34+
std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attr2 = {
35+
"accept_second_attr", "some other attr"};
36+
std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attr3 = {
37+
"accept_third_attr", 3};
38+
39+
const instrumentation_scope::InstrumentationScope instrumentation_scopes[] = {
40+
*instrumentation_scope::InstrumentationScope::Create("test_scope_1").get(),
41+
*instrumentation_scope::InstrumentationScope::Create("test_scope_2", "1.0").get(),
42+
*instrumentation_scope::InstrumentationScope::Create("test_scope_3", "0", "https://opentelemetry.io/schemas/v1.18.0").get(),
43+
*instrumentation_scope::InstrumentationScope::Create("test_scope_3", "0", "https://opentelemetry.io/schemas/v1.18.0", {attr1})
44+
.get(),
45+
*instrumentation_scope::InstrumentationScope::Create("test_scope_4", "0", "https://opentelemetry.io/schemas/v1.18.0", {attr1, attr2, attr3})
46+
.get(),
47+
};
48+
49+
// Test fixture for VerifyDefaultConfiguratorBehavior
50+
class DefaultTracerConfiguratorTestFixture : public ::testing::TestWithParam<instrumentation_scope::InstrumentationScope>
51+
{};
52+
53+
// verifies that the default configurator always returns the default tracer config
54+
TEST_P(DefaultTracerConfiguratorTestFixture, VerifyDefaultConfiguratorBehavior)
55+
{
56+
const instrumentation_scope::InstrumentationScope& scope = GetParam();
57+
trace_sdk::TracerConfigurator default_configurator = trace_sdk::TracerConfig::DefaultConfigurator();
58+
59+
ASSERT_EQ(default_configurator(scope), trace_sdk::TracerConfig::Default());
60+
}
61+
62+
INSTANTIATE_TEST_SUITE_P(InstrumentationScopes, DefaultTracerConfiguratorTestFixture, ::testing::ValuesIn(instrumentation_scopes));

0 commit comments

Comments
 (0)