feat: working tests; stuff

mem-usage
Jef Roosens 2022-12-26 15:03:57 +01:00
parent 7f6327b01a
commit a1049943a4
4 changed files with 26 additions and 16 deletions

View File

@ -52,14 +52,6 @@ pub fn (c &DefaultCollector) counter_increment(metric Metric) {
} }
} }
pub fn (c &DefaultCollector) counter_decrement(metric Metric) {
rlock c.counters {
entry := c.counter_indexes[metric.str()]
stdatomic.sub_u64(&c.counters[entry.index], 1)
}
}
pub fn (c &DefaultCollector) counter_get(metric Metric) ?u64 { pub fn (c &DefaultCollector) counter_get(metric Metric) ?u64 {
return rlock c.counters { return rlock c.counters {
entry := c.counter_indexes[metric.str()] or { return none } entry := c.counter_indexes[metric.str()] or { return none }
@ -68,6 +60,18 @@ pub fn (c &DefaultCollector) counter_get(metric Metric) ?u64 {
} }
} }
pub fn (c &DefaultCollector) counters() []Metric {
mut metrics := []Metric{}
rlock c.counters {
for _, entry in c.counter_indexes {
metrics << entry.metric
}
}
return metrics
}
pub fn (mut c DefaultCollector) histogram_register(metric Metric) { pub fn (mut c DefaultCollector) histogram_register(metric Metric) {
lock c.histograms { lock c.histograms {
c.histograms[metric.str()] = HistogramEntry{ c.histograms[metric.str()] = HistogramEntry{

View File

@ -1,8 +1,14 @@
import metrics module metrics
fn test_counter_increment() { fn test_counter_increment() {
mut m := new_default_metrics_collector() mut m := new_default_collector()
m.counter_register(0, 'test') m.counter_register(0, name: 'test')
m.counter_increment('test') m.counter_increment(name: 'test')
assert m.counter_get(name: 'test')? == u64(1)
m.counter_increment(name: 'test')
assert m.counter_get(name: 'test')? == u64(2)
} }

View File

@ -19,11 +19,11 @@ pub fn (m &Metric) str() string {
return '$m.name{${m.labels.map(join_two_array(it)).join(',')}}' return '$m.name{${m.labels.map(join_two_array(it)).join(',')}}'
} }
interface MetricsCollector { pub interface MetricsCollector {
counter_register(value u64, metric Metric) counter_register(value u64, metric Metric)
counter_increment(metric Metric) counter_increment(metric Metric)
counter_decrement(metric Metric)
counter_get(metric Metric) ?u64 counter_get(metric Metric) ?u64
counters() []Metric
histogram_register(metric Metric) histogram_register(metric Metric)
histogram_add(value f64, metric Metric) histogram_add(value f64, metric Metric)
histogram_get(metric Metric) ? histogram_get(metric Metric) ?

4
null.v
View File

@ -11,12 +11,12 @@ pub fn (c &NullCollector) counter_register(value u64, metric Metric) {}
pub fn (c &NullCollector) counter_increment(metric Metric) {} pub fn (c &NullCollector) counter_increment(metric Metric) {}
pub fn (c &NullCollector) counter_decrement(metric Metric) {}
pub fn (c &NullCollector) counter_get(metric Metric) ?u64 { pub fn (c &NullCollector) counter_get(metric Metric) ?u64 {
return none return none
} }
pub fn (c &NullCollector) counters() []Metric { return [] }
pub fn (c &NullCollector) histogram_register(metric Metric) {} pub fn (c &NullCollector) histogram_register(metric Metric) {}
pub fn (c &NullCollector) histogram_add(value f64, metric Metric) {} pub fn (c &NullCollector) histogram_add(value f64, metric Metric) {}