Skip to content

Commit

Permalink
Refactor in response to code smell score
Browse files Browse the repository at this point in the history
  • Loading branch information
meeech committed Sep 6, 2023
1 parent 417866a commit d4cf3e7
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions metricproviders/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,16 @@ func (p *Provider) Run(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alph
return metricutil.MarkMeasurementError(measurement, err)
}

apiVersion := DefaultApiVersion
if dd.ApiVersion != "" {
apiVersion = dd.ApiVersion
if dd.ApiVersion == "" {
dd.ApiVersion = DefaultApiVersion
}

if apiVersion == "v1" {
if dd.ApiVersion == "v1" {
p.logCtx.Warn("Datadog will soon deprecate their API v1. Please consider switching to v2 soon.")
}

route := "/api/v1/query"
if apiVersion == "v2" {
if dd.ApiVersion == "v2" {
route = "/api/v2/query/timeseries"
}

Expand All @@ -146,19 +145,7 @@ func (p *Provider) Run(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alph
}

var request *http.Request
if apiVersion == "v1" {
if dd.Query == "" {
return metricutil.MarkMeasurementError(measurement, errors.New("Query is empty. v1 requires using the query parameter in your Analysis Template"))
}
request, err = p.createRequestV1(dd.Query, now, interval, url)
} else if apiVersion == "v2" {
// we know dd.Query and ddQueries are mutually exclusive in the spec
if dd.Query != "" {
dd.Queries = map[string]string{"query": dd.Query}
}

request, err = p.createRequestV2(dd.Queries, dd.Formula, now, interval, url)
}
request, err = p.createRequest(dd, now, interval, url)

if err != nil {
return metricutil.MarkMeasurementError(measurement, err)
Expand All @@ -179,7 +166,7 @@ func (p *Provider) Run(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alph
return metricutil.MarkMeasurementError(measurement, err)
}

value, status, err := p.parseResponse(metric, response, apiVersion)
value, status, err := p.parseResponse(metric, response, dd.ApiVersion)
if err != nil {
return metricutil.MarkMeasurementError(measurement, err)
}
Expand All @@ -192,6 +179,22 @@ func (p *Provider) Run(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alph
return measurement
}

func (p *Provider) createRequest(dd *v1alpha1.DatadogMetric, now int64, interval int64, url *url.URL) (*http.Request, error) {
if dd.ApiVersion == "v1" {
if dd.Query == "" {
return nil, errors.New("Query is empty. v1 requires using the query parameter in your Analysis Template")

Check warning on line 185 in metricproviders/datadog/datadog.go

View check run for this annotation

Codecov / codecov/patch

metricproviders/datadog/datadog.go#L185

Added line #L185 was not covered by tests
}
return p.createRequestV1(dd.Query, now, interval, url)
}

// we know dd.Query and ddQueries are mutually exclusive in the spec
if dd.Query != "" {
dd.Queries = map[string]string{"query": dd.Query}
}

return p.createRequestV2(dd.Queries, dd.Formula, now, interval, url)
}

func (p *Provider) createRequestV1(query string, now int64, interval int64, url *url.URL) (*http.Request, error) {
q := url.Query()
q.Set("query", query)
Expand Down

0 comments on commit d4cf3e7

Please sign in to comment.