139 lines
2.6 KiB
V
139 lines
2.6 KiB
V
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)
|
|
|
|
// Test with labels
|
|
metric := Metric{
|
|
name: 'test2'
|
|
labels: [['hi', 'label']!, ['hi2', 'label2']!]
|
|
}
|
|
|
|
m.counter_set(15, metric)
|
|
m.counter_increment(metric)
|
|
assert m.counter_get(metric)? == u64(16)
|
|
}
|
|
|
|
fn test_histogram() {
|
|
mut m := new_default_collector()
|
|
|
|
m.histogram_record(5.0, name: 'test')
|
|
|
|
assert m.histogram_get(name: 'test')? == Histogram{
|
|
metric: Metric{
|
|
name: 'test'
|
|
}
|
|
total_count: 1
|
|
sum: 5.0
|
|
buckets: []
|
|
bucket_counts: []
|
|
}
|
|
|
|
m.histogram_record(7.0, name: 'test')
|
|
|
|
assert m.histogram_get(name: 'test')? == Histogram{
|
|
metric: Metric{
|
|
name: 'test'
|
|
}
|
|
total_count: 2
|
|
sum: 12.0
|
|
buckets: []
|
|
bucket_counts: []
|
|
}
|
|
|
|
// Test with labels
|
|
metric := Metric{
|
|
name: 'test2'
|
|
labels: [['hi', 'label']!, ['hi2', 'label2']!]
|
|
}
|
|
|
|
m.histogram_buckets_set('test2', [10.0])
|
|
m.histogram_record(5.0, metric)
|
|
|
|
assert m.histogram_get(metric)? == Histogram{
|
|
metric: metric
|
|
total_count: 1
|
|
sum: 5.0
|
|
buckets: [10.0]
|
|
bucket_counts: [u64(1)]
|
|
}
|
|
|
|
m.histogram_record(7.0, metric)
|
|
|
|
assert m.histogram_get(metric)? == Histogram{
|
|
metric: metric
|
|
total_count: 2
|
|
sum: 12.0
|
|
buckets: [10.0]
|
|
bucket_counts: [u64(2)]
|
|
}
|
|
}
|
|
|
|
fn test_gauge_add() {
|
|
mut m := new_default_collector()
|
|
|
|
m.gauge_add(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)
|
|
m.gauge_add(5.0, metric)
|
|
assert m.gauge_get(metric)? == 8.0
|
|
}
|
|
|
|
fn test_gauge_sub() {
|
|
mut m := new_default_collector()
|
|
|
|
m.gauge_sub(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)
|
|
m.gauge_sub(5.0, metric)
|
|
assert m.gauge_get(metric)? == -2.0
|
|
}
|
|
|
|
fn test_gauge_set() {
|
|
mut m := new_default_collector()
|
|
|
|
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
|
|
}
|