2022-12-26 18:17:27 +01:00
|
|
|
module metrics
|
|
|
|
|
|
|
|
fn test_implements_interface() {
|
|
|
|
_ := MetricsCollector(new_default_collector())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_null_implements_interface() {
|
|
|
|
_ := MetricsCollector(new_null_collector())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_counter_increment() {
|
|
|
|
mut m := new_default_collector()
|
|
|
|
|
|
|
|
m.counter_increment(name: 'test')
|
|
|
|
assert m.counter_get(name: 'test')? == u64(1)
|
|
|
|
|
|
|
|
m.counter_increment(name: 'test')
|
|
|
|
assert m.counter_get(name: 'test')? == u64(2)
|
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
// Test with labels
|
|
|
|
metric := Metric{
|
|
|
|
name: 'test2'
|
|
|
|
labels: [['hi', 'label']!, ['hi2', 'label2']!]
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
m.counter_set(15, metric)
|
2022-12-26 18:17:27 +01:00
|
|
|
m.counter_increment(metric)
|
|
|
|
assert m.counter_get(metric)? == u64(16)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_histogram() {
|
|
|
|
mut m := new_default_collector()
|
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
m.histogram_record(5.0, name: 'test')
|
2023-01-28 17:21:59 +01:00
|
|
|
|
|
|
|
assert m.histogram_get(name: 'test')? == Histogram{
|
|
|
|
metric: Metric{
|
|
|
|
name: 'test'
|
|
|
|
}
|
|
|
|
total_count: 1
|
|
|
|
sum: 5.0
|
|
|
|
buckets: []
|
|
|
|
bucket_counts: []
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
m.histogram_record(7.0, name: 'test')
|
2023-01-28 17:21:59 +01:00
|
|
|
|
|
|
|
assert m.histogram_get(name: 'test')? == Histogram{
|
|
|
|
metric: Metric{
|
|
|
|
name: 'test'
|
|
|
|
}
|
|
|
|
total_count: 2
|
|
|
|
sum: 12.0
|
|
|
|
buckets: []
|
|
|
|
bucket_counts: []
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
// Test with labels
|
|
|
|
metric := Metric{
|
|
|
|
name: 'test2'
|
|
|
|
labels: [['hi', 'label']!, ['hi2', 'label2']!]
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2023-01-28 17:21:59 +01:00
|
|
|
m.histogram_buckets_set('test2', [10.0])
|
2022-12-26 21:49:07 +01:00
|
|
|
m.histogram_record(5.0, metric)
|
2023-01-28 17:21:59 +01:00
|
|
|
|
|
|
|
assert m.histogram_get(metric)? == Histogram{
|
|
|
|
metric: metric
|
|
|
|
total_count: 1
|
|
|
|
sum: 5.0
|
|
|
|
buckets: [10.0]
|
|
|
|
bucket_counts: [u64(1)]
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
m.histogram_record(7.0, metric)
|
2023-01-28 17:21:59 +01:00
|
|
|
|
|
|
|
assert m.histogram_get(metric)? == Histogram{
|
|
|
|
metric: metric
|
|
|
|
total_count: 2
|
|
|
|
sum: 12.0
|
|
|
|
buckets: [10.0]
|
|
|
|
bucket_counts: [u64(2)]
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_gauge_add() {
|
|
|
|
mut m := new_default_collector()
|
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
m.gauge_add(5.0, name: 'test')
|
|
|
|
assert m.gauge_get(name: 'test')? == 5.0
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
// Test with labels
|
|
|
|
metric := Metric{
|
|
|
|
name: 'test2'
|
|
|
|
labels: [['hi', 'label']!, ['hi2', 'label2']!]
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
m.gauge_set(3.0, metric)
|
2022-12-26 21:49:07 +01:00
|
|
|
m.gauge_add(5.0, metric)
|
|
|
|
assert m.gauge_get(metric)? == 8.0
|
2022-12-26 18:17:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_gauge_sub() {
|
|
|
|
mut m := new_default_collector()
|
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
m.gauge_sub(5.0, name: 'test')
|
|
|
|
assert m.gauge_get(name: 'test')? == -5.0
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
// Test with labels
|
|
|
|
metric := Metric{
|
|
|
|
name: 'test2'
|
|
|
|
labels: [['hi', 'label']!, ['hi2', 'label2']!]
|
|
|
|
}
|
2022-12-26 18:17:27 +01:00
|
|
|
|
2022-12-27 09:03:41 +01:00
|
|
|
m.gauge_set(3.0, metric)
|
2022-12-26 21:49:07 +01:00
|
|
|
m.gauge_sub(5.0, metric)
|
|
|
|
assert m.gauge_get(metric)? == -2.0
|
2022-12-26 18:17:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_gauge_set() {
|
|
|
|
mut m := new_default_collector()
|
|
|
|
|
2022-12-26 21:49:07 +01:00
|
|
|
m.gauge_set(3.0, name: 'test')
|
|
|
|
assert m.gauge_get(name: 'test')? == 3.0
|
|
|
|
m.gauge_set(5.0, name: 'test')
|
|
|
|
assert m.gauge_get(name: 'test')? == 5.0
|
|
|
|
|
|
|
|
// Test with labels
|
|
|
|
metric := Metric{
|
|
|
|
name: 'test2'
|
|
|
|
labels: [['hi', 'label']!, ['hi2', 'label2']!]
|
|
|
|
}
|
|
|
|
|
|
|
|
m.gauge_set(3.0, metric)
|
|
|
|
assert m.gauge_get(metric)? == 3.0
|
|
|
|
m.gauge_set(5.0, metric)
|
|
|
|
assert m.gauge_get(metric)? == 5.0
|
2022-12-26 18:17:27 +01:00
|
|
|
}
|