mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 09:47:54 +00:00
gofmt this mess.
This commit is contained in:
parent
fc88e6e57a
commit
bf9be4e53a
2 changed files with 72 additions and 23 deletions
49
pkg/custom-provider/regex_matcher_test.go
Normal file
49
pkg/custom-provider/regex_matcher_test.go
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/directxman12/k8s-prometheus-adapter/pkg/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestReMatcherIs(t *testing.T) {
|
||||||
|
filter := config.RegexFilter{
|
||||||
|
Is: "my_.*",
|
||||||
|
}
|
||||||
|
|
||||||
|
matcher, err := newReMatcher(filter)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
result := matcher.Matches("my_label")
|
||||||
|
require.True(t, result)
|
||||||
|
|
||||||
|
result = matcher.Matches("your_label")
|
||||||
|
require.False(t, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReMatcherIsNot(t *testing.T) {
|
||||||
|
filter := config.RegexFilter{
|
||||||
|
IsNot: "my_.*",
|
||||||
|
}
|
||||||
|
|
||||||
|
matcher, err := newReMatcher(filter)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
result := matcher.Matches("my_label")
|
||||||
|
require.False(t, result)
|
||||||
|
|
||||||
|
result = matcher.Matches("your_label")
|
||||||
|
require.True(t, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnforcesIsOrIsNotButNotBoth(t *testing.T) {
|
||||||
|
filter := config.RegexFilter{
|
||||||
|
Is: "my_.*",
|
||||||
|
IsNot: "your_.*",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := newReMatcher(filter)
|
||||||
|
require.Error(t, err)
|
||||||
|
}
|
||||||
|
|
@ -75,11 +75,11 @@ func NewResourceConverter(resourceTemplate string, overrides map[string]config.G
|
||||||
return converter, nil
|
return converter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *resourceConverter) LabelForResource(resource schema.GroupResource) (pmodel.LabelName, error) {
|
func (r *resourceConverter) LabelForResource(resource schema.GroupResource) (pmodel.LabelName, error) {
|
||||||
n.labelResourceMu.RLock()
|
r.labelResourceMu.RLock()
|
||||||
// check if we have a cached copy or override
|
// check if we have a cached copy or override
|
||||||
lbl, ok := n.resourceToLabel[resource]
|
lbl, ok := r.resourceToLabel[resource]
|
||||||
n.labelResourceMu.RUnlock() // release before we call makeLabelForResource
|
r.labelResourceMu.RUnlock() // release before we call makeLabelForResource
|
||||||
if ok {
|
if ok {
|
||||||
return lbl, nil
|
return lbl, nil
|
||||||
}
|
}
|
||||||
|
|
@ -89,7 +89,7 @@ func (n *resourceConverter) LabelForResource(resource schema.GroupResource) (pmo
|
||||||
// it, as long as we're correct.
|
// it, as long as we're correct.
|
||||||
|
|
||||||
// otherwise, use the template and save the result
|
// otherwise, use the template and save the result
|
||||||
lbl, err := n.makeLabelForResource(resource)
|
lbl, err := r.makeLabelForResource(resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("unable to convert resource %s into label: %v", resource.String(), err)
|
return "", fmt.Errorf("unable to convert resource %s into label: %v", resource.String(), err)
|
||||||
}
|
}
|
||||||
|
|
@ -100,13 +100,13 @@ var groupNameSanitizer = strings.NewReplacer(".", "_", "-", "_")
|
||||||
|
|
||||||
// makeLabelForResource constructs a label name for the given resource, and saves the result.
|
// makeLabelForResource constructs a label name for the given resource, and saves the result.
|
||||||
// It must *not* be called under an existing lock.
|
// It must *not* be called under an existing lock.
|
||||||
func (n *resourceConverter) makeLabelForResource(resource schema.GroupResource) (pmodel.LabelName, error) {
|
func (r *resourceConverter) makeLabelForResource(resource schema.GroupResource) (pmodel.LabelName, error) {
|
||||||
if n.labelTemplate == nil {
|
if r.labelTemplate == nil {
|
||||||
return "", fmt.Errorf("no generic resource label form specified for this metric")
|
return "", fmt.Errorf("no generic resource label form specified for this metric")
|
||||||
}
|
}
|
||||||
buff := new(bytes.Buffer)
|
buff := new(bytes.Buffer)
|
||||||
|
|
||||||
singularRes, err := n.mapper.ResourceSingularizer(resource.Resource)
|
singularRes, err := r.mapper.ResourceSingularizer(resource.Resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("unable to singularize resource %s: %v", resource.String(), err)
|
return "", fmt.Errorf("unable to singularize resource %s: %v", resource.String(), err)
|
||||||
}
|
}
|
||||||
|
|
@ -115,7 +115,7 @@ func (n *resourceConverter) makeLabelForResource(resource schema.GroupResource)
|
||||||
Resource: singularRes,
|
Resource: singularRes,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := n.labelTemplate.Execute(buff, convResource); err != nil {
|
if err := r.labelTemplate.Execute(buff, convResource); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if buff.Len() == 0 {
|
if buff.Len() == 0 {
|
||||||
|
|
@ -123,15 +123,15 @@ func (n *resourceConverter) makeLabelForResource(resource schema.GroupResource)
|
||||||
}
|
}
|
||||||
lbl := pmodel.LabelName(buff.String())
|
lbl := pmodel.LabelName(buff.String())
|
||||||
|
|
||||||
n.labelResourceMu.Lock()
|
r.labelResourceMu.Lock()
|
||||||
defer n.labelResourceMu.Unlock()
|
defer r.labelResourceMu.Unlock()
|
||||||
|
|
||||||
n.resourceToLabel[resource] = lbl
|
r.resourceToLabel[resource] = lbl
|
||||||
n.labelToResource[lbl] = resource
|
r.labelToResource[lbl] = resource
|
||||||
return lbl, nil
|
return lbl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *resourceConverter) ResourcesForSeries(series prom.Series) ([]schema.GroupResource, bool) {
|
func (r *resourceConverter) ResourcesForSeries(series prom.Series) ([]schema.GroupResource, bool) {
|
||||||
// use an updates map to avoid having to drop the read lock to update the cache
|
// use an updates map to avoid having to drop the read lock to update the cache
|
||||||
// until the end. Since we'll probably have few updates after the first run,
|
// until the end. Since we'll probably have few updates after the first run,
|
||||||
// this should mean that we rarely have to hold the write lock.
|
// this should mean that we rarely have to hold the write lock.
|
||||||
|
|
@ -141,23 +141,23 @@ func (n *resourceConverter) ResourcesForSeries(series prom.Series) ([]schema.Gro
|
||||||
|
|
||||||
// use an anon func to get the right defer behavior
|
// use an anon func to get the right defer behavior
|
||||||
func() {
|
func() {
|
||||||
n.labelResourceMu.RLock()
|
r.labelResourceMu.RLock()
|
||||||
defer n.labelResourceMu.RUnlock()
|
defer r.labelResourceMu.RUnlock()
|
||||||
|
|
||||||
for lbl := range series.Labels {
|
for lbl := range series.Labels {
|
||||||
var groupRes schema.GroupResource
|
var groupRes schema.GroupResource
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
// check if we have an override
|
// check if we have an override
|
||||||
if groupRes, ok = n.labelToResource[lbl]; ok {
|
if groupRes, ok = r.labelToResource[lbl]; ok {
|
||||||
resources = append(resources, groupRes)
|
resources = append(resources, groupRes)
|
||||||
} else if groupRes, ok = updates[lbl]; ok {
|
} else if groupRes, ok = updates[lbl]; ok {
|
||||||
resources = append(resources, groupRes)
|
resources = append(resources, groupRes)
|
||||||
} else if n.labelResExtractor != nil {
|
} else if r.labelResExtractor != nil {
|
||||||
// if not, check if it matches the form we expect, and if so,
|
// if not, check if it matches the form we expect, and if so,
|
||||||
// convert to a group-resource.
|
// convert to a group-resource.
|
||||||
if groupRes, ok = n.labelResExtractor.GroupResourceForLabel(lbl); ok {
|
if groupRes, ok = r.labelResExtractor.GroupResourceForLabel(lbl); ok {
|
||||||
info, _, err := provider.CustomMetricInfo{GroupResource: groupRes}.Normalized(n.mapper)
|
info, _, err := provider.CustomMetricInfo{GroupResource: groupRes}.Normalized(r.mapper)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("unable to normalize group-resource %s from label %q, skipping: %v", groupRes.String(), lbl, err)
|
glog.Errorf("unable to normalize group-resource %s from label %q, skipping: %v", groupRes.String(), lbl, err)
|
||||||
continue
|
continue
|
||||||
|
|
@ -180,11 +180,11 @@ func (n *resourceConverter) ResourcesForSeries(series prom.Series) ([]schema.Gro
|
||||||
// (plus, we don't care if someone else updates the cache first, since the results
|
// (plus, we don't care if someone else updates the cache first, since the results
|
||||||
// are necessarily the same, so at most we've done extra work).
|
// are necessarily the same, so at most we've done extra work).
|
||||||
if len(updates) > 0 {
|
if len(updates) > 0 {
|
||||||
n.labelResourceMu.Lock()
|
r.labelResourceMu.Lock()
|
||||||
defer n.labelResourceMu.Unlock()
|
defer r.labelResourceMu.Unlock()
|
||||||
|
|
||||||
for lbl, groupRes := range updates {
|
for lbl, groupRes := range updates {
|
||||||
n.labelToResource[lbl] = groupRes
|
r.labelToResource[lbl] = groupRes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue