12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import gc
15
16
import os
16
17
from typing import TYPE_CHECKING
17
18
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
+ }
72
+
64
73
65
74
class PrometheusMetricsExporter :
66
75
""" Class that sends hathor metrics to a node exporter that will be read by Prometheus
@@ -112,6 +121,7 @@ def _initial_setup(self) -> None:
112
121
113
122
self ._initialize_peer_connection_metrics ()
114
123
self ._initialize_tx_storage_metrics ()
124
+ self ._initialize_garbage_collection_metrics ()
115
125
116
126
for name , comment in METRIC_INFO .items ():
117
127
self .metric_gauges [name ] = Gauge (self .metrics_prefix + name , comment , registry = self .registry )
@@ -147,6 +157,22 @@ def _initialize_tx_storage_metrics(self) -> None:
147
157
) for name , description in TX_STORAGE_METRICS .items ()
148
158
}
149
159
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
+
150
176
def start (self ) -> None :
151
177
""" Starts exporter
152
178
"""
@@ -161,6 +187,7 @@ def set_new_metrics(self) -> None:
161
187
162
188
self ._set_rocksdb_tx_storage_metrics ()
163
189
self ._set_new_peer_connection_metrics ()
190
+ self ._set_garbage_collection_metrics ()
164
191
165
192
write_to_textfile (self .filepath , self .registry )
166
193
@@ -179,6 +206,18 @@ def _set_new_peer_connection_metrics(self) -> None:
179
206
connection_string = connection_metric .connection_string
180
207
).set (getattr (connection_metric , name ))
181
208
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
+
182
221
def _write_data (self ) -> None :
183
222
""" Update all metric data with new values
184
223
Write new data to file
0 commit comments