Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
Jef Roosens | 79079bbcd4 | |
Jef Roosens | 0df06647b3 | |
Jef Roosens | ae2eeeb974 |
|
@ -10,7 +10,7 @@ pub:
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (m &Metric) str() string {
|
pub fn (m &Metric) str() string {
|
||||||
return '$m.name $m.labels'
|
return '${m.name} ${m.labels}'
|
||||||
}
|
}
|
||||||
|
|
||||||
pub interface MetricsCollector {
|
pub interface MetricsCollector {
|
||||||
|
@ -23,6 +23,7 @@ pub interface MetricsCollector {
|
||||||
mut:
|
mut:
|
||||||
counter_set(val u64, metric Metric)
|
counter_set(val u64, metric Metric)
|
||||||
counter_increment(metric Metric)
|
counter_increment(metric Metric)
|
||||||
|
histogram_buckets_set(name string, buckets []f64)
|
||||||
histogram_record(value f64, metric Metric)
|
histogram_record(value f64, metric Metric)
|
||||||
gauge_set(value f64, metric Metric)
|
gauge_set(value f64, metric Metric)
|
||||||
gauge_add(value f64, metric Metric)
|
gauge_add(value f64, metric Metric)
|
||||||
|
|
2
null.v
2
null.v
|
@ -19,6 +19,8 @@ pub fn (c &NullCollector) counters() []Metric {
|
||||||
return []
|
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_record(value f64, metric Metric) {}
|
||||||
|
|
||||||
pub fn (c &NullCollector) histogram_get(metric Metric) ?Histogram {
|
pub fn (c &NullCollector) histogram_get(metric Metric) ?Histogram {
|
||||||
|
|
14
prometheus.v
14
prometheus.v
|
@ -27,10 +27,10 @@ fn join_two_array(arr [2]string) string {
|
||||||
|
|
||||||
pub fn (e &PrometheusExporter) serialize_metric(metric Metric) string {
|
pub fn (e &PrometheusExporter) serialize_metric(metric Metric) string {
|
||||||
if metric.labels.len == 0 {
|
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 {
|
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) ! {
|
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 := '${e.serialize_metric(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 := '${e.serialize_metric(gauge)} $val\n'
|
line := '${e.serialize_metric(gauge)} ${val}\n'
|
||||||
|
|
||||||
writer.write(line.bytes())!
|
writer.write(line.bytes())!
|
||||||
}
|
}
|
||||||
|
@ -63,13 +63,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('${e.serialize_metric(m)} $hist_data.total_count\n'.bytes())!
|
writer.write('${e.serialize_metric(m)} ${hist_data.total_count}\n'.bytes())!
|
||||||
|
|
||||||
m = Metric{
|
m = Metric{
|
||||||
...hist
|
...hist
|
||||||
name: '${hist.name}_sum'
|
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{}
|
mut le_labels := [][2]string{}
|
||||||
le_labels.prepend(hist.labels)
|
le_labels.prepend(hist.labels)
|
||||||
|
@ -95,7 +95,7 @@ pub fn (mut e PrometheusExporter) export_to_writer(mut writer io.Writer) ! {
|
||||||
labels: le_labels
|
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())!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ fn test_only_gauges() {
|
||||||
fn test_single_histogram() {
|
fn test_single_histogram() {
|
||||||
mut m := new_default_collector()
|
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(5.0, name: 'test')
|
||||||
m.histogram_record(7.0, name: 'test')
|
m.histogram_record(7.0, name: 'test')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue