2022-12-26 13:31:50 +01:00
|
|
|
module metrics
|
|
|
|
|
2022-12-26 18:17:27 +01:00
|
|
|
struct Counter {
|
2022-12-26 13:31:50 +01:00
|
|
|
metric Metric
|
2022-12-27 09:03:41 +01:00
|
|
|
mut:
|
|
|
|
data u64
|
2022-12-26 13:31:50 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 16:25:55 +01:00
|
|
|
struct Gauge {
|
|
|
|
metric Metric
|
|
|
|
pub mut:
|
|
|
|
data f64
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Histogram {
|
|
|
|
metric Metric
|
|
|
|
pub mut:
|
2023-01-28 17:21:59 +01:00
|
|
|
total_count u64
|
|
|
|
sum f64
|
|
|
|
buckets []f64
|
|
|
|
bucket_counts []u64
|
2022-12-26 14:18:31 +01:00
|
|
|
}
|
|
|
|
|
2022-12-26 13:31:50 +01:00
|
|
|
[heap]
|
2022-12-26 14:27:43 +01:00
|
|
|
struct DefaultCollector {
|
2022-12-26 13:31:50 +01:00
|
|
|
mut:
|
2023-01-28 17:21:59 +01:00
|
|
|
buckets map[string][]f64
|
2022-12-26 18:17:27 +01:00
|
|
|
counters shared map[string]&Counter
|
2023-01-28 17:21:59 +01:00
|
|
|
histograms shared map[string]&Histogram
|
2023-01-28 16:25:55 +01:00
|
|
|
gauges shared map[string]&Gauge
|
2022-12-26 13:31:50 +01:00
|
|
|
}
|
|
|
|
|
2022-12-26 14:27:43 +01:00
|
|
|
pub fn new_default_collector() &DefaultCollector {
|
|
|
|
return &DefaultCollector{
|
2023-01-28 17:21:59 +01:00
|
|
|
buckets: map[string][]f64{}
|
2022-12-26 18:17:27 +01:00
|
|
|
counters: map[string]&Counter{}
|
2023-01-28 17:21:59 +01:00
|
|
|
histograms: map[string]&Histogram{}
|
2023-01-28 16:25:55 +01:00
|
|
|
gauges: map[string]&Gauge{}
|
2022-12-26 13:31:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
pub fn (mut c DefaultCollector) counter_increment(metric Metric) {
|
2022-12-26 14:18:31 +01:00
|
|
|
lock c.counters {
|
2022-12-27 09:03:41 +01:00
|
|
|
mut entry := c.counters[metric.str()] or {
|
|
|
|
counter := &Counter{
|
|
|
|
metric: metric
|
|
|
|
data: 0
|
|
|
|
}
|
|
|
|
c.counters[metric.str()] = counter
|
|
|
|
|
|
|
|
counter
|
2022-12-26 13:31:50 +01:00
|
|
|
}
|
2022-12-27 09:03:41 +01:00
|
|
|
|
|
|
|
entry.data += 1
|
2022-12-26 13:31:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
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
|
|
|
|
}
|
2022-12-26 14:18:31 +01:00
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
entry.data = value
|
2022-12-26 13:31:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-26 14:32:19 +01:00
|
|
|
pub fn (c &DefaultCollector) counter_get(metric Metric) ?u64 {
|
2022-12-26 14:18:31 +01:00
|
|
|
return rlock c.counters {
|
2022-12-26 18:17:27 +01:00
|
|
|
entry := c.counters[metric.str()] or { return none }
|
2022-12-26 14:18:31 +01:00
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
entry.data
|
2022-12-26 14:18:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-26 15:03:57 +01:00
|
|
|
pub fn (c &DefaultCollector) counters() []Metric {
|
2022-12-26 18:17:27 +01:00
|
|
|
mut metrics := []Metric{}
|
2022-12-26 15:03:57 +01:00
|
|
|
|
2022-12-26 18:17:27 +01:00
|
|
|
rlock c.counters {
|
|
|
|
for _, entry in c.counters {
|
|
|
|
metrics << entry.metric
|
|
|
|
}
|
|
|
|
}
|
2022-12-26 15:03:57 +01:00
|
|
|
|
2022-12-26 18:17:27 +01:00
|
|
|
return metrics
|
2022-12-26 15:03:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 17:21:59 +01:00
|
|
|
pub fn (mut c DefaultCollector) histogram_buckets_set(name string, buckets []f64) {
|
|
|
|
lock c.histograms {
|
|
|
|
c.buckets[name] = buckets
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c DefaultCollector) histogram_record(value f64, metric Metric) {
|
2022-12-26 14:18:31 +01:00
|
|
|
lock c.histograms {
|
2022-12-27 09:03:41 +01:00
|
|
|
mut entry := c.histograms[metric.str()] or {
|
2023-01-28 17:21:59 +01:00
|
|
|
buckets := c.buckets[metric.name] or { [] }
|
|
|
|
hist := &Histogram{
|
2022-12-27 09:03:41 +01:00
|
|
|
metric: metric
|
2023-01-28 17:21:59 +01:00
|
|
|
buckets: buckets
|
|
|
|
bucket_counts: []u64{len: buckets.len, init: 0}
|
2022-12-27 09:03:41 +01:00
|
|
|
}
|
|
|
|
c.histograms[metric.str()] = hist
|
|
|
|
|
|
|
|
hist
|
2022-12-26 14:18:31 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 17:21:59 +01:00
|
|
|
entry.sum += value
|
|
|
|
entry.total_count += 1
|
|
|
|
|
|
|
|
mut i := entry.buckets.len - 1
|
|
|
|
|
|
|
|
for i >= 0 && value <= entry.buckets[i] {
|
|
|
|
entry.bucket_counts[i]++
|
|
|
|
|
|
|
|
i -= 1
|
|
|
|
}
|
2022-12-26 14:18:31 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-26 14:27:43 +01:00
|
|
|
|
2023-01-28 17:21:59 +01:00
|
|
|
pub fn (c &DefaultCollector) histogram_get(metric Metric) ?Histogram {
|
2022-12-27 09:03:41 +01:00
|
|
|
return rlock c.histograms {
|
|
|
|
entry := c.histograms[metric.str()] or { return none }
|
2022-12-26 14:27:43 +01:00
|
|
|
|
|
|
|
// Return a clone of the data to prevent user from altering
|
|
|
|
// internal structure
|
2023-01-28 17:21:59 +01:00
|
|
|
Histogram{
|
|
|
|
metric: metric
|
|
|
|
total_count: entry.total_count
|
|
|
|
sum: entry.sum
|
|
|
|
buckets: entry.buckets.clone()
|
|
|
|
bucket_counts: entry.bucket_counts.clone()
|
|
|
|
}
|
2022-12-26 14:27:43 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
pub fn (mut c DefaultCollector) gauge_add(value f64, metric Metric) {
|
2022-12-26 18:17:27 +01:00
|
|
|
lock c.gauges {
|
2022-12-27 09:03:41 +01:00
|
|
|
mut entry := c.gauges[metric.str()] or {
|
2023-01-28 16:25:55 +01:00
|
|
|
gauge := &Gauge{
|
2022-12-27 09:03:41 +01:00
|
|
|
metric: metric
|
2023-01-28 16:25:55 +01:00
|
|
|
data: 0.0
|
2022-12-27 09:03:41 +01:00
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
c.gauges[metric.str()] = gauge
|
|
|
|
|
|
|
|
gauge
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2023-01-28 16:25:55 +01:00
|
|
|
entry.data += value
|
2022-12-26 18:17:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
pub fn (mut c DefaultCollector) gauge_sub(value f64, metric Metric) {
|
|
|
|
lock c.gauges {
|
|
|
|
mut entry := c.gauges[metric.str()] or {
|
2023-01-28 16:25:55 +01:00
|
|
|
gauge := &Gauge{
|
2022-12-27 09:03:41 +01:00
|
|
|
metric: metric
|
2023-01-28 16:25:55 +01:00
|
|
|
data: 0.0
|
2022-12-27 09:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c.gauges[metric.str()] = gauge
|
|
|
|
|
|
|
|
gauge
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2023-01-28 16:25:55 +01:00
|
|
|
entry.data -= value
|
2022-12-26 18:17:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
pub fn (mut c DefaultCollector) gauge_set(value f64, metric Metric) {
|
|
|
|
lock c.gauges {
|
|
|
|
mut entry := c.gauges[metric.str()] or {
|
2023-01-28 16:25:55 +01:00
|
|
|
gauge := &Gauge{
|
2022-12-27 09:03:41 +01:00
|
|
|
metric: metric
|
2023-01-28 16:25:55 +01:00
|
|
|
data: 0.0
|
2022-12-27 09:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c.gauges[metric.str()] = gauge
|
|
|
|
|
|
|
|
gauge
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2023-01-28 16:25:55 +01:00
|
|
|
entry.data = value
|
2022-12-26 18:17:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c &DefaultCollector) gauge_get(metric Metric) ?f64 {
|
2022-12-27 09:03:41 +01:00
|
|
|
return rlock c.gauges {
|
|
|
|
entry := c.gauges[metric.str()] or { return none }
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2023-01-28 16:25:55 +01:00
|
|
|
entry.data
|
2022-12-26 18:17:27 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-26 21:49:07 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|