feat: exporter name prefix & custom metric serialisation
parent
23d384989e
commit
7e39051c1f
15
metrics.v
15
metrics.v
|
@ -4,21 +4,13 @@ import io
|
||||||
|
|
||||||
[params]
|
[params]
|
||||||
pub struct Metric {
|
pub struct Metric {
|
||||||
|
pub:
|
||||||
name string [required]
|
name string [required]
|
||||||
labels [][2]string
|
labels [][2]string
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
|
||||||
fn join_two_array(arr [2]string) string {
|
|
||||||
return '${arr[0]}="${arr[1]}"'
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (m &Metric) str() string {
|
pub fn (m &Metric) str() string {
|
||||||
if m.labels.len == 0 {
|
return '$m.name $m.labels'
|
||||||
return m.name
|
|
||||||
}
|
|
||||||
|
|
||||||
return '$m.name{${m.labels.map(join_two_array(it)).join(',')}}'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub interface MetricsCollector {
|
pub interface MetricsCollector {
|
||||||
|
@ -38,7 +30,8 @@ mut:
|
||||||
}
|
}
|
||||||
|
|
||||||
pub interface MetricsExporter {
|
pub interface MetricsExporter {
|
||||||
load(collector MetricsCollector)
|
load(prefix string, collector MetricsCollector)
|
||||||
export_to_writer(writer io.Writer) !
|
export_to_writer(writer io.Writer) !
|
||||||
export_to_string() !string
|
export_to_string() !string
|
||||||
|
serialize_metric(metric Metric) string
|
||||||
}
|
}
|
||||||
|
|
27
prometheus.v
27
prometheus.v
|
@ -7,6 +7,7 @@ import arrays
|
||||||
pub struct PrometheusExporter {
|
pub struct PrometheusExporter {
|
||||||
buckets []f64
|
buckets []f64
|
||||||
mut:
|
mut:
|
||||||
|
prefix string
|
||||||
collector &MetricsCollector = unsafe { nil }
|
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 {
|
unsafe {
|
||||||
e.collector = collector
|
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 {
|
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) ! {
|
pub fn (mut e PrometheusExporter) export_to_writer(mut writer io.Writer) ! {
|
||||||
for counter in e.collector.counters() {
|
for counter in e.collector.counters() {
|
||||||
val := e.collector.counter_get(counter) or { return error("This can't happen.") }
|
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())!
|
writer.write(line.bytes())!
|
||||||
}
|
}
|
||||||
|
|
||||||
for gauge in e.collector.gauges() {
|
for gauge in e.collector.gauges() {
|
||||||
val := e.collector.gauge_get(gauge) or { return error("This can't happen.") }
|
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())!
|
writer.write(line.bytes())!
|
||||||
}
|
}
|
||||||
|
@ -72,13 +87,13 @@ pub fn (mut e PrometheusExporter) export_to_writer(mut writer io.Writer) ! {
|
||||||
...hist
|
...hist
|
||||||
name: '${hist.name}_count'
|
name: '${hist.name}_count'
|
||||||
}
|
}
|
||||||
writer.write('$m $total_count\n'.bytes())!
|
writer.write('${e.serialize_metric(m)} $total_count\n'.bytes())!
|
||||||
|
|
||||||
m = Metric{
|
m = Metric{
|
||||||
...hist
|
...hist
|
||||||
name: '${hist.name}_sum'
|
name: '${hist.name}_sum'
|
||||||
}
|
}
|
||||||
writer.write('$m $sum\n'.bytes())!
|
writer.write('${e.serialize_metric(m)} $sum\n'.bytes())!
|
||||||
|
|
||||||
mut le_labels := [][2]string{}
|
mut le_labels := [][2]string{}
|
||||||
le_labels.prepend(hist.labels)
|
le_labels.prepend(hist.labels)
|
||||||
|
@ -92,7 +107,7 @@ pub fn (mut e PrometheusExporter) export_to_writer(mut writer io.Writer) ! {
|
||||||
labels: le_labels
|
labels: le_labels
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.write('$m ${bucket_counts[j]}\n'.bytes())!
|
writer.write('${e.serialize_metric(m)} ${bucket_counts[j]}\n'.bytes())!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,9 @@ fn test_only_counters() {
|
||||||
m.counter_increment(name: 'test')
|
m.counter_increment(name: 'test')
|
||||||
|
|
||||||
mut e := new_prometheus_exporter([])
|
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{
|
metric := Metric{
|
||||||
name: 'test2'
|
name: 'test2'
|
||||||
|
@ -16,7 +16,7 @@ fn test_only_counters() {
|
||||||
m.counter_increment(metric)
|
m.counter_increment(metric)
|
||||||
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() {
|
fn test_only_gauges() {
|
||||||
|
@ -24,9 +24,9 @@ fn test_only_gauges() {
|
||||||
m.gauge_set(3.25, name: 'test')
|
m.gauge_set(3.25, name: 'test')
|
||||||
|
|
||||||
mut e := new_prometheus_exporter([])
|
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{
|
metric := Metric{
|
||||||
name: 'test2'
|
name: 'test2'
|
||||||
|
@ -34,7 +34,7 @@ fn test_only_gauges() {
|
||||||
}
|
}
|
||||||
m.gauge_add(2.5, metric)
|
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() {
|
fn test_single_histogram() {
|
||||||
|
@ -43,7 +43,7 @@ fn test_single_histogram() {
|
||||||
m.histogram_record(5.0, name: 'test')
|
m.histogram_record(5.0, name: 'test')
|
||||||
|
|
||||||
mut e := new_prometheus_exporter([0.5, 5.0])
|
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'
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue