Skip to content

Commit 50cdeee

Browse files
authored
resource-detector-containerid: demote failure to read cgroup files to debug (#3579)
* resource-detector-containerid: demote failure to read cgroup files to debug Make the detection more quiet so we can load it on application not running on linux machines without spamming logs. * Add changelog
1 parent 04f8899 commit 50cdeee

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

CHANGELOG.md

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

1414
## Version 1.34.0/0.55b0 (2025-06-04)
1515

16+
### Fixed
17+
18+
- `opentelemetry-resource-detector-containerid`: make it more quiet on platforms without cgroups
19+
([#3579](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3579))
20+
1621
### Added
1722

1823
- `opentelemetry-instrumentation-aiokafka` Add instrumentation of `consumer.getmany` (batch)

resource/opentelemetry-resource-detector-containerid/src/opentelemetry/resource/detector/containerid/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _get_container_id_v1():
4141
break
4242

4343
except FileNotFoundError as exception:
44-
logger.warning("Failed to get container id. Exception: %s", exception)
44+
logger.debug("Failed to get container id. Exception: %s", exception)
4545
return container_id
4646

4747

@@ -66,7 +66,7 @@ def _get_container_id_v2():
6666
break
6767

6868
except FileNotFoundError as exception:
69-
logger.warning("Failed to get container id. Exception: %s", exception)
69+
logger.debug("Failed to get container id. Exception: %s", exception)
7070
return container_id
7171

7272

resource/opentelemetry-resource-detector-containerid/tests/test_container.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,48 @@ def test_container_id_detect_from_mountinfo_file(
109109
actual.attributes.copy(), MockContainerResourceAttributes
110110
)
111111

112+
@patch(
113+
"opentelemetry.resource.detector.containerid._get_container_id_v1",
114+
return_value=None,
115+
)
116+
@patch(
117+
"builtins.open",
118+
side_effect=FileNotFoundError,
119+
)
120+
def test_cannot_read_mountinfo_file(
121+
self, mock_get_container_id_v1, mock_mountinfo_file
122+
):
123+
with self.assertLogs(
124+
"opentelemetry.resource.detector.containerid", level="DEBUG"
125+
) as cm:
126+
actual = ContainerResourceDetector().detect()
127+
self.assertFalse(actual.attributes.copy())
128+
self.assertIn(
129+
"DEBUG:opentelemetry.resource.detector.containerid:Failed to get container id. Exception: ",
130+
cm.output,
131+
)
132+
133+
@patch(
134+
"opentelemetry.resource.detector.containerid._get_container_id_v2",
135+
return_value=None,
136+
)
137+
@patch(
138+
"builtins.open",
139+
side_effect=FileNotFoundError,
140+
)
141+
def test_cannot_read_cgroup_file(
142+
self, mock_get_container_id_v2, mock_cgroup_file
143+
):
144+
with self.assertLogs(
145+
"opentelemetry.resource.detector.containerid", level="DEBUG"
146+
) as cm:
147+
actual = ContainerResourceDetector().detect()
148+
self.assertFalse(actual.attributes.copy())
149+
self.assertIn(
150+
"DEBUG:opentelemetry.resource.detector.containerid:Failed to get container id. Exception: ",
151+
cm.output,
152+
)
153+
112154
@patch(
113155
"opentelemetry.resource.detector.containerid._get_container_id",
114156
return_value=MockContainerResourceAttributes[

0 commit comments

Comments
 (0)