From 7e39051c1f4f1e3074846087493bea971bc349f8 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 4 Jan 2023 09:07:45 +0100 Subject: [PATCH] feat: exporter name prefix & custom metric serialisation --- metrics.v | 15 ++++----------- prometheus.v | 27 +++++++++++++++++++++------ prometheus_test.v | 16 ++++++++-------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/metrics.v b/metrics.v index fad19f1..d5cd886 100644 --- a/metrics.v +++ b/metrics.v @@ -4,21 +4,13 @@ import io [params] pub struct Metric { +pub: name string [required] labels [][2]string } -[inline] -fn join_two_array(arr [2]string) string { - return '${arr[0]}="${arr[1]}"' -} - pub fn (m &Metric) str() string { - if m.labels.len == 0 { - return m.name - } - - return '$m.name{${m.labels.map(join_two_array(it)).join(',')}}' + return '$m.name $m.labels' } pub interface MetricsCollector { @@ -38,7 +30,8 @@ mut: } pub interface MetricsExporter { - load(collector MetricsCollector) + load(prefix string, collector MetricsCollector) export_to_writer(writer io.Writer) ! export_to_string() !string + serialize_metric(metric Metric) string } diff --git a/prometheus.v b/prometheus.v index 49e70c0..e922ebd 100644 --- a/prometheus.v +++ b/prometheus.v @@ -7,6 +7,7 @@ import arrays pub struct PrometheusExporter { buckets []f64 mut: + prefix string collector &MetricsCollector = unsafe { nil } } @@ -16,10 +17,24 @@ pub fn new_prometheus_exporter(buckets []f64) PrometheusExporter { } } -pub fn (mut e PrometheusExporter) load(collector &MetricsCollector) { +pub fn (mut e PrometheusExporter) load(prefix string, collector &MetricsCollector) { unsafe { e.collector = collector } + e.prefix = prefix +} + +[inline] +fn join_two_array(arr [2]string) string { + return '${arr[0]}="${arr[1]}"' +} + +pub fn (e &PrometheusExporter) serialize_metric(metric Metric) string { + if metric.labels.len == 0 { + return '$e.prefix$metric.name' + } + + return '$e.prefix$metric.name{${metric.labels.map(join_two_array(it)).join(',')}}' } pub fn (mut e PrometheusExporter) export_to_string() !string { @@ -33,14 +48,14 @@ pub fn (mut e PrometheusExporter) export_to_string() !string { pub fn (mut e PrometheusExporter) export_to_writer(mut writer io.Writer) ! { for counter in e.collector.counters() { val := e.collector.counter_get(counter) or { return error("This can't happen.") } - line := '$counter $val\n' + line := '${e.serialize_metric(counter)} $val\n' writer.write(line.bytes())! } for gauge in e.collector.gauges() { val := e.collector.gauge_get(gauge) or { return error("This can't happen.") } - line := '$gauge $val\n' + line := '${e.serialize_metric(gauge)} $val\n' writer.write(line.bytes())! } @@ -72,13 +87,13 @@ pub fn (mut e PrometheusExporter) export_to_writer(mut writer io.Writer) ! { ...hist name: '${hist.name}_count' } - writer.write('$m $total_count\n'.bytes())! + writer.write('${e.serialize_metric(m)} $total_count\n'.bytes())! m = Metric{ ...hist name: '${hist.name}_sum' } - writer.write('$m $sum\n'.bytes())! + writer.write('${e.serialize_metric(m)} $sum\n'.bytes())! mut le_labels := [][2]string{} le_labels.prepend(hist.labels) @@ -92,7 +107,7 @@ pub fn (mut e PrometheusExporter) export_to_writer(mut writer io.Writer) ! { labels: le_labels } - writer.write('$m ${bucket_counts[j]}\n'.bytes())! + writer.write('${e.serialize_metric(m)} ${bucket_counts[j]}\n'.bytes())! } } } diff --git a/prometheus_test.v b/prometheus_test.v index 1cb31b8..d78d315 100644 --- a/prometheus_test.v +++ b/prometheus_test.v @@ -5,9 +5,9 @@ fn test_only_counters() { m.counter_increment(name: 'test') mut e := new_prometheus_exporter([]) - e.load(m) + e.load('hi_', m) - assert e.export_to_string()! == 'test 1\n' + assert e.export_to_string()! == 'hi_test 1\n' metric := Metric{ name: 'test2' @@ -16,7 +16,7 @@ fn test_only_counters() { m.counter_increment(metric) m.counter_increment(metric) - assert e.export_to_string()! == 'test 1\ntest2{hi="label",hi2="label2"} 2\n' + assert e.export_to_string()! == 'hi_test 1\nhi_test2{hi="label",hi2="label2"} 2\n' } fn test_only_gauges() { @@ -24,9 +24,9 @@ fn test_only_gauges() { m.gauge_set(3.25, name: 'test') mut e := new_prometheus_exporter([]) - e.load(m) + e.load('hi_', m) - assert e.export_to_string()! == 'test 3.25\n' + assert e.export_to_string()! == 'hi_test 3.25\n' metric := Metric{ name: 'test2' @@ -34,7 +34,7 @@ fn test_only_gauges() { } m.gauge_add(2.5, metric) - assert e.export_to_string()! == 'test 3.25\ntest2{hi="label",hi2="label2"} 2.5\n' + assert e.export_to_string()! == 'hi_test 3.25\nhi_test2{hi="label",hi2="label2"} 2.5\n' } fn test_single_histogram() { @@ -43,7 +43,7 @@ fn test_single_histogram() { m.histogram_record(5.0, name: 'test') mut e := new_prometheus_exporter([0.5, 5.0]) - e.load(m) + e.load('hi_', m) - assert e.export_to_string()! == 'test_count 1\ntest_sum 5.0\ntest_bucket{le="0.5"} 0\ntest_bucket{le="5.0"} 1\n' + assert e.export_to_string()! == 'hi_test_count 1\nhi_test_sum 5.0\nhi_test_bucket{le="0.5"} 0\nhi_test_bucket{le="5.0"} 1\n' }