13
13
# limitations under the License.
14
14
15
15
import os
16
+ import gc
16
17
from typing import TYPE_CHECKING
17
18
18
19
from prometheus_client import CollectorRegistry , Gauge , write_to_textfile
61
62
'total_sst_files_size' : 'Storage size in bytes of all SST files of a certain column-family in RocksDB'
62
63
}
63
64
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
+ }
64
72
65
73
class PrometheusMetricsExporter :
66
74
""" Class that sends hathor metrics to a node exporter that will be read by Prometheus
@@ -112,6 +120,7 @@ def _initial_setup(self) -> None:
112
120
113
121
self ._initialize_peer_connection_metrics ()
114
122
self ._initialize_tx_storage_metrics ()
123
+ self ._initialize_garbage_collection_metrics ()
115
124
116
125
for name , comment in METRIC_INFO .items ():
117
126
self .metric_gauges [name ] = Gauge (self .metrics_prefix + name , comment , registry = self .registry )
@@ -147,6 +156,22 @@ def _initialize_tx_storage_metrics(self) -> None:
147
156
) for name , description in TX_STORAGE_METRICS .items ()
148
157
}
149
158
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
+
150
175
def start (self ) -> None :
151
176
""" Starts exporter
152
177
"""
@@ -161,6 +186,7 @@ def set_new_metrics(self) -> None:
161
186
162
187
self ._set_rocksdb_tx_storage_metrics ()
163
188
self ._set_new_peer_connection_metrics ()
189
+ self ._set_garbage_collection_metrics ()
164
190
165
191
write_to_textfile (self .filepath , self .registry )
166
192
@@ -179,6 +205,18 @@ def _set_new_peer_connection_metrics(self) -> None:
179
205
connection_string = connection_metric .connection_string
180
206
).set (getattr (connection_metric , name ))
181
207
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
+
182
220
def _write_data (self ) -> None :
183
221
""" Update all metric data with new values
184
222
Write new data to file
0 commit comments