Compare commits

..

3 Commits

Author SHA1 Message Date
Jef Roosens 79079bbcd4
chore: ran v fmt for v 0.3.3 changes 2023-02-08 10:45:43 +01:00
Jef Roosens 0df06647b3
fix: i'm dumb 2023-01-28 17:33:48 +01:00
Jef Roosens ae2eeeb974
fix: add buckets_set function to interface 2023-01-28 17:32:57 +01:00
4 changed files with 12 additions and 9 deletions

View File

@ -10,7 +10,7 @@ pub:
}
pub fn (m &Metric) str() string {
return '$m.name $m.labels'
return '${m.name} ${m.labels}'
}
pub interface MetricsCollector {
@ -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)

2
null.v
View File

@ -19,6 +19,8 @@ pub fn (c &NullCollector) counters() []Metric {
return []
}
pub fn (c &NullCollector) 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 {

View File

@ -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())!
}
}
}