From a012250b3a9bc403ae838f2555023166e456ab5f Mon Sep 17 00:00:00 2001 From: Lucas Thiesen Date: Mon, 8 May 2023 16:16:23 +0200 Subject: [PATCH] Compile regex at plugin level for time optimization Signed-off-by: Lucas Thiesen --- pkg/collector/hostname_collector.go | 17 ++++++++++++----- pkg/collector/hostname_collector_test.go | 10 +++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/pkg/collector/hostname_collector.go b/pkg/collector/hostname_collector.go index 20ace385..d52f8152 100644 --- a/pkg/collector/hostname_collector.go +++ b/pkg/collector/hostname_collector.go @@ -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, diff --git a/pkg/collector/hostname_collector_test.go b/pkg/collector/hostname_collector_test.go index c9a4d5e9..c26ace7a 100644 --- a/pkg/collector/hostname_collector_test.go +++ b/pkg/collector/hostname_collector_test.go @@ -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()) }