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 1, 2021
1 parent 05e9fe1 commit 8e7f447
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
16 changes: 10 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,17 @@ 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(","):
if not item.find("="):
logger.warning(
"Invalid key value resource attribute pair %s", item
)

key, value = item.split("=", 1)
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
7 changes: 7 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,13 @@ 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,,"
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 8e7f447

Please sign in to comment.