Skip to content

Commit 4abb491

Browse files
committed
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.
1 parent 59cc34e commit 4abb491

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

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)