Skip to content

datadogV2.GetMonthlyCostAttribution: fail to parse http response to MonthlyCostAttributionResponse due to different in time format #3016

@nvatuan

Description

@nvatuan

Describe the bug

I was following the example from the official usage-metering docs on get-monthly-cost-attribution and I cannot seem to retrieve the Values field in the Response object.

MonthlyCostAttributionResponse field Attributes.Values is nil after successfully fetched and unmarshalled.

To Reproduce
Steps to reproduce the behavior:

  1. The program to replicate the issue:
`main.go`
package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
	"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
	"github.com/sirupsen/logrus"
)

var log = logrus.New()

func main() {
	// https://docs.datadoghq.com/api/latest/usage-metering/#get-monthly-cost-attribution example
	ctx := datadog.NewDefaultContext(context.Background())
	configuration := datadog.NewConfiguration()
	apiClient := datadog.NewAPIClient(configuration)
	api := datadogV2.NewUsageMeteringApi(apiClient)

	// go back 1 month and 20 days to ensure last month. Eg:
	// April 13th -> Get February data
	// April 20th -> Get March data
	timeToExtractMonth := time.Now().AddDate(0, -1, -20)
	log.Infof("Getting monthly cost attribution for %v...", timeToExtractMonth)

	resp, r, err := api.GetMonthlyCostAttribution(
		ctx,
		timeToExtractMonth,
		"*",
	)

	log.Infof("resp status: %v", r.StatusCode) // => 200

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error when calling `UsageMeteringApi.GetMonthlyCostAttribution`: %v\n", err)
		fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
	}

	data, ok := resp.GetDataOk() // => ok
	if !ok {
		log.Errorf("get data not ok")
		panic("get data not ok")
	}
	if len(*data) == 0 {
		log.Errorf("no data")
		panic("no data")
	}
	log.Infof("ok can get data (data len = %v)", len(*data)) // => ok
	//
	attrib, ok := (*data)[0].GetAttributesOk() // => ok
	if !ok {
		log.Errorf("get attrib not ok")
		panic("get attrib not ok")
	}
	log.Infof("ok can get attrib") // => ok
	//
	values, ok := attrib.GetValuesOk() // => NOT OK, values is nil
	log.Infof("values: %v", values)    // => nil
	if !ok {
		log.Errorf("get values not ok")
		panic("get values not ok")
	}
	log.Infof("ok can get values")
	//
	valuesMap, ok := (*values).(map[string]interface{})
	if !ok {
		log.Errorf("values is not a map, resp: %v", resp)
		panic("values is not a map")
	}
	log.Infof("values len: %v", len(valuesMap))
}
  1. Runs with appropriate env:
DD_API_KEY="<datadog-api-key>" DD_APP_KEY="<datadog-app-key>" go run main.go
  1. Logs:
INFO[0000] Getting monthly cost attribution for 2025-02-12... 
INFO[0001] resp status: 200                             
INFO[0001] ok can get data (data len = 2)               
INFO[0001] ok can get attrib                            
INFO[0001] values: <nil>                                
ERRO[0001] get values not ok                            
panic: get values not ok

Expected behavior

The HTTP Response was successful, I confirmed the return data contains complete data and also data for the Values field.
There were no error in err, thus, I expected var monthlyCostAttributionData.Attributes.Values to not be nil after parsing.

Environment and Versions (please complete the following information):

Additional context
The Values data can be found in monthlyCostAttributionData.Attributes.UnparsedObject, so I suspect parsing has failed at some point, but I have debugged up until the json.Unmarshal call and I couldn't find anything suspicous.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugBug related issuestaleStale - Bot reminder

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions