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 e9c9649
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2207](https://github.com/open-telemetry/opentelemetry-python/pull/2207))
- remove `X-B3-ParentSpanId` for B3 propagator as per OpenTelemetry specification
([#2237](https://github.com/open-telemetry/opentelemetry-python/pull/2237))
- `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
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 e9c9649

Please sign in to comment.