Skip to content

Commit

Permalink
Compile regex at plugin level for time optimization
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Thiesen <[email protected]>
  • Loading branch information
lucastt committed May 8, 2023
1 parent cae3fff commit a012250
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
17 changes: 12 additions & 5 deletions pkg/collector/hostname_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
type HostnameCollectorPlugin struct {
metricName string
promPlugin CollectorPlugin
pattern *regexp.Regexp
}

type HostnameCollector struct {
Expand All @@ -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
}

Expand All @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion pkg/collector/hostname_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collector

import (
"fmt"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -169,18 +174,21 @@ 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{},
&MetricConfig{Config: map[string]string{"hostnames": "foo.bar.baz"}},
interval,
)

require.NotNil(t, c)
require.Nil(t, err)
require.NotNil(t, c)
require.Equal(t, interval, c.Interval())
}

Expand Down

0 comments on commit a012250

Please sign in to comment.