Skip to content

Adding dimensions to metrics in PutMetricDataRequest results in a MalformedInput exception #26

Closed
@ghost

Description

When constructing a MetricDatum for CloudWatch, it seems that adding any dimensions to the metric will result in errors upon attempting to submit the data to CloudWatch via the CloudWatchClient.PutMetricData(...) function.

It looks like the issue is that the output format generated by the CloudWatchClient does not match the specification here:

https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html

The code below generates the following request body:

Action=PutMetricData
&Namespace=MyMetricTest
&MetricData.member.1.MetricName=MyMetricName
&MetricData.member.1.Dimensions.Name=MyDimensionName
&MetricData.member.1.Dimensions.Value=MyDimensionValue
&MetricData.member.1.Value=100
&Version=2010-08-01

But according to the specification, it looks like it should actually be generating this request body instead (Notice the extra ".member.1" after the "Dimensions" string):

Action=PutMetricData
&Namespace=MyMetricTest
&MetricData.member.1.MetricName=MyMetricName
&MetricData.member.1.Dimensions.member.1.Name=MyDimensionName
&MetricData.member.1.Dimensions.member.1.Value=MyDimensionValue
&MetricData.member.1.Value=100
&Version=2010-08-01

Here is a code snippet to fully reproduce the issue:

#include <iostream>

#include "aws/core/auth/AWSCredentialsProvider.h"
#include "aws/monitoring/CloudWatchClient.h"
#include "aws/monitoring/model/PutMetricDataRequest.h"

using namespace Aws;
using namespace Aws::Auth;
using namespace Aws::CloudWatch;
using namespace Aws::CloudWatch::Model;
using namespace Aws::Http;
using namespace Aws::Client;
using namespace std::chrono;

int main()
{
    /*
    This code results in the following output:

    ERROR: MalformedInput::Unable to parse ExceptionName: MalformedInput Message: Unexpected complex element termination
    Transmission failure
    */

    const char* accessKey = "MY_ACCESS_KEY";
    const char* secretKey = "MY_SECRET_KEY";

    AWSCredentials basicCreds(accessKey, secretKey);
    CloudWatchClient client = CloudWatchClient(basicCreds);
    PutMetricDataRequest request;
    request.SetNamespace("MyMetricTest");

    MetricDatum datum;
    datum.SetMetricName("MyMetricName");
    datum.SetValue(100.0);

    Dimension dim;
    dim.SetName("MyDimensionName");
    dim.SetValue("MyDimensionValue");

    //This line is the problem!
    datum.AddDimensions(dim);

    Aws::Vector<MetricDatum> metrics;
    metrics.push_back(datum);
    request.SetMetricData(metrics);

    char requestDump[4096];
    request.GetBody()->getline(requestDump,4096);
    std::cout << "REQUEST = " << requestDump << std::endl;

    PutMetricDataOutcome outcome = client.PutMetricData(request);
    if (!outcome.IsSuccess())
    {
        std::cout << "ERROR: " << outcome.GetError().GetExceptionName() << "::" << outcome.GetError().GetMessage() << std::endl;
    }
    std::cout << "Transmission " << (outcome.IsSuccess() ? "success" : "failure") << std::endl;

    char dummy[256];
    std::cin.getline(dummy, 256);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions