Skip to content

Commit 22f478c

Browse files
authored
chore: collect garbage collector metrics (#1137)
1 parent 8da62a1 commit 22f478c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

hathor/prometheus.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import gc
1516
import os
1617
from typing import TYPE_CHECKING
1718

@@ -61,6 +62,14 @@
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+
}
72+
6473

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

113122
self._initialize_peer_connection_metrics()
114123
self._initialize_tx_storage_metrics()
124+
self._initialize_garbage_collection_metrics()
115125

116126
for name, comment in METRIC_INFO.items():
117127
self.metric_gauges[name] = Gauge(self.metrics_prefix + name, comment, registry=self.registry)
@@ -147,6 +157,22 @@ def _initialize_tx_storage_metrics(self) -> None:
147157
) for name, description in TX_STORAGE_METRICS.items()
148158
}
149159

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

162188
self._set_rocksdb_tx_storage_metrics()
163189
self._set_new_peer_connection_metrics()
190+
self._set_garbage_collection_metrics()
164191

165192
write_to_textfile(self.filepath, self.registry)
166193

@@ -179,6 +206,18 @@ def _set_new_peer_connection_metrics(self) -> None:
179206
connection_string=connection_metric.connection_string
180207
).set(getattr(connection_metric, name))
181208

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

0 commit comments

Comments
 (0)