diff --git a/CHANGELOG.md b/CHANGELOG.md index 339cdb62460..1897d3d735c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Add url decode values from OTEL_RESOURCE_ATTRIBUTES + ([#3046](https://github.com/open-telemetry/opentelemetry-python/pull/3046)) - Add missing entry points for OTLP/HTTP exporter ([#3027](https://github.com/open-telemetry/opentelemetry-python/pull/3027)) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 3088dbc5bbf..56707415e5b 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -62,6 +62,7 @@ import sys import typing from json import dumps +from urllib import parse import pkg_resources @@ -289,7 +290,8 @@ def detect(self) -> "Resource": exc, ) continue - env_resource_map[key.strip()] = value.strip() + value_url_decoded = parse.unquote(value.strip()) + env_resource_map[key.strip()] = value_url_decoded service_name = os.environ.get(OTEL_SERVICE_NAME) if service_name: diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index 2d584981e62..19499fe240a 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -492,6 +492,16 @@ def test_invalid_key_value_pairs(self): resources.Resource({"k": "v", "k2": "v2", "foo": "bar=baz"}), ) + def test_multiple_with_url_decode(self): + detector = resources.OTELResourceDetector() + os.environ[ + resources.OTEL_RESOURCE_ATTRIBUTES + ] = "key=value%20test%0A, key2=value+%202" + self.assertEqual( + detector.detect(), + resources.Resource({"key": "value test\n", "key2": "value+ 2"}), + ) + @mock.patch.dict( os.environ, {resources.OTEL_SERVICE_NAME: "test-srv-name"},