2022-12-26 13:31:50 +01:00
|
|
|
module metrics
|
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
import io
|
|
|
|
|
2022-12-26 13:31:50 +01:00
|
|
|
[params]
|
|
|
|
pub struct Metric {
|
|
|
|
name string [required]
|
|
|
|
labels [][2]string
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn join_two_array(arr [2]string) string {
|
2022-12-26 21:49:07 +01:00
|
|
|
return '${arr[0]}="${arr[1]}"'
|
2022-12-26 13:31:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (m &Metric) str() string {
|
|
|
|
if m.labels.len == 0 {
|
|
|
|
return m.name
|
|
|
|
}
|
|
|
|
|
|
|
|
return '$m.name{${m.labels.map(join_two_array(it)).join(',')}}'
|
|
|
|
}
|
|
|
|
|
2022-12-26 15:03:57 +01:00
|
|
|
pub interface MetricsCollector {
|
2022-12-26 14:27:43 +01:00
|
|
|
counter_get(metric Metric) ?u64
|
2022-12-26 18:17:27 +01:00
|
|
|
counters() []Metric
|
|
|
|
histogram_get(metric Metric) ?[]f64
|
2022-12-26 21:49:07 +01:00
|
|
|
histograms() []Metric
|
2022-12-26 18:17:27 +01:00
|
|
|
gauge_get(metric Metric) ?f64
|
2022-12-26 21:49:07 +01:00
|
|
|
gauges() []Metric
|
2022-12-26 18:17:27 +01:00
|
|
|
mut:
|
2022-12-27 09:03:41 +01:00
|
|
|
counter_set(val u64, metric Metric)
|
|
|
|
counter_increment(metric Metric)
|
|
|
|
histogram_record(value f64, metric Metric)
|
|
|
|
gauge_set(value f64, metric Metric)
|
|
|
|
gauge_add(value f64, metric Metric)
|
|
|
|
gauge_sub(value f64, metric Metric)
|
2022-12-26 13:31:50 +01:00
|
|
|
}
|
2022-12-26 21:49:07 +01:00
|
|
|
|
|
|
|
pub interface MetricsExporter {
|
|
|
|
load(collector MetricsCollector)
|
|
|
|
export_to_writer(writer io.Writer) !
|
|
|
|
export_to_string() !string
|
|
|
|
}
|