From 489053e0f0cd44d025ede53249e3e18763df6c84 Mon Sep 17 00:00:00 2001 From: mdsjip <2284562+mdsjip@users.noreply.github.com> Date: Sun, 30 Apr 2023 17:47:05 +0200 Subject: [PATCH 1/2] fix(analysis): Graphite query - remove whitespaces Signed-off-by: mdsjip <2284562+mdsjip@users.noreply.github.com> --- metricproviders/graphite/api.go | 2 +- metricproviders/graphite/api_test.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/metricproviders/graphite/api.go b/metricproviders/graphite/api.go index fc304395b1..03a0d22243 100644 --- a/metricproviders/graphite/api.go +++ b/metricproviders/graphite/api.go @@ -77,7 +77,7 @@ func (api APIClient) Query(quer string) ([]dataPoint, error) { func (api APIClient) trimQuery(q string) string { space := regexp.MustCompile(`\s+`) - return space.ReplaceAllString(q, " ") + return space.ReplaceAllString(q, "") } type dataPoint struct { diff --git a/metricproviders/graphite/api_test.go b/metricproviders/graphite/api_test.go index bca62bbae2..ed2d61adea 100644 --- a/metricproviders/graphite/api_test.go +++ b/metricproviders/graphite/api_test.go @@ -91,6 +91,29 @@ func TestQuery(t *testing.T) { nil, `[]`, 200, + }, { + "query with surrounding whitespace", + fmt.Sprintf("\n %s \t \n", query), + targetQuery, + fromQuery, + goodResult, + nil, + fmt.Sprintf(`[ + { + "datapoints": [ + [ + %f, + %d + ] + ], + "target": " sumSeries(app.http.*.*.count)", + "tags": { + "aggregatedBy": "sum", + "name": "sumSeries(app.http.*.*.count)" + } + } + ]`, value, timestamp), + 200, }, { "graphite response body with invalid JSON", query, From 628842f0b2f45d20ba46f1480659bec4eab72ab1 Mon Sep 17 00:00:00 2001 From: mdsjip <2284562+mdsjip@users.noreply.github.com> Date: Sun, 30 Apr 2023 19:18:21 +0200 Subject: [PATCH 2/2] refactor(analysis): Graphite query - proper naming and regexp usage Signed-off-by: mdsjip <2284562+mdsjip@users.noreply.github.com> --- metricproviders/graphite/api.go | 9 +++++---- metricproviders/graphite/api_test.go | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/metricproviders/graphite/api.go b/metricproviders/graphite/api.go index 03a0d22243..06833ac028 100644 --- a/metricproviders/graphite/api.go +++ b/metricproviders/graphite/api.go @@ -27,9 +27,11 @@ type APIClient struct { logCTX log.Entry } +var spaceRegex = regexp.MustCompile(`\s+`) + // Query performs a Graphite API query with the query it's passed func (api APIClient) Query(quer string) ([]dataPoint, error) { - query := api.trimQuery(quer) + query := api.sanitizeQuery(quer) u, err := url.Parse(fmt.Sprintf("./render?%s", query)) if err != nil { return []dataPoint{}, err @@ -75,9 +77,8 @@ func (api APIClient) Query(quer string) ([]dataPoint, error) { return result[0].DataPoints, nil } -func (api APIClient) trimQuery(q string) string { - space := regexp.MustCompile(`\s+`) - return space.ReplaceAllString(q, "") +func (api APIClient) sanitizeQuery(q string) string { + return spaceRegex.ReplaceAllLiteralString(q, "") } type dataPoint struct { diff --git a/metricproviders/graphite/api_test.go b/metricproviders/graphite/api_test.go index ed2d61adea..74f0ef1176 100644 --- a/metricproviders/graphite/api_test.go +++ b/metricproviders/graphite/api_test.go @@ -106,7 +106,7 @@ func TestQuery(t *testing.T) { %d ] ], - "target": " sumSeries(app.http.*.*.count)", + "target": "sumSeries(app.http.*.*.count)", "tags": { "aggregatedBy": "sum", "name": "sumSeries(app.http.*.*.count)"