Skip to content

Commit

Permalink
Filter invalid resource attribute pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanielRN committed Nov 8, 2021
1 parent a1903c9 commit 2fb6b61
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1887](https://github.com/open-telemetry/opentelemetry-python/pull/1887))
- Make batch processor fork aware and reinit when needed
([#2242](https://github.com/open-telemetry/opentelemetry-python/pull/2242))
- `opentelemetry-sdk` Sanitize env var resource attribute pairs
([#2256](https://github.com/open-telemetry/opentelemetry-python/pull/2256))

## [1.6.2-0.25b2](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.6.2-0.25b2) - 2021-10-19

Expand Down
19 changes: 13 additions & 6 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,20 @@ class OTELResourceDetector(ResourceDetector):
def detect(self) -> "Resource":
env_resources_items = os.environ.get(OTEL_RESOURCE_ATTRIBUTES)
env_resource_map = {}

if env_resources_items:
env_resource_map = {
key.strip(): value.strip()
for key, value in (
item.split("=") for item in env_resources_items.split(",")
)
}
for item in env_resources_items.split(","):
try:
key, value = item.split("=")
except ValueError as exc:
logger.warning(
"Invalid key value resource attribute pair %s: %s",
item,
exc,
)
continue
env_resource_map[key.strip()] = value.strip()

service_name = os.environ.get(OTEL_SERVICE_NAME)
if service_name:
env_resource_map[SERVICE_NAME] = service_name
Expand Down
9 changes: 9 additions & 0 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,15 @@ def test_multiple_with_whitespace(self):
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
)

def test_invalid_key_value_pairs(self):
detector = resources.OTELResourceDetector()
os.environ[
resources.OTEL_RESOURCE_ATTRIBUTES
] = "k=v,k2=v2,invalid,,foo=bar=baz,"
self.assertEqual(
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
)

@mock.patch.dict(
os.environ,
{resources.OTEL_SERVICE_NAME: "test-srv-name"},
Expand Down

0 comments on commit 2fb6b61

Please sign in to comment.