From 116b85e039f5ed4628f2601853c525f2ecafb019 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 28 Jan 2023 16:25:55 +0100 Subject: [PATCH] refactor: make Gauge own struct --- collector.v | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/collector.v b/collector.v index 149095b..39c2b42 100644 --- a/collector.v +++ b/collector.v @@ -6,6 +6,18 @@ mut: data u64 } +struct Gauge { + metric Metric +pub mut: + data f64 +} + +struct Histogram { + metric Metric +pub mut: + buckets []f64 +} + struct FloatSeries { metric Metric pub mut: @@ -17,14 +29,14 @@ struct DefaultCollector { mut: counters shared map[string]&Counter histograms shared map[string]&FloatSeries - gauges shared map[string]&FloatSeries + gauges shared map[string]&Gauge } pub fn new_default_collector() &DefaultCollector { return &DefaultCollector{ counters: map[string]&Counter{} histograms: map[string]&FloatSeries{} - gauges: map[string]&FloatSeries{} + gauges: map[string]&Gauge{} } } @@ -109,9 +121,9 @@ pub fn (c &DefaultCollector) histogram_get(metric Metric) ?[]f64 { pub fn (mut c DefaultCollector) gauge_add(value f64, metric Metric) { lock c.gauges { mut entry := c.gauges[metric.str()] or { - gauge := &FloatSeries{ + gauge := &Gauge{ metric: metric - data: [0.0] + data: 0.0 } c.gauges[metric.str()] = gauge @@ -119,16 +131,16 @@ pub fn (mut c DefaultCollector) gauge_add(value f64, metric Metric) { gauge } - entry.data[0] += value + entry.data += value } } pub fn (mut c DefaultCollector) gauge_sub(value f64, metric Metric) { lock c.gauges { mut entry := c.gauges[metric.str()] or { - gauge := &FloatSeries{ + gauge := &Gauge{ metric: metric - data: [0.0] + data: 0.0 } c.gauges[metric.str()] = gauge @@ -136,16 +148,16 @@ pub fn (mut c DefaultCollector) gauge_sub(value f64, metric Metric) { gauge } - entry.data[0] -= value + entry.data -= value } } pub fn (mut c DefaultCollector) gauge_set(value f64, metric Metric) { lock c.gauges { mut entry := c.gauges[metric.str()] or { - gauge := &FloatSeries{ + gauge := &Gauge{ metric: metric - data: [0.0] + data: 0.0 } c.gauges[metric.str()] = gauge @@ -153,7 +165,7 @@ pub fn (mut c DefaultCollector) gauge_set(value f64, metric Metric) { gauge } - entry.data[0] = value + entry.data = value } } @@ -161,7 +173,7 @@ pub fn (c &DefaultCollector) gauge_get(metric Metric) ?f64 { return rlock c.gauges { entry := c.gauges[metric.str()] or { return none } - entry.data[0] + entry.data } }