Skip to content

Commit d2d136d

Browse files
authored
Update garbage collection metrics to use get_stats to align with semconv (#3661)
1 parent 9bf77b5 commit d2d136d

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,10 +905,10 @@ def _get_runtime_gc_collections(
905905
self, options: CallbackOptions
906906
) -> Iterable[Observation]:
907907
"""Observer callback for garbage collection"""
908-
for index, count in enumerate(gc.get_count()):
908+
for index, stat in enumerate(gc.get_stats()):
909909
self._runtime_gc_collections_labels["generation"] = str(index)
910910
yield Observation(
911-
count, self._runtime_gc_collections_labels.copy()
911+
stat["collections"], self._runtime_gc_collections_labels.copy()
912912
)
913913

914914
def _get_runtime_thread_count(

instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -959,16 +959,24 @@ def test_runtime_get_count(self, mock_gc_get_count):
959959
expected_gc_count,
960960
)
961961

962-
@mock.patch("gc.get_count")
962+
@mock.patch("gc.get_stats")
963963
@skipIf(
964964
python_implementation().lower() == "pypy", "not supported for pypy"
965965
)
966-
def test_runtime_get_gc_collections(self, mock_gc_get_count):
967-
mock_gc_get_count.configure_mock(**{"return_value": (1, 2, 3)})
966+
def test_runtime_get_gc_collections(self, mock_gc_get_stats):
967+
mock_gc_get_stats.configure_mock(
968+
**{
969+
"return_value": [
970+
{"collections": 10, "collected": 100, "uncollectable": 1},
971+
{"collections": 20, "collected": 200, "uncollectable": 2},
972+
{"collections": 30, "collected": 300, "uncollectable": 3},
973+
]
974+
}
975+
)
968976
expected_gc_collections = [
969-
_SystemMetricsResult({"generation": "0"}, 1),
970-
_SystemMetricsResult({"generation": "1"}, 2),
971-
_SystemMetricsResult({"generation": "2"}, 3),
977+
_SystemMetricsResult({"generation": "0"}, 10),
978+
_SystemMetricsResult({"generation": "1"}, 20),
979+
_SystemMetricsResult({"generation": "2"}, 30),
972980
]
973981
self._test_metrics(
974982
"cpython.gc.collections",

0 commit comments

Comments
 (0)