feat: add histogram_add; update interface

mem-usage
Jef Roosens 2022-12-26 14:27:43 +01:00
parent 50be02f4a2
commit ab065681aa
2 changed files with 25 additions and 9 deletions

View File

@ -14,7 +14,7 @@ pub mut:
}
[heap]
struct DefaultMetricsCollector {
struct DefaultCollector {
mut:
// The mutex for counters also applies to counter_indexes. Both data
// structuress are only ever updated together. Note that only registering a
@ -25,15 +25,15 @@ mut:
histograms shared map[string]HistogramEntry
}
pub fn new_default_metrics_collector() &DefaultMetricsCollector {
return &DefaultMetricsCollector{
pub fn new_default_collector() &DefaultCollector {
return &DefaultCollector{
counters: []u64{}
counter_indexes: map[string]CounterEntry{}
histograms: map[string]HistogramEntry{}
}
}
pub fn (mut c DefaultMetricsCollector) counter_register(value u64, metric Metric) {
pub fn (mut c DefaultCollector) counter_register(value u64, metric Metric) {
lock c.counters {
c.counters << value
@ -44,7 +44,7 @@ pub fn (mut c DefaultMetricsCollector) counter_register(value u64, metric Metric
}
}
pub fn (mut c DefaultMetricsCollector) counter_increment(metric Metric) {
pub fn (mut c DefaultCollector) counter_increment(metric Metric) {
rlock c.counters {
entry := c.counter_indexes[metric.str()]
@ -52,7 +52,7 @@ pub fn (mut c DefaultMetricsCollector) counter_increment(metric Metric) {
}
}
pub fn (mut c DefaultMetricsCollector) counter_decrement(metric Metric) {
pub fn (mut c DefaultCollector) counter_decrement(metric Metric) {
rlock c.counters {
entry := c.counter_indexes[metric.str()]
@ -60,7 +60,7 @@ pub fn (mut c DefaultMetricsCollector) counter_decrement(metric Metric) {
}
}
pub fn (mut c DefaultMetricsCollector) counter_get(metric Metric) ?u64 {
pub fn (mut c DefaultCollector) counter_get(metric Metric) ?u64 {
return rlock c.counters {
entry := c.counter_indexes[metric.str()] or { return none }
@ -68,7 +68,7 @@ pub fn (mut c DefaultMetricsCollector) counter_get(metric Metric) ?u64 {
}
}
pub fn (mut c DefaultMetricsCollector) histogram_register(metric Metric) {
pub fn (mut c DefaultCollector) histogram_register(metric Metric) {
lock c.histograms {
c.histograms[metric.str()] = HistogramEntry{
metric: metric
@ -77,7 +77,7 @@ pub fn (mut c DefaultMetricsCollector) histogram_register(metric Metric) {
}
}
pub fn (mut c DefaultMetricsCollector) histogram_add(value f64, metric Metric) {
pub fn (mut c DefaultCollector) histogram_add(value f64, metric Metric) {
entry := rlock c.histograms {
c.histograms[metric.str()]
}
@ -86,3 +86,15 @@ pub fn (mut c DefaultMetricsCollector) histogram_add(value f64, metric Metric) {
entry.data << value
}
}
pub fn (mut 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()
}
}

View File

@ -23,4 +23,8 @@ interface MetricsCollector {
counter_register(value u64, metric Metric)
counter_increment(metric Metric)
counter_decrement(metric Metric)
counter_get(metric Metric) ?u64
histogram_register(metric Metric)
histogram_add(value f64, metric Metric)
histogram_get(metric Metric) ?
}