forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
K8s: Refactor metrics to share k8s registry (grafana#79106)
- Loading branch information
1 parent
c631261
commit 2a2a132
Showing
13 changed files
with
197 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package metrics | ||
|
||
import ( | ||
"testing" | ||
|
||
dto "github.com/prometheus/client_model/go" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestK8sGathererWrapper_Gather(t *testing.T) { | ||
orig := &mockGatherer{} | ||
g := newAddPrefixWrapper(orig) | ||
|
||
t.Run("metrics with grafana and go prefix are not modified", func(t *testing.T) { | ||
originalMF := []*dto.MetricFamily{ | ||
{Name: strptr("grafana_metric1")}, | ||
{Name: strptr("metric2")}, | ||
{Name: strptr("go_metric1")}, | ||
} | ||
|
||
orig.GatherFunc = func() ([]*dto.MetricFamily, error) { | ||
return originalMF, nil | ||
} | ||
|
||
expectedMF := []*dto.MetricFamily{ | ||
{Name: strptr("grafana_metric1")}, | ||
{Name: strptr("grafana_metric2")}, | ||
{Name: strptr("go_metric1")}, | ||
} | ||
|
||
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) { | ||
originalMF := []*dto.MetricFamily{ | ||
{Name: strptr("grafana_metric1")}, | ||
{Name: strptr("metric1")}, | ||
} | ||
|
||
orig.GatherFunc = func() ([]*dto.MetricFamily, error) { | ||
return originalMF, nil | ||
} | ||
|
||
_, err := g.Gather() | ||
require.Error(t, err) | ||
}) | ||
} | ||
|
||
type mockGatherer struct { | ||
GatherFunc func() ([]*dto.MetricFamily, error) | ||
} | ||
|
||
func (m *mockGatherer) Gather() ([]*dto.MetricFamily, error) { | ||
return m.GatherFunc() | ||
} | ||
|
||
func strptr(s string) *string { | ||
return &s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/google/wire" | ||
) | ||
|
||
var WireSet = wire.NewSet( | ||
ProvideService, | ||
ProvideRegisterer, | ||
ProvideGatherer, | ||
) | ||
|
||
var WireSetForTest = wire.NewSet( | ||
ProvideService, | ||
ProvideRegistererForTest, | ||
ProvideGathererForTest, | ||
) |
Oops, something went wrong.