@@ -238,6 +238,51 @@ TEST(ResourceTest, OtelResourceDetector)
238238 unsetenv (" OTEL_RESOURCE_ATTRIBUTES" );
239239}
240240
241+ TEST (ResourceTest, OtelResourceDetectorPercentDecodesValues)
242+ {
243+ std::map<std::string, std::string> expected_attributes = {
244+ {" key1" , " hello world" }, {" key2" , " a,b" }, {" key3" , " 100%" }};
245+
246+ setenv (" OTEL_RESOURCE_ATTRIBUTES" , " key1=hello%20world,key2=a%2Cb,key3=100%25" , 1 );
247+
248+ OTELResourceDetector detector;
249+ auto resource = detector.Detect ();
250+ auto received_attributes = resource.GetAttributes ();
251+ for (auto &e : received_attributes)
252+ {
253+ EXPECT_TRUE (expected_attributes.find (e.first ) != expected_attributes.end ());
254+ if (expected_attributes.find (e.first ) != expected_attributes.end ())
255+ {
256+ EXPECT_EQ (expected_attributes.find (e.first )->second , nostd::get<std::string>(e.second ));
257+ }
258+ }
259+ EXPECT_EQ (received_attributes.size (), expected_attributes.size ());
260+
261+ unsetenv (" OTEL_RESOURCE_ATTRIBUTES" );
262+ }
263+
264+ TEST (ResourceTest, OtelResourceDetectorMalformedEscapeLeftAsIs)
265+ {
266+ std::map<std::string, std::string> expected_attributes = {{" key" , " 100%" }, {" bad" , " 50%z" }};
267+
268+ setenv (" OTEL_RESOURCE_ATTRIBUTES" , " key=100%,bad=50%z" , 1 );
269+
270+ OTELResourceDetector detector;
271+ auto resource = detector.Detect ();
272+ auto received_attributes = resource.GetAttributes ();
273+ for (auto &e : received_attributes)
274+ {
275+ EXPECT_TRUE (expected_attributes.find (e.first ) != expected_attributes.end ());
276+ if (expected_attributes.find (e.first ) != expected_attributes.end ())
277+ {
278+ EXPECT_EQ (expected_attributes.find (e.first )->second , nostd::get<std::string>(e.second ));
279+ }
280+ }
281+ EXPECT_EQ (received_attributes.size (), expected_attributes.size ());
282+
283+ unsetenv (" OTEL_RESOURCE_ATTRIBUTES" );
284+ }
285+
241286TEST (ResourceTest, OtelResourceDetectorServiceNameOverride)
242287{
243288 std::map<std::string, std::string> expected_attributes = {{" service.name" , " new_name" }};
0 commit comments