The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/infra/metrics/metricutil/utils_test.go

119 lines
4.2 KiB

package metricutil
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLabelNameSanitization(t *testing.T) {
testcases := []struct {
input string
expected string
err bool
}{
{input: "job", expected: "job"},
{input: "job._loal['", expected: "job_loal"},
{input: "", expected: "", err: true},
{input: ";;;", expected: "", err: true},
{input: "Data source", expected: "Data_source"},
}
for _, tc := range testcases {
got, err := SanitizeLabelName(tc.input)
if tc.err {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tc.expected, got)
}
}
}
func Test_buildLabelSets(t *testing.T) {
testcases := map[string]struct {
labels []string
labelValues map[string][]string
expected []prometheus.Labels
}{
"single label, single value": {
labels: []string{"operation"},
labelValues: map[string][]string{
"operation": {"insert"},
},
expected: []prometheus.Labels{
map[string]string{"operation": "insert"},
},
},
"single label, multiple values": {
labels: []string{"operation"},
labelValues: map[string][]string{
"operation": {"insert", "delete"},
},
expected: []prometheus.Labels{
map[string]string{"operation": "insert"},
map[string]string{"operation": "delete"},
},
},
"multiple label, single value": {
labels: []string{"operation", "success"},
labelValues: map[string][]string{
"operation": {"insert"},
"success": {"true"},
},
expected: []prometheus.Labels{
map[string]string{"operation": "insert", "success": "true"},
},
},
"multiple label, multiple values": {
labels: []string{"operation", "success"},
labelValues: map[string][]string{
"operation": {"insert", "delete"},
"success": {"true", "false"},
},
expected: []prometheus.Labels{
map[string]string{"operation": "insert", "success": "true"},
map[string]string{"operation": "insert", "success": "false"},
map[string]string{"operation": "delete", "success": "true"},
map[string]string{"operation": "delete", "success": "false"},
},
},
"irregular labels and values": {
labels: []string{"operation", "success", "environment"},
labelValues: map[string][]string{
"operation": {"insert", "update", "delete"},
"success": {"true", "false"},
"environment": {"dev", "test", "staging"},
},
expected: []prometheus.Labels{
map[string]string{"operation": "insert", "success": "true", "environment": "dev"},
map[string]string{"operation": "insert", "success": "true", "environment": "test"},
map[string]string{"operation": "insert", "success": "true", "environment": "staging"},
map[string]string{"operation": "insert", "success": "false", "environment": "dev"},
map[string]string{"operation": "insert", "success": "false", "environment": "test"},
map[string]string{"operation": "insert", "success": "false", "environment": "staging"},
map[string]string{"operation": "update", "success": "true", "environment": "dev"},
map[string]string{"operation": "update", "success": "true", "environment": "test"},
map[string]string{"operation": "update", "success": "true", "environment": "staging"},
map[string]string{"operation": "update", "success": "false", "environment": "dev"},
map[string]string{"operation": "update", "success": "false", "environment": "test"},
map[string]string{"operation": "update", "success": "false", "environment": "staging"},
map[string]string{"operation": "delete", "success": "true", "environment": "dev"},
map[string]string{"operation": "delete", "success": "true", "environment": "test"},
map[string]string{"operation": "delete", "success": "true", "environment": "staging"},
map[string]string{"operation": "delete", "success": "false", "environment": "dev"},
map[string]string{"operation": "delete", "success": "false", "environment": "test"},
map[string]string{"operation": "delete", "success": "false", "environment": "staging"},
},
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
got := buildLabelSets(tc.labels, tc.labelValues)
assert.Equal(t, tc.expected, got)
})
}
}