remove prometheus dependencies from k/k and add testcases for LabelsMatch
Kubernetes-commit: 6e986249ee4252f83037f229a8773869feaab15a
This commit is contained in:
parent
e4ed80588e
commit
529b6da9bb
18
go.mod
18
go.mod
|
|
@ -27,7 +27,6 @@ require (
|
|||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 // indirect
|
||||
github.com/prometheus/client_model v0.2.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/stretchr/testify v1.4.0
|
||||
go.etcd.io/etcd v0.5.0-alpha.5.0.20200401174654-e694b7bb0875
|
||||
|
|
@ -41,10 +40,10 @@ require (
|
|||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||
gopkg.in/square/go-jose.v2 v2.2.2
|
||||
gopkg.in/yaml.v2 v2.2.8
|
||||
k8s.io/api v0.0.0-20200421221621-2ca06ba83edf
|
||||
k8s.io/apimachinery v0.0.0-20200423195709-147f4cafe1e5
|
||||
k8s.io/client-go v0.0.0-20200424080147-0ad81dc3c85d
|
||||
k8s.io/component-base v0.0.0-20200423061759-681caaeb5a1e
|
||||
k8s.io/api v0.0.0
|
||||
k8s.io/apimachinery v0.0.0
|
||||
k8s.io/client-go v0.0.0
|
||||
k8s.io/component-base v0.0.0
|
||||
k8s.io/klog v1.0.0
|
||||
k8s.io/kube-openapi v0.0.0-20200403204345-e1beb1bd0f35
|
||||
k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89
|
||||
|
|
@ -56,8 +55,9 @@ require (
|
|||
replace (
|
||||
golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // pinned to release-branch.go1.13
|
||||
golang.org/x/tools => golang.org/x/tools v0.0.0-20190821162956-65e3620a7ae7 // pinned to release-branch.go1.13
|
||||
k8s.io/api => k8s.io/api v0.0.0-20200421221621-2ca06ba83edf
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200423195709-147f4cafe1e5
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20200424080147-0ad81dc3c85d
|
||||
k8s.io/component-base => k8s.io/component-base v0.0.0-20200423061759-681caaeb5a1e
|
||||
k8s.io/api => ../api
|
||||
k8s.io/apimachinery => ../apimachinery
|
||||
k8s.io/apiserver => ../apiserver
|
||||
k8s.io/client-go => ../client-go
|
||||
k8s.io/component-base => ../component-base
|
||||
)
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -362,10 +362,6 @@ gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
|
|||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
k8s.io/api v0.0.0-20200421221621-2ca06ba83edf/go.mod h1:ulbB/6H6vVXAkqfLv6nAqwp3OAksqUGzbqvrQ8uWFCs=
|
||||
k8s.io/apimachinery v0.0.0-20200423195709-147f4cafe1e5/go.mod h1:imoz42hIYwpLTRWXU8pdJ9IE8DbxUsnU9lyVN8Y1SNo=
|
||||
k8s.io/client-go v0.0.0-20200424080147-0ad81dc3c85d/go.mod h1:6fX2/t8t2anrYnkj3wBvT0ObCPteMb7rnUL+W2Tn914=
|
||||
k8s.io/component-base v0.0.0-20200423061759-681caaeb5a1e/go.mod h1:HZ3pegcljoPKhQyaHWt9RSDAwJjsWJXD5M5vQhy+iIE=
|
||||
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
|
||||
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
|
||||
k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
|
||||
|
|
|
|||
|
|
@ -19,43 +19,36 @@ package metrics
|
|||
import (
|
||||
"testing"
|
||||
|
||||
ptype "github.com/prometheus/client_model/go"
|
||||
"k8s.io/component-base/metrics/legacyregistry"
|
||||
"k8s.io/component-base/metrics/testutil"
|
||||
)
|
||||
|
||||
func labelsMatch(metric *ptype.Metric, labelFilter map[string]string) bool {
|
||||
for _, lp := range metric.GetLabel() {
|
||||
if value, ok := labelFilter[lp.GetName()]; ok && lp.GetValue() != value {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// expectFindMetric find a metric with the given name nad labels or reports a fatal test error.
|
||||
func expectFindMetric(t *testing.T, name string, expectedLabels map[string]string) *ptype.Metric {
|
||||
func expectFindMetric(t *testing.T, name string, expectedLabels map[string]string) {
|
||||
metrics, err := legacyregistry.DefaultGatherer.Gather()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to gather metrics: %s", err)
|
||||
}
|
||||
|
||||
if len(metrics) == 0 {
|
||||
t.Fatalf("No metric found with name %s and labels %#+v", name, expectedLabels)
|
||||
}
|
||||
|
||||
for _, mf := range metrics {
|
||||
if mf.GetName() == name {
|
||||
for _, metric := range mf.GetMetric() {
|
||||
if labelsMatch(metric, expectedLabels) {
|
||||
if testutil.LabelsMatch(metric, expectedLabels) {
|
||||
gotLabelCount := len(metric.GetLabel())
|
||||
wantLabelCount := len(expectedLabels)
|
||||
if wantLabelCount != gotLabelCount {
|
||||
t.Errorf("Got metric with %d labels, but wanted %d labels. Wanted %#+v for %s",
|
||||
gotLabelCount, wantLabelCount, expectedLabels, metric.String())
|
||||
continue
|
||||
}
|
||||
return metric
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
t.Fatalf("No metric found with name %s and labels %#+v", name, expectedLabels)
|
||||
return nil
|
||||
}
|
||||
|
||||
// expectHistogramCountTotal ensures that the sum of counts of metrics matching the labelFilter is as
|
||||
|
|
@ -72,7 +65,7 @@ func expectHistogramCountTotal(t *testing.T, name string, labelFilter map[string
|
|||
continue // Ignore other metrics.
|
||||
}
|
||||
for _, metric := range mf.GetMetric() {
|
||||
if !labelsMatch(metric, labelFilter) {
|
||||
if !testutil.LabelsMatch(metric, labelFilter) {
|
||||
continue
|
||||
}
|
||||
counterSum += int(metric.GetHistogram().GetSampleCount())
|
||||
|
|
@ -104,7 +97,7 @@ func expectCounterValue(t *testing.T, name string, labelFilter map[string]string
|
|||
continue // Ignore other metrics.
|
||||
}
|
||||
for _, metric := range mf.GetMetric() {
|
||||
if !labelsMatch(metric, labelFilter) {
|
||||
if !testutil.LabelsMatch(metric, labelFilter) {
|
||||
continue
|
||||
}
|
||||
counterSum += int(metric.GetCounter().GetValue())
|
||||
|
|
|
|||
Loading…
Reference in New Issue