From fef9cdbb7a7f4562f67d3ed7a95d361d7ed301a5 Mon Sep 17 00:00:00 2001 From: Alex Eftimie Date: Sun, 14 May 2023 05:58:33 +0200 Subject: [PATCH] fix: properly wrap Datadog API v2 request body (#2771) (#2775) * Datadog: properly wrap request body Signed-off-by: Alex Eftimie * Use milliseconds in v2 calls to datadog Signed-off-by: Alex Eftimie --------- Signed-off-by: Alex Eftimie --- metricproviders/datadog/datadog.go | 27 ++++++++++++++--------- metricproviders/datadog/datadogV2_test.go | 16 +++++++------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/metricproviders/datadog/datadog.go b/metricproviders/datadog/datadog.go index 232dc59083..00f9c8a8ac 100644 --- a/metricproviders/datadog/datadog.go +++ b/metricproviders/datadog/datadog.go @@ -56,6 +56,10 @@ type datadogQuery struct { QueryType string `json:"type"` } +type datadogRequest struct { + Data datadogQuery `json:"data"` +} + type datadogResponseV1 struct { Series []struct { Pointlist [][]float64 `json:"pointlist"` @@ -182,17 +186,18 @@ func (p *Provider) createRequest(query string, apiVersion string, now int64, int return &http.Request{Method: "GET"}, nil } else if apiVersion == "v2" { - queryBody, err := json.Marshal(datadogQuery{ - QueryType: "timeseries_request", - Attributes: datadogQueryAttributes{ - From: now - interval, - To: now, - Queries: []map[string]string{{ - "data_source": "metrics", - "query": query, - }}, - }, - }) + queryBody, err := json.Marshal(datadogRequest{ + Data: datadogQuery{ + QueryType: "timeseries_request", + Attributes: datadogQueryAttributes{ + From: (now - interval) * 1000, + To: now * 1000, + Queries: []map[string]string{{ + "data_source": "metrics", + "query": query, + }}, + }, + }}) if err != nil { return nil, fmt.Errorf("Could not parse your JSON request: %v", err) } diff --git a/metricproviders/datadog/datadogV2_test.go b/metricproviders/datadog/datadogV2_test.go index c6e04fab3d..11a82411c7 100644 --- a/metricproviders/datadog/datadogV2_test.go +++ b/metricproviders/datadog/datadogV2_test.go @@ -245,28 +245,28 @@ func TestRunSuiteV2(t *testing.T) { t.Errorf("\nreceived no bytes in request: %v", err) } - var reqBody datadogQuery + var reqBody datadogRequest err = json.Unmarshal(bodyBytes, &reqBody) if err != nil { t.Errorf("\nCould not parse JSON request body: %v", err) } - actualQuery := reqBody.Attributes.Queries[0]["query"] - actualFrom := reqBody.Attributes.From - actualTo := reqBody.Attributes.To + actualQuery := reqBody.Data.Attributes.Queries[0]["query"] + actualFrom := reqBody.Data.Attributes.From + actualTo := reqBody.Data.Attributes.To if actualQuery != "avg:kubernetes.cpu.user.total{*}" { t.Errorf("\nquery expected avg:kubernetes.cpu.user.total{*} but got %s", actualQuery) } - if actualFrom != unixNow()-test.expectedIntervalSeconds { - t.Errorf("\nfrom %d expected be equal to %d", actualFrom, unixNow()-test.expectedIntervalSeconds) + if actualFrom != (unixNow()-test.expectedIntervalSeconds)*1000 { + t.Errorf("\nfrom %d expected be equal to %d", actualFrom, (unixNow()-test.expectedIntervalSeconds)*1000) } else if err != nil { t.Errorf("\nfailed to parse from: %v", err) } - if actualTo != unixNow() { - t.Errorf("\nto %d was expected be equal to %d", actualTo, unixNow()) + if actualTo != unixNow()*1000 { + t.Errorf("\nto %d was expected be equal to %d", actualTo, unixNow()*1000) } else if err != nil { t.Errorf("\nfailed to parse to: %v", err) }