vault/helper/metricsutil/wrapped_metrics_test.go
hashicorp-copywrite[bot] 0b12cdcfd1
[COMPLIANCE] License changes (#22290)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License.

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* add missing license headers

* Update copyright file headers to BUS-1.1

* Fix test that expected exact offset on hcl file

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
2023-08-10 18:14:03 -07:00

114 lines
3.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package metricsutil
import (
"testing"
"time"
"github.com/armon/go-metrics"
)
func isLabelPresent(toFind Label, ls []Label) bool {
for _, l := range ls {
if l == toFind {
return true
}
}
return false
}
// We can use a sink directly, or wrap the top-level
// go-metrics implementation for testing purposes.
func defaultMetrics(sink metrics.MetricSink) *metrics.Metrics {
// No service name
config := metrics.DefaultConfig("")
// No host name
config.EnableHostname = false
m, _ := metrics.New(config, sink)
return m
}
func TestClusterLabelPresent(t *testing.T) {
testClusterName := "test-cluster"
// Use a ridiculously long time to minimize the chance
// that we have to deal with more than one interval.
// InMemSink rounds down to an interval boundary rather than
// starting one at the time of initialization.
inmemSink := metrics.NewInmemSink(
1000000*time.Hour,
2000000*time.Hour)
clusterSink := NewClusterMetricSink(testClusterName, defaultMetrics(inmemSink))
key1 := []string{"aaa", "bbb"}
key2 := []string{"ccc", "ddd"}
key3 := []string{"eee", "fff"}
labels1 := []Label{{"dim1", "val1"}}
labels2 := []Label{{"dim2", "val2"}}
labels3 := []Label{{"dim3", "val3"}}
clusterLabel := Label{"cluster", testClusterName}
expectedKey1 := "aaa.bbb;dim1=val1;cluster=" + testClusterName
expectedKey2 := "ccc.ddd;dim2=val2;cluster=" + testClusterName
expectedKey3 := "eee.fff;dim3=val3;cluster=" + testClusterName
clusterSink.SetGaugeWithLabels(key1, 1.0, labels1)
clusterSink.IncrCounterWithLabels(key2, 2.0, labels2)
clusterSink.AddSampleWithLabels(key3, 3.0, labels3)
intervals := inmemSink.Data()
// If we start very close to the end of an interval, then our metrics might be
// split across two different buckets. We won't write the code to try to handle that.
// 100000-hours = at most once every 4167 days
if len(intervals) > 1 {
t.Skip("Detected interval crossing.")
}
// Check Gauge
g, ok := intervals[0].Gauges[expectedKey1]
if !ok {
t.Fatal("Key", expectedKey1, "not found in map", intervals[0].Gauges)
}
if g.Value != 1.0 {
t.Error("Gauge value", g.Value, "does not match", 1.0)
}
if !isLabelPresent(labels1[0], g.Labels) {
t.Error("Gauge label", g.Labels, "does not include", labels1)
}
if !isLabelPresent(clusterLabel, g.Labels) {
t.Error("Gauge label", g.Labels, "does not include", clusterLabel)
}
// Check Counter
c, ok := intervals[0].Counters[expectedKey2]
if !ok {
t.Fatal("Key", expectedKey2, "not found in map", intervals[0].Counters)
}
if c.Sum != 2.0 {
t.Error("Counter value", c.Sum, "does not match", 2.0)
}
if !isLabelPresent(labels2[0], c.Labels) {
t.Error("Counter label", c.Labels, "does not include", labels2)
}
if !isLabelPresent(clusterLabel, c.Labels) {
t.Error("Counter label", c.Labels, "does not include", clusterLabel)
}
// Check Sample
s, ok := intervals[0].Samples[expectedKey3]
if !ok {
t.Fatal("Key", expectedKey3, "not found in map", intervals[0].Samples)
}
if s.Sum != 3.0 {
t.Error("Sample value", s.Sum, "does not match", 3.0)
}
if !isLabelPresent(labels3[0], s.Labels) {
t.Error("Sample label", s.Labels, "does not include", labels3)
}
if !isLabelPresent(clusterLabel, s.Labels) {
t.Error("Sample label", s.Labels, "does not include", clusterLabel)
}
}