metrics/collector.v

172 lines
3.0 KiB
Coq
Raw Normal View History

2022-12-26 13:31:50 +01:00
module metrics
import sync.stdatomic
2022-12-26 18:17:27 +01:00
struct Counter {
2022-12-26 13:31:50 +01:00
metric Metric
2022-12-26 18:17:27 +01:00
data u64
2022-12-26 13:31:50 +01:00
}
2022-12-26 18:17:27 +01:00
struct FloatSeries {
metric Metric
pub mut:
data shared []f64
}
2022-12-26 13:31:50 +01:00
[heap]
struct DefaultCollector {
2022-12-26 13:31:50 +01:00
mut:
2022-12-26 18:17:27 +01:00
counters shared map[string]&Counter
histograms shared map[string]&FloatSeries
gauges shared map[string]&FloatSeries
2022-12-26 13:31:50 +01:00
}
pub fn new_default_collector() &DefaultCollector {
return &DefaultCollector{
2022-12-26 18:17:27 +01:00
counters: map[string]&Counter{}
histograms: map[string]&FloatSeries{}
gauges: map[string]&FloatSeries{}
2022-12-26 13:31:50 +01:00
}
}
pub fn (mut c DefaultCollector) counter_register(value u64, metric Metric) {
lock c.counters {
2022-12-26 18:17:27 +01:00
c.counters[metric.str()] = &Counter{
2022-12-26 13:31:50 +01:00
metric: metric
2022-12-26 18:17:27 +01:00
data: value
2022-12-26 13:31:50 +01:00
}
}
}
2022-12-26 14:32:19 +01:00
pub fn (c &DefaultCollector) counter_increment(metric Metric) {
2022-12-26 13:31:50 +01:00
rlock c.counters {
2022-12-26 18:17:27 +01:00
entry := c.counters[metric.str()]
2022-12-26 18:17:27 +01:00
stdatomic.add_u64(&entry.data, 1)
2022-12-26 13:31:50 +01:00
}
}
2022-12-26 14:32:19 +01:00
pub fn (c &DefaultCollector) counter_get(metric Metric) ?u64 {
return rlock c.counters {
2022-12-26 18:17:27 +01:00
entry := c.counters[metric.str()] or { return none }
2022-12-26 18:17:27 +01:00
stdatomic.load_u64(&entry.data)
}
}
2022-12-26 15:03:57 +01:00
pub fn (c &DefaultCollector) counters() []Metric {
2022-12-26 18:17:27 +01:00
mut metrics := []Metric{}
2022-12-26 15:03:57 +01:00
2022-12-26 18:17:27 +01:00
rlock c.counters {
for _, entry in c.counters {
metrics << entry.metric
}
}
2022-12-26 15:03:57 +01:00
2022-12-26 18:17:27 +01:00
return metrics
2022-12-26 15:03:57 +01:00
}
pub fn (mut c DefaultCollector) histogram_register(metric Metric) {
lock c.histograms {
2022-12-26 18:17:27 +01:00
c.histograms[metric.str()] = &FloatSeries{
metric: metric
data: []f64{}
}
}
}
2022-12-26 18:17:27 +01:00
pub fn (c &DefaultCollector) histogram_record(value f64, metric Metric) {
entry := rlock c.histograms {
c.histograms[metric.str()]
}
lock entry.data {
entry.data << value
}
}
2022-12-26 14:32:19 +01:00
pub fn (c &DefaultCollector) histogram_get(metric Metric) ?[]f64 {
entry := rlock c.histograms {
c.histograms[metric.str()] or { return none }
}
return rlock entry.data {
// Return a clone of the data to prevent user from altering
// internal structure
entry.data.clone()
}
}
2022-12-26 18:17:27 +01:00
pub fn (mut c DefaultCollector) gauge_register(value f64, metric Metric) {
lock c.gauges {
c.gauges[metric.str()] = &FloatSeries{
metric: metric
data: [value]
}
}
}
pub fn (c &DefaultCollector) gauge_add(value f64, metric Metric) {
entry := rlock c.gauges {
c.gauges[metric.str()]
}
lock entry.data {
entry.data[0] += value
}
}
pub fn (c &DefaultCollector) gauge_sub(value f64, metric Metric) {
entry := rlock c.gauges {
c.gauges[metric.str()]
}
lock entry.data {
entry.data[0] -= value
}
}
pub fn (c &DefaultCollector) gauge_set(value f64, metric Metric) {
entry := rlock c.gauges {
c.gauges[metric.str()]
}
lock entry.data {
entry.data[0] = value
}
}
pub fn (c &DefaultCollector) gauge_get(metric Metric) ?f64 {
entry := rlock c.gauges {
c.gauges[metric.str()]
}
return rlock entry.data {
entry.data[0]
}
}
2022-12-26 21:49:07 +01:00
pub fn (c &DefaultCollector) histograms() []Metric {
mut metrics := []Metric{}
rlock c.histograms {
for _, entry in c.histograms {
metrics << entry.metric
}
}
return metrics
}
pub fn (c &DefaultCollector) gauges() []Metric {
mut metrics := []Metric{}
rlock c.gauges {
for _, entry in c.gauges {
metrics << entry.metric
}
}
return metrics
}