Skip to content

Commit

Permalink
chore: use different metric default name (kedacore#1429)
Browse files Browse the repository at this point in the history
* chore: use different metric default name

Signed-off-by: Yoofi Quansah <yqsummon@gmail.com>

* chore: fix for masking server url and connection string

Signed-off-by: Yoofi Quansah <yqsummon@gmail.com>
  • Loading branch information
yquansah authored and ycabrer committed Mar 1, 2021
1 parent 2db6c0e commit 3ded87f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
24 changes: 16 additions & 8 deletions pkg/scalers/influxdb_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package scalers
import (
"context"
"fmt"
"net/url"
"strconv"
"time"

influxdb2 "github.com/influxdata/influxdb-client-go/v2"
api "github.com/influxdata/influxdb-client-go/v2/api"
Expand Down Expand Up @@ -71,12 +71,6 @@ func parseInfluxDBMetadata(config *ScalerConfig) (*influxDBMetadata, error) {
return nil, fmt.Errorf("no auth token given")
}

if val, ok := config.TriggerMetadata["metricName"]; ok {
metricName = val
} else {
metricName = strconv.FormatInt(time.Now().UTC().Unix(), 10)
}

val, ok = config.TriggerMetadata["organizationName"]
switch {
case ok && val != "":
Expand All @@ -103,6 +97,20 @@ func parseInfluxDBMetadata(config *ScalerConfig) (*influxDBMetadata, error) {
return nil, fmt.Errorf("no server url given")
}

if val, ok := config.TriggerMetadata["metricName"]; ok {
metricName = val
} else {
parsedURL, err := url.Parse(serverURL)
if err != nil {
return nil, fmt.Errorf("unable to parse server url")
}

metricName, err = kedautil.MaskPartOfURL(parsedURL.String(), kedautil.Hostname)
if err != nil {
return nil, fmt.Errorf("failure masking part of url")
}
}

if val, ok := config.TriggerMetadata["thresholdValue"]; ok {
value, err := strconv.ParseFloat(val, 10)
if err != nil {
Expand Down Expand Up @@ -187,7 +195,7 @@ func (s *influxDBScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetMetricValue := resource.NewQuantity(int64(s.metadata.thresholdValue), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s", "influxdb", s.metadata.organizationName, s.metadata.metricName)),
Name: kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s", "influxdb", s.metadata.metricName, s.metadata.organizationName)),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
3 changes: 2 additions & 1 deletion pkg/scalers/influxdb_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var testInfluxDBMetadata = []parseInfluxDBMetadataTestData{
{map[string]string{"serverURL": "https://influxdata.com", "organizationName": "influx_org", "query": "from(bucket: hello)", "thresholdValue": "10"}, true}}

var influxDBMetricIdentifiers = []influxDBMetricIdentifier{
{&testInfluxDBMetadata[1], "influxdb-influx_org-influx_metric"},
{&testInfluxDBMetadata[1], "influxdb-influx_metric-influx_org"},
{&testInfluxDBMetadata[2], "influxdb-https---xxx-influx_org"},
}

func TestInfluxDBParseMetadata(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/scalers/postgresql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func parsePostgreSQLMetadata(config *ScalerConfig) (*postgreSQLMetadata, error)
meta.metricName = kedautil.NormalizeString(fmt.Sprintf("postgresql-%s", val))
} else {
if meta.connection != "" {
maskedConnectionString, err := kedautil.MaskPassword(meta.connection)
maskedConnectionString, err := kedautil.MaskPartOfURL(meta.connection, kedautil.Password)
if err != nil {
return nil, fmt.Errorf("url parsing error %s", err.Error())
}
Expand Down
29 changes: 25 additions & 4 deletions pkg/util/normalize_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import (
"strings"
)

type urlPart string

const (
// Hostname is a constant refers to the hostname part of the url
Hostname urlPart = "Hostname"
// Password is a constant that refers to a password portion of the url if there is one
Password urlPart = "Password"
)

// NormalizeString will replace all slashes, dots, colons and percent signs with dashes
func NormalizeString(s string) string {
s = strings.ReplaceAll(s, "/", "-")
Expand All @@ -14,15 +23,27 @@ func NormalizeString(s string) string {
return s
}

// MaskPassword will parse a url and returned a masked version or an error
func MaskPassword(s string) (string, error) {
// MaskPartOfURL will parse a url and returned a masked version or an error
func MaskPartOfURL(s string, part urlPart) (string, error) {
url, err := url.Parse(s)
if err != nil {
return "", err
}

if password, ok := url.User.Password(); ok {
return strings.ReplaceAll(s, password, "xxx"), nil
var partOfURL string
switch part {
case Hostname:
hostname := url.Hostname()
partOfURL = hostname
case Password:
password, ok := url.User.Password()
if ok {
partOfURL = password
}
}

if partOfURL != "" {
return strings.ReplaceAll(s, partOfURL, "xxx"), nil
}

return s, nil
Expand Down

0 comments on commit 3ded87f

Please sign in to comment.