Skip to content

Commit d95b53e

Browse files
committed
Rework for propagators, added tests.
1 parent d9580d0 commit d95b53e

File tree

9 files changed

+136
-12
lines changed

9 files changed

+136
-12
lines changed

functional/configuration/shelltests/kitchen-sink.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,11 @@ propagator:
562562
# The value is a comma separated list of propagator identifiers matching the format of OTEL_PROPAGATORS. See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#general-sdk-configuration for details.
563563
# Built-in propagator identifiers include: tracecontext, baggage, b3, b3multi, jaeger, ottrace. Known third party identifiers include: xray.
564564
# If the resolved list of propagators (from .composite and .composite_list) is empty, a noop propagator is used.
565-
composite_list: "tracecontext,baggage,b3,b3multi,jaeger,ottrace,xray"
565+
# composite_list: "tracecontext,baggage,b3,b3multi,jaeger,ottrace,xray"
566+
567+
# ottrace, xray not supported in opentelemetry-cpp
568+
composite_list: "tracecontext,baggage,b3,b3multi,jaeger"
569+
566570
# Configure tracer provider.
567571
# If omitted, a noop tracer provider is used.
568572
tracer_provider:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
$ otel_configuration_check --yaml shelltests/propagator_both.yaml
5+
>
6+
MODEL PARSED
7+
SDK CREATED
8+
>= 0
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
file_format: 0.0
5+
6+
propagator:
7+
composite:
8+
- tracecontext:
9+
- baggage:
10+
composite_list: "b3,b3multi"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
$ otel_configuration_check --yaml shelltests/propagator_composite_list.yaml
5+
>
6+
MODEL PARSED
7+
SDK CREATED
8+
>= 0
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
file_format: 0.0
5+
6+
propagator:
7+
composite_list: "tracecontext,baggage,b3,b3multi"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
$ otel_configuration_check --yaml shelltests/propagator_duplicates.yaml
5+
>
6+
MODEL PARSED
7+
SDK CREATED
8+
>= 0
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
file_format: 0.0
5+
6+
propagator:
7+
composite:
8+
- tracecontext:
9+
- baggage:
10+
composite_list: "tracecontext,baggage,baggage,b3,b3multi"

sdk/src/configuration/sdk_builder.cc

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,42 +1061,87 @@ SdkBuilder::CreateTextMapPropagator(const std::string &name) const
10611061
throw UnsupportedException(die);
10621062
}
10631063

1064+
static bool IsDuplicate(const std::vector<std::string> &propagator_seen, const std::string &name)
1065+
{
1066+
bool duplicate = false;
1067+
for (const auto &seen : propagator_seen)
1068+
{
1069+
if (name == seen)
1070+
{
1071+
duplicate = true;
1072+
}
1073+
}
1074+
1075+
return duplicate;
1076+
}
1077+
10641078
std::unique_ptr<opentelemetry::context::propagation::TextMapPropagator>
10651079
SdkBuilder::CreatePropagator(
10661080
const std::unique_ptr<opentelemetry::sdk::configuration::PropagatorConfiguration> &model) const
10671081
{
1082+
std::unique_ptr<opentelemetry::context::propagation::CompositePropagator> sdk;
10681083
std::vector<std::unique_ptr<opentelemetry::context::propagation::TextMapPropagator>> propagators;
10691084
std::unique_ptr<opentelemetry::context::propagation::TextMapPropagator> propagator;
1085+
std::vector<std::string> propagator_seen;
1086+
bool duplicate;
1087+
1088+
/*
1089+
* Note that the spec only requires to check duplicates between
1090+
* composite and composite_list.
1091+
* Here we check for duplicates globally, for ease of use.
1092+
*/
10701093

10711094
for (const auto &name : model->composite)
10721095
{
1073-
propagator = CreateTextMapPropagator(name);
1074-
propagators.push_back(std::move(propagator));
1096+
duplicate = IsDuplicate(propagator_seen, name);
1097+
1098+
if (!duplicate)
1099+
{
1100+
propagator = CreateTextMapPropagator(name);
1101+
propagators.push_back(std::move(propagator));
1102+
propagator_seen.push_back(name);
1103+
}
10751104
}
10761105

10771106
if (model->composite_list.size() > 0)
10781107
{
10791108
std::string str_list = model->composite_list;
1080-
size_t start_pos = 0;
1081-
size_t end_pos = 0;
1082-
char separator = ',';
1109+
size_t start_pos = 0;
1110+
size_t end_pos = 0;
1111+
char separator = ',';
10831112
std::string name;
10841113

10851114
while ((end_pos = str_list.find(separator, start_pos)) != std::string::npos)
10861115
{
10871116
name = str_list.substr(start_pos, end_pos - start_pos);
1088-
propagator = CreateTextMapPropagator(name);
1089-
propagators.push_back(std::move(propagator));
1117+
1118+
duplicate = IsDuplicate(propagator_seen, name);
1119+
1120+
if (!duplicate)
1121+
{
1122+
propagator = CreateTextMapPropagator(name);
1123+
propagators.push_back(std::move(propagator));
1124+
propagator_seen.push_back(name);
1125+
}
10901126
start_pos = end_pos + 1;
10911127
}
10921128

10931129
name = str_list.substr(start_pos);
1094-
propagator = CreateTextMapPropagator(name);
1095-
propagators.push_back(std::move(propagator));
1130+
1131+
duplicate = IsDuplicate(propagator_seen, name);
1132+
1133+
if (!duplicate)
1134+
{
1135+
propagator = CreateTextMapPropagator(name);
1136+
propagators.push_back(std::move(propagator));
1137+
}
10961138
}
10971139

1098-
auto sdk = std::make_unique<opentelemetry::context::propagation::CompositePropagator>(
1099-
std::move(propagators));
1140+
if (propagators.size() > 0)
1141+
{
1142+
sdk = std::make_unique<opentelemetry::context::propagation::CompositePropagator>(
1143+
std::move(propagators));
1144+
}
11001145

11011146
return sdk;
11021147
}

sdk/test/configuration/yaml_propagator_test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,27 @@ file_format: xx.yy
146146
ASSERT_EQ(config->propagator->composite[2], "ccc");
147147
ASSERT_EQ(config->propagator->composite_list, "ddd,eee,fff");
148148
}
149+
150+
TEST(YamlPropagator, propagator_duplicates)
151+
{
152+
std::string yaml = R"(
153+
file_format: xx.yy
154+
propagator:
155+
composite:
156+
- aaa:
157+
- bbb:
158+
- bbb:
159+
- ccc:
160+
composite_list: "aaa,eee,eee,fff,ccc"
161+
)";
162+
163+
auto config = DoParse(yaml);
164+
ASSERT_NE(config, nullptr);
165+
ASSERT_NE(config->propagator, nullptr);
166+
ASSERT_EQ(config->propagator->composite.size(), 4);
167+
ASSERT_EQ(config->propagator->composite[0], "aaa");
168+
ASSERT_EQ(config->propagator->composite[1], "bbb");
169+
ASSERT_EQ(config->propagator->composite[2], "bbb");
170+
ASSERT_EQ(config->propagator->composite[3], "ccc");
171+
ASSERT_EQ(config->propagator->composite_list, "aaa,eee,eee,fff,ccc");
172+
}

0 commit comments

Comments
 (0)