Publishing Custom Metric Data
A number of AWS services publish their own metrics in namespaces beginning
with AWS/
You can also publish custom metric data using your own namespace (as long as it doesn’t
begin with AWS/
).
Prerequisites
Before you begin, we recommend you read Getting started using the AWS SDK for C++.
Download the example code and build the solution as described in Get started on code examples.
To run the examples, the user profile your code uses to make the requests must have proper permissions in AWS (for the service and the action). For more information, see Providing AWS credentials.
Publish Custom Metric Data
To publish your own metric data, call the CloudWatchClient’s PutMetricData
function with a
PutMetricDataRequestPutMetricDataRequest
must include the custom namespace to use for the data, and
information about the data point itself in a MetricDatum
Note
You cannot specify a namespace that begins with AWS/
. Namespaces that begin with
AWS/
are reserved for use by Amazon Web Services products.
Includes
#include <aws/core/Aws.h>
#include <aws/monitoring/CloudWatchClient.h>
#include <aws/monitoring/model/PutMetricDataRequest.h>
#include <iostream>
Code
Aws::CloudWatch::CloudWatchClient cw;
Aws::CloudWatch::Model::Dimension dimension;
dimension.SetName("UNIQUE_PAGES");
dimension.SetValue("URLS");
Aws::CloudWatch::Model::MetricDatum datum;
datum.SetMetricName("PAGES_VISITED");
datum.SetUnit(Aws::CloudWatch::Model::StandardUnit::None);
datum.SetValue(data_point);
datum.AddDimensions(dimension);
Aws::CloudWatch::Model::PutMetricDataRequest request;
request.SetNamespace("SITE/TRAFFIC");
request.AddMetricData(datum);
auto outcome = cw.PutMetricData(request);
if (!outcome.IsSuccess())
{
std::cout << "Failed to put sample metric data:" <<
outcome.GetError().GetMessage() << std::endl;
}
else
{
std::cout << "Successfully put sample metric data" << std::endl;
}
See the complete example
More Information
-
Using Amazon CloudWatch Metrics in the Amazon CloudWatch User Guide.
-
AWS Namespaces in the Amazon CloudWatch User Guide.
-
PutMetricData in the Amazon CloudWatch API Reference.