2021-11-15 13:31:25 +00:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2021-11-15 13:15:43 +00:00
2021-12-17 14:41:43 +00:00
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: put_metric_data.cpp demonstrates how to submit Amazon CloudWatch metric data.
*
* Prerequisites:
2021-12-21 13:36:46 +00:00
* An Amazon CloudWatch metric.
2021-12-17 14:41:43 +00:00
*
* Inputs:
2021-12-17 17:57:59 +00:00
* - value_stream: The CloudWatch metric stream (entered as the second argument in the command line).
2021-12-17 14:41:43 +00:00
*
* ///////////////////////////////////////////////////////////////////////// */
2021-11-15 13:15:43 +00:00
// snippet-start:[cw.cpp.put_metric_data.inc]
# include <aws/core/Aws.h>
# include <aws/monitoring/CloudWatchClient.h>
# include <aws/monitoring/model/PutMetricDataRequest.h>
# include <iostream>
// snippet-end:[cw.cpp.put_metric_data.inc]
/**
* Attempts to put a sample metric data point with value based on command line input
*/
int main ( int argc , char * * argv )
{
if ( argc ! = 2 )
{
std : : cout < < " Usage: put_metric_data <data_point_value> " < <
std : : endl ;
return 1 ;
}
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
Aws : : StringStream value_stream ( argv [ 1 ] ) ;
double data_point = 1.0 ;
value_stream > > data_point ;
// snippet-start:[cw.cpp.put_metric_data.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 ;
}
// snippet-end:[cw.cpp.put_metric_data.code]
}
Aws : : ShutdownAPI ( options ) ;
return 0 ;
}