From ae2eeeb97415cc0c251e44f19445fbedd0bd368b Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 28 Jan 2023 17:32:57 +0100 Subject: [PATCH 1/3] fix: add buckets_set function to interface --- metrics.v | 1 + null.v | 2 ++ prometheus_test.v | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/metrics.v b/metrics.v index 7f54e4a..6fdc534 100644 --- a/metrics.v +++ b/metrics.v @@ -23,6 +23,7 @@ pub interface MetricsCollector { mut: counter_set(val u64, metric Metric) counter_increment(metric Metric) + histogram_buckets_set(name string, buckets []f64) histogram_record(value f64, metric Metric) gauge_set(value f64, metric Metric) gauge_add(value f64, metric Metric) diff --git a/null.v b/null.v index 300a208..257547f 100644 --- a/null.v +++ b/null.v @@ -19,6 +19,8 @@ pub fn (c &NullCollector) counters() []Metric { return [] } +pub fn (mut c DefaultCollector) histogram_buckets_set(name string, buckets []f64) {} + pub fn (c &NullCollector) histogram_record(value f64, metric Metric) {} pub fn (c &NullCollector) histogram_get(metric Metric) ?Histogram { diff --git a/prometheus_test.v b/prometheus_test.v index de1bfe9..d849327 100644 --- a/prometheus_test.v +++ b/prometheus_test.v @@ -40,7 +40,7 @@ fn test_only_gauges() { fn test_single_histogram() { mut m := new_default_collector() - m.histogram_buckets_set('test', [0.5, 5.0]) + m.histogram_buckets_set('test', [0.5, 5.0]) m.histogram_record(5.0, name: 'test') m.histogram_record(7.0, name: 'test') From 0df06647b3f83e7810859ad46b35c03309d6b91d Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 28 Jan 2023 17:33:48 +0100 Subject: [PATCH 2/3] fix: i'm dumb --- null.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/null.v b/null.v index 257547f..46d40b4 100644 --- a/null.v +++ b/null.v @@ -19,7 +19,7 @@ pub fn (c &NullCollector) counters() []Metric { return [] } -pub fn (mut c DefaultCollector) histogram_buckets_set(name string, buckets []f64) {} +pub fn (c &NullCollector) histogram_buckets_set(name string, buckets []f64) {} pub fn (c &NullCollector) histogram_record(value f64, metric Metric) {} From 79079bbcd46371525c5310bebee181ca519731f6 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 8 Feb 2023 10:45:43 +0100 Subject: [PATCH 3/3] chore: ran v fmt for v 0.3.3 changes --- metrics.v | 2 +- prometheus.v | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/metrics.v b/metrics.v index 6fdc534..1ecd33c 100644 --- a/metrics.v +++ b/metrics.v @@ -10,7 +10,7 @@ pub: } pub fn (m &Metric) str() string { - return '$m.name $m.labels' + return '${m.name} ${m.labels}' } pub interface MetricsCollector { diff --git a/prometheus.v b/prometheus.v index b4606fe..53895a8 100644 --- a/prometheus.v +++ b/prometheus.v @@ -27,10 +27,10 @@ fn join_two_array(arr [2]string) string { pub fn (e &PrometheusExporter) serialize_metric(metric Metric) string { if metric.labels.len == 0 { - return '$e.prefix$metric.name' + return '${e.prefix}${metric.name}' } - return '$e.prefix$metric.name{${metric.labels.map(join_two_array(it)).join(',')}}' + return '${e.prefix}${metric.name}{${metric.labels.map(join_two_array(it)).join(',')}}' } pub fn (mut e PrometheusExporter) export_to_string() !string { @@ -44,14 +44,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 := '${e.serialize_metric(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 := '${e.serialize_metric(gauge)} $val\n' + line := '${e.serialize_metric(gauge)} ${val}\n' writer.write(line.bytes())! } @@ -63,13 +63,13 @@ pub fn (mut e PrometheusExporter) export_to_writer(mut writer io.Writer) ! { ...hist name: '${hist.name}_count' } - writer.write('${e.serialize_metric(m)} $hist_data.total_count\n'.bytes())! + writer.write('${e.serialize_metric(m)} ${hist_data.total_count}\n'.bytes())! m = Metric{ ...hist name: '${hist.name}_sum' } - writer.write('${e.serialize_metric(m)} $hist_data.sum\n'.bytes())! + writer.write('${e.serialize_metric(m)} ${hist_data.sum}\n'.bytes())! mut le_labels := [][2]string{} le_labels.prepend(hist.labels) @@ -95,7 +95,7 @@ pub fn (mut e PrometheusExporter) export_to_writer(mut writer io.Writer) ! { labels: le_labels } - writer.write('${e.serialize_metric(m)} $hist_data.total_count\n'.bytes())! + writer.write('${e.serialize_metric(m)} ${hist_data.total_count}\n'.bytes())! } } }