|
|
|
@ -7,7 +7,7 @@ import ( |
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func TestK8sGathererWrapper_Gather(t *testing.T) { |
|
|
|
|
func TestGathererPrefixWrapper_Gather(t *testing.T) { |
|
|
|
|
orig := &mockGatherer{} |
|
|
|
|
g := newAddPrefixWrapper(orig) |
|
|
|
|
|
|
|
|
@ -48,6 +48,88 @@ func TestK8sGathererWrapper_Gather(t *testing.T) { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestMultiRegistry_Gather(t *testing.T) { |
|
|
|
|
one := &mockGatherer{} |
|
|
|
|
two := &mockGatherer{} |
|
|
|
|
g := newMultiRegistry(one, two) |
|
|
|
|
|
|
|
|
|
t.Run("should merge and sort metrics", func(t *testing.T) { |
|
|
|
|
one.GatherFunc = func() ([]*dto.MetricFamily, error) { |
|
|
|
|
return []*dto.MetricFamily{ |
|
|
|
|
{Name: strptr("b")}, |
|
|
|
|
{Name: strptr("a")}, |
|
|
|
|
}, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
two.GatherFunc = func() ([]*dto.MetricFamily, error) { |
|
|
|
|
return []*dto.MetricFamily{ |
|
|
|
|
{Name: strptr("d")}, |
|
|
|
|
{Name: strptr("c")}, |
|
|
|
|
}, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
expectedMF := []*dto.MetricFamily{ |
|
|
|
|
{Name: strptr("a")}, |
|
|
|
|
{Name: strptr("b")}, |
|
|
|
|
{Name: strptr("c")}, |
|
|
|
|
{Name: strptr("d")}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mf, err := g.Gather() |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, expectedMF, mf) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("duplicate metrics result in an error", func(t *testing.T) { |
|
|
|
|
one.GatherFunc = func() ([]*dto.MetricFamily, error) { |
|
|
|
|
return []*dto.MetricFamily{ |
|
|
|
|
{Name: strptr("b")}, |
|
|
|
|
{Name: strptr("a")}, |
|
|
|
|
}, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
two.GatherFunc = func() ([]*dto.MetricFamily, error) { |
|
|
|
|
return []*dto.MetricFamily{ |
|
|
|
|
{Name: strptr("d")}, |
|
|
|
|
{Name: strptr("c")}, |
|
|
|
|
{Name: strptr("a")}, |
|
|
|
|
}, nil |
|
|
|
|
} |
|
|
|
|
_, err := g.Gather() |
|
|
|
|
require.Error(t, err) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("duplicate go_ prefixed metrics do not result in an error", func(t *testing.T) { |
|
|
|
|
one.GatherFunc = func() ([]*dto.MetricFamily, error) { |
|
|
|
|
return []*dto.MetricFamily{ |
|
|
|
|
{Name: strptr("b")}, |
|
|
|
|
{Name: strptr("a")}, |
|
|
|
|
{Name: strptr("go_a")}, |
|
|
|
|
}, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
two.GatherFunc = func() ([]*dto.MetricFamily, error) { |
|
|
|
|
return []*dto.MetricFamily{ |
|
|
|
|
{Name: strptr("d")}, |
|
|
|
|
{Name: strptr("c")}, |
|
|
|
|
{Name: strptr("go_a")}, |
|
|
|
|
}, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
expectedMF := []*dto.MetricFamily{ |
|
|
|
|
{Name: strptr("a")}, |
|
|
|
|
{Name: strptr("b")}, |
|
|
|
|
{Name: strptr("c")}, |
|
|
|
|
{Name: strptr("d")}, |
|
|
|
|
{Name: strptr("go_a")}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mf, err := g.Gather() |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, expectedMF, mf) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type mockGatherer struct { |
|
|
|
|
GatherFunc func() ([]*dto.MetricFamily, error) |
|
|
|
|
} |
|
|
|
|