mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-07 22:25:03 +00:00
Breaking down some more components, adding tests.
* Some bug fixes found during testing/test repair. * Trying to tease apart the various responsibilities of `metricNamer` into smaller chunks and adding tests for each individual chunk. * Updating the `provider_test` and `series_registry_test` to fix failures.
This commit is contained in:
parent
76217a552b
commit
fc88e6e57a
18 changed files with 1038 additions and 516 deletions
85
pkg/custom-provider/metric_name_converter_test.go
Normal file
85
pkg/custom-provider/metric_name_converter_test.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package provider
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
|
||||
config "github.com/directxman12/k8s-prometheus-adapter/pkg/config"
|
||||
)
|
||||
|
||||
func TestWhenNoMappingMetricNameIsUnaltered(t *testing.T) {
|
||||
emptyMapping := config.NameMapping{}
|
||||
RunTest(t, emptyMapping, "my_series", "my_series")
|
||||
RunTest(t, emptyMapping, "your_series", "your_series")
|
||||
}
|
||||
|
||||
func TestWhenMappingWithOneCaptureGroupMetricNameIsCorrect(t *testing.T) {
|
||||
mapping := config.NameMapping{
|
||||
Matches: "my_(.*)",
|
||||
As: "your_$1",
|
||||
}
|
||||
|
||||
RunTest(t, mapping, "my_requests_per_second", "your_requests_per_second")
|
||||
}
|
||||
|
||||
func TestWhenMappingWithMultipleCaptureGroupsMetricNameIsCorrect(t *testing.T) {
|
||||
//ExpandString has some strange behavior when using the $1, $2 syntax
|
||||
//Specifically, it doesn't return the expected values for templates like:
|
||||
//$1_$2
|
||||
//You can work around it by using the ${1} syntax.
|
||||
mapping := config.NameMapping{
|
||||
Matches: "my_([^_]+)_([^_]+)",
|
||||
As: "your_${1}_is_${2}_large",
|
||||
}
|
||||
|
||||
RunTest(t, mapping, "my_horse_very", "your_horse_is_very_large")
|
||||
RunTest(t, mapping, "my_dog_not", "your_dog_is_not_large")
|
||||
}
|
||||
|
||||
func TestAsCanBeInferred(t *testing.T) {
|
||||
//When we've got one capture group, we should infer that as the target.
|
||||
mapping := config.NameMapping{
|
||||
Matches: "my_(.+)",
|
||||
}
|
||||
|
||||
RunTest(t, mapping, "my_test_metric", "test_metric")
|
||||
|
||||
//When we have no capture groups, we should infer that the whole thing as the target.
|
||||
mapping = config.NameMapping{
|
||||
Matches: "my_metric",
|
||||
}
|
||||
|
||||
RunTest(t, mapping, "my_metric", "my_metric")
|
||||
}
|
||||
|
||||
func TestWhenAsCannotBeInferredError(t *testing.T) {
|
||||
//More than one capture group should
|
||||
//result in us giving up on making an educated guess.
|
||||
mapping := config.NameMapping{
|
||||
Matches: "my_([^_]+)_([^_]+)",
|
||||
}
|
||||
|
||||
RunTestExpectingError(t, mapping, "my_horse_very")
|
||||
RunTestExpectingError(t, mapping, "my_dog_not")
|
||||
}
|
||||
|
||||
func RunTest(t *testing.T, mapping config.NameMapping, input string, expectedResult string) {
|
||||
converter, err := NewMetricNameConverter(mapping)
|
||||
require.NoError(t, err)
|
||||
|
||||
series := prom.Series{
|
||||
Name: input,
|
||||
}
|
||||
|
||||
actualResult, err := converter.GetMetricNameForSeries(series)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, expectedResult, actualResult)
|
||||
}
|
||||
|
||||
func RunTestExpectingError(t *testing.T, mapping config.NameMapping, input string) {
|
||||
_, err := NewMetricNameConverter(mapping)
|
||||
require.Error(t, err)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue