refactor: make Gauge own struct

mem-usage
Jef Roosens 2023-01-28 16:25:55 +01:00
parent 08d95965af
commit 116b85e039
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 24 additions and 12 deletions

View File

@ -6,6 +6,18 @@ mut:
data u64 data u64
} }
struct Gauge {
metric Metric
pub mut:
data f64
}
struct Histogram {
metric Metric
pub mut:
buckets []f64
}
struct FloatSeries { struct FloatSeries {
metric Metric metric Metric
pub mut: pub mut:
@ -17,14 +29,14 @@ struct DefaultCollector {
mut: mut:
counters shared map[string]&Counter counters shared map[string]&Counter
histograms shared map[string]&FloatSeries histograms shared map[string]&FloatSeries
gauges shared map[string]&FloatSeries gauges shared map[string]&Gauge
} }
pub fn new_default_collector() &DefaultCollector { pub fn new_default_collector() &DefaultCollector {
return &DefaultCollector{ return &DefaultCollector{
counters: map[string]&Counter{} counters: map[string]&Counter{}
histograms: map[string]&FloatSeries{} 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) { pub fn (mut c DefaultCollector) gauge_add(value f64, metric Metric) {
lock c.gauges { lock c.gauges {
mut entry := c.gauges[metric.str()] or { mut entry := c.gauges[metric.str()] or {
gauge := &FloatSeries{ gauge := &Gauge{
metric: metric metric: metric
data: [0.0] data: 0.0
} }
c.gauges[metric.str()] = gauge c.gauges[metric.str()] = gauge
@ -119,16 +131,16 @@ pub fn (mut c DefaultCollector) gauge_add(value f64, metric Metric) {
gauge gauge
} }
entry.data[0] += value entry.data += value
} }
} }
pub fn (mut c DefaultCollector) gauge_sub(value f64, metric Metric) { pub fn (mut c DefaultCollector) gauge_sub(value f64, metric Metric) {
lock c.gauges { lock c.gauges {
mut entry := c.gauges[metric.str()] or { mut entry := c.gauges[metric.str()] or {
gauge := &FloatSeries{ gauge := &Gauge{
metric: metric metric: metric
data: [0.0] data: 0.0
} }
c.gauges[metric.str()] = gauge c.gauges[metric.str()] = gauge
@ -136,16 +148,16 @@ pub fn (mut c DefaultCollector) gauge_sub(value f64, metric Metric) {
gauge gauge
} }
entry.data[0] -= value entry.data -= value
} }
} }
pub fn (mut c DefaultCollector) gauge_set(value f64, metric Metric) { pub fn (mut c DefaultCollector) gauge_set(value f64, metric Metric) {
lock c.gauges { lock c.gauges {
mut entry := c.gauges[metric.str()] or { mut entry := c.gauges[metric.str()] or {
gauge := &FloatSeries{ gauge := &Gauge{
metric: metric metric: metric
data: [0.0] data: 0.0
} }
c.gauges[metric.str()] = gauge c.gauges[metric.str()] = gauge
@ -153,7 +165,7 @@ pub fn (mut c DefaultCollector) gauge_set(value f64, metric Metric) {
gauge 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 { return rlock c.gauges {
entry := c.gauges[metric.str()] or { return none } entry := c.gauges[metric.str()] or { return none }
entry.data[0] entry.data
} }
} }