mirror of
https://github.com/zalando-incubator/kube-metrics-adapter.git
synced 2024-12-22 19:16:06 +00:00
Compile regex at plugin level for time optimization
Signed-off-by: Lucas Thiesen <lucas.thiesen@zalando.de>
This commit is contained in:
parent
cd986058e4
commit
ff6d479f1a
@ -18,6 +18,7 @@ const (
|
||||
type HostnameCollectorPlugin struct {
|
||||
metricName string
|
||||
promPlugin CollectorPlugin
|
||||
pattern *regexp.Regexp
|
||||
}
|
||||
|
||||
type HostnameCollector struct {
|
||||
@ -33,9 +34,15 @@ func NewHostnameCollectorPlugin(
|
||||
return nil, fmt.Errorf("Failed to initialize hostname collector plugin, metric name was not defined")
|
||||
}
|
||||
|
||||
p, err := regexp.Compile("^[a-zA-Z0-9.-]+$")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to create regular expression to match hostname format")
|
||||
}
|
||||
|
||||
return &HostnameCollectorPlugin{
|
||||
metricName: metricName,
|
||||
promPlugin: promPlugin,
|
||||
pattern: p,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -56,13 +63,13 @@ func (p *HostnameCollectorPlugin) NewCollector(
|
||||
if _, ok := config.Config["hostnames"]; !ok {
|
||||
return nil, fmt.Errorf("Hostname is not specified, unable to create collector")
|
||||
}
|
||||
regex, err := regexp.Compile("^[a-zA-Z0-9.-]+$")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to create regular expression to match hostname format")
|
||||
}
|
||||
|
||||
hostnames := strings.Split(config.Config["hostnames"], ",")
|
||||
if p.pattern == nil {
|
||||
return nil, fmt.Errorf("Plugin did not specify hostname regex pattern, unable to create collector")
|
||||
}
|
||||
for _, h := range hostnames {
|
||||
if ok := regex.MatchString(h); !ok {
|
||||
if ok := p.pattern.MatchString(h); !ok {
|
||||
return nil, fmt.Errorf(
|
||||
"Invalid hostname format, unable to create collector: %s",
|
||||
h,
|
||||
|
@ -2,6 +2,7 @@ package collector
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -42,9 +43,13 @@ func TestHostnameCollectorPluginConstructor(tt *testing.T) {
|
||||
func TestHostnamePluginNewCollector(tt *testing.T) {
|
||||
fakePlugin := &FakeCollectorPlugin{}
|
||||
|
||||
pattern, err := regexp.Compile("^[a-zA-Z0-9.-]+$")
|
||||
require.Nil(tt, err, "Something is up, regex compiling failed.")
|
||||
|
||||
plugin := &HostnameCollectorPlugin{
|
||||
metricName: "a_valid_one",
|
||||
promPlugin: fakePlugin,
|
||||
pattern: pattern,
|
||||
}
|
||||
interval := time.Duration(42)
|
||||
|
||||
@ -169,9 +174,12 @@ func TestHostnameCollectorGetMetrics(tt *testing.T) {
|
||||
func TestHostnameCollectorInterval(t *testing.T) {
|
||||
interval := time.Duration(42)
|
||||
fakePlugin := &FakeCollectorPlugin{}
|
||||
pattern, err := regexp.Compile("^[a-zA-Z0-9.-]+$")
|
||||
require.Nil(t, err, "Something is up, regex compiling failed.")
|
||||
plugin := &HostnameCollectorPlugin{
|
||||
metricName: "a_valid_one",
|
||||
promPlugin: fakePlugin,
|
||||
pattern: pattern,
|
||||
}
|
||||
c, err := plugin.NewCollector(
|
||||
&autoscalingv2.HorizontalPodAutoscaler{},
|
||||
@ -179,8 +187,8 @@ func TestHostnameCollectorInterval(t *testing.T) {
|
||||
interval,
|
||||
)
|
||||
|
||||
require.NotNil(t, c)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, c)
|
||||
require.Equal(t, interval, c.Interval())
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user