metrics/collector.v

191 lines
3.2 KiB
V

module metrics
struct Counter {
metric Metric
mut:
data u64
}
struct FloatSeries {
metric Metric
pub mut:
data []f64
}
[heap]
struct DefaultCollector {
mut:
counters shared map[string]&Counter
histograms shared map[string]&FloatSeries
gauges shared map[string]&FloatSeries
}
pub fn new_default_collector() &DefaultCollector {
return &DefaultCollector{
counters: map[string]&Counter{}
histograms: map[string]&FloatSeries{}
gauges: map[string]&FloatSeries{}
}
}
pub fn (mut c DefaultCollector) counter_increment(metric Metric) {
lock c.counters {
mut entry := c.counters[metric.str()] or {
counter := &Counter{
metric: metric
data: 0
}
c.counters[metric.str()] = counter
counter
}
entry.data += 1
}
}
pub fn (mut c DefaultCollector) counter_set(value u64, metric Metric) {
lock c.counters {
mut entry := c.counters[metric.str()] or {
counter := &Counter{
metric: metric
data: 0
}
c.counters[metric.str()] = counter
counter
}
entry.data = value
}
}
pub fn (c &DefaultCollector) counter_get(metric Metric) ?u64 {
return rlock c.counters {
entry := c.counters[metric.str()] or { return none }
entry.data
}
}
pub fn (c &DefaultCollector) counters() []Metric {
mut metrics := []Metric{}
rlock c.counters {
for _, entry in c.counters {
metrics << entry.metric
}
}
return metrics
}
pub fn (c &DefaultCollector) histogram_record(value f64, metric Metric) {
lock c.histograms {
mut entry := c.histograms[metric.str()] or {
hist := &FloatSeries{
metric: metric
data: []f64{}
}
c.histograms[metric.str()] = hist
hist
}
entry.data << value
}
}
pub fn (c &DefaultCollector) histogram_get(metric Metric) ?[]f64 {
return rlock c.histograms {
entry := c.histograms[metric.str()] or { return none }
// Return a clone of the data to prevent user from altering
// internal structure
entry.data.clone()
}
}
pub fn (mut c DefaultCollector) gauge_add(value f64, metric Metric) {
lock c.gauges {
mut entry := c.gauges[metric.str()] or {
gauge := &FloatSeries{
metric: metric
data: [0.0]
}
c.gauges[metric.str()] = gauge
gauge
}
entry.data[0] += value
}
}
pub fn (mut c DefaultCollector) gauge_sub(value f64, metric Metric) {
lock c.gauges {
mut entry := c.gauges[metric.str()] or {
gauge := &FloatSeries{
metric: metric
data: [0.0]
}
c.gauges[metric.str()] = gauge
gauge
}
entry.data[0] -= value
}
}
pub fn (mut c DefaultCollector) gauge_set(value f64, metric Metric) {
lock c.gauges {
mut entry := c.gauges[metric.str()] or {
gauge := &FloatSeries{
metric: metric
data: [0.0]
}
c.gauges[metric.str()] = gauge
gauge
}
entry.data[0] = value
}
}
pub fn (c &DefaultCollector) gauge_get(metric Metric) ?f64 {
return rlock c.gauges {
entry := c.gauges[metric.str()] or { return none }
entry.data[0]
}
}
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
}