Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(analysis): Graphite query - remove whitespaces #2752

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions metricproviders/graphite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions metricproviders/graphite/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down