Skip to content

Commit 37b41b8

Browse files
committed
chore: collect garbage collector metrics
1 parent 9fddd7a commit 37b41b8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

hathor/prometheus.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16+
import gc
1617
from typing import TYPE_CHECKING
1718

1819
from prometheus_client import CollectorRegistry, Gauge, write_to_textfile
@@ -61,6 +62,13 @@
6162
'total_sst_files_size': 'Storage size in bytes of all SST files of a certain column-family in RocksDB'
6263
}
6364

65+
GC_METRICS = {
66+
'objects': 'Number of objects tracked by the garbage collector',
67+
'collections': 'Number of collections done by the garbage collector',
68+
'collected': 'Number of objects collected by the garbage collector',
69+
'uncollectable': 'Number of objects that could not be collected by the garbage collector',
70+
'threshold': 'Current threshold of the garbage collector',
71+
}
6472

6573
class PrometheusMetricsExporter:
6674
""" Class that sends hathor metrics to a node exporter that will be read by Prometheus
@@ -112,6 +120,7 @@ def _initial_setup(self) -> None:
112120

113121
self._initialize_peer_connection_metrics()
114122
self._initialize_tx_storage_metrics()
123+
self._initialize_garbage_collection_metrics()
115124

116125
for name, comment in METRIC_INFO.items():
117126
self.metric_gauges[name] = Gauge(self.metrics_prefix + name, comment, registry=self.registry)
@@ -147,6 +156,22 @@ def _initialize_tx_storage_metrics(self) -> None:
147156
) for name, description in TX_STORAGE_METRICS.items()
148157
}
149158

159+
def _initialize_garbage_collection_metrics(self) -> None:
160+
"""Initializes the metrics related to garbage collection
161+
"""
162+
gc_labels = ["generation"]
163+
164+
prefix = self.metrics_prefix + "python_gc_"
165+
166+
self.gc_metrics = {
167+
name: Gauge(
168+
prefix + name,
169+
description,
170+
labelnames=gc_labels,
171+
registry=self.registry
172+
) for name, description in GC_METRICS.items()
173+
}
174+
150175
def start(self) -> None:
151176
""" Starts exporter
152177
"""
@@ -161,6 +186,7 @@ def set_new_metrics(self) -> None:
161186

162187
self._set_rocksdb_tx_storage_metrics()
163188
self._set_new_peer_connection_metrics()
189+
self._set_garbage_collection_metrics()
164190

165191
write_to_textfile(self.filepath, self.registry)
166192

@@ -179,6 +205,18 @@ def _set_new_peer_connection_metrics(self) -> None:
179205
connection_string=connection_metric.connection_string
180206
).set(getattr(connection_metric, name))
181207

208+
def _set_garbage_collection_metrics(self) -> None:
209+
counts = gc.get_count()
210+
stats = gc.get_stats()
211+
threshold = gc.get_threshold()
212+
213+
for i in range(3):
214+
self.gc_metrics['objects'].labels(generation=i).set(counts[i])
215+
self.gc_metrics['collections'].labels(generation=i).set(stats[i]['collections'])
216+
self.gc_metrics['collected'].labels(generation=i).set(stats[i]['collected'])
217+
self.gc_metrics['uncollectable'].labels(generation=i).set(stats[i]['uncollectable'])
218+
self.gc_metrics['threshold'].labels(generation=i).set(threshold[i])
219+
182220
def _write_data(self) -> None:
183221
""" Update all metric data with new values
184222
Write new data to file

0 commit comments

Comments
 (0)