Skip to content

Commit 579e526

Browse files
committed
Add uri decode for OTEL_RESOUCE_ATTRIBUTES
1 parent 74ced85 commit 579e526

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Add uri decode values from OTEL_RESOURCE_ATTRIBUTES
11+
([#3046](https://github.com/open-telemetry/opentelemetry-python/pull/3046))
1012
- Add missing entry points for OTLP/HTTP exporter
1113
([#3027](https://github.com/open-telemetry/opentelemetry-python/pull/3027))
1214

opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import os
6262
import sys
6363
import typing
64+
from urllib import parse
6465
from json import dumps
6566

6667
import pkg_resources
@@ -289,7 +290,8 @@ def detect(self) -> "Resource":
289290
exc,
290291
)
291292
continue
292-
env_resource_map[key.strip()] = value.strip()
293+
value_uri_decoded = parse.unquote(value.strip())
294+
env_resource_map[key.strip()] = value_uri_decoded
293295

294296
service_name = os.environ.get(OTEL_SERVICE_NAME)
295297
if service_name:

opentelemetry-sdk/tests/resources/test_resources.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,16 @@ def test_invalid_key_value_pairs(self):
492492
resources.Resource({"k": "v", "k2": "v2", "foo": "bar=baz"}),
493493
)
494494

495+
def test_multiple_with_uri_decode(self):
496+
detector = resources.OTELResourceDetector()
497+
os.environ[
498+
resources.OTEL_RESOURCE_ATTRIBUTES
499+
] = "key=value%20test%0A, key2=value+%202"
500+
self.assertEqual(
501+
detector.detect(),
502+
resources.Resource({"key": "value test\n", "key2": "value+ 2"}),
503+
)
504+
495505
@mock.patch.dict(
496506
os.environ,
497507
{resources.OTEL_SERVICE_NAME: "test-srv-name"},

0 commit comments

Comments
 (0)