cloudwatch_metrics_facade.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <aws/core/Aws.h>
17 #include <aws/core/utils/logging/LogMacros.h>
18 
19 #include <aws/monitoring/model/MetricDatum.h>
20 #include <aws/monitoring/model/PutMetricDataRequest.h>
22 
24 
26 
27 #include <string>
28 
29 namespace Aws {
30 namespace CloudWatchMetrics {
31 namespace Utils {
32 
33 // https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html
34 #define MAX_METRIC_DATUMS_PER_REQUEST 20
35 
36 CloudWatchMetricsFacade::CloudWatchMetricsFacade(const Aws::Client::ClientConfiguration & client_config)
37 {
38  this->cw_client_ = std::make_shared<Aws::CloudWatch::CloudWatchClient>(client_config);
39 }
40 
41 CloudWatchMetricsFacade::CloudWatchMetricsFacade(const std::shared_ptr<Aws::CloudWatch::CloudWatchClient>& cw_client)
42 {
43  this->cw_client_ = cw_client;
44 }
45 
47  const Aws::CloudWatch::Model::PutMetricDataRequest & request)
48 {
49 
50  auto status = SUCCESS;
51  auto response = this->cw_client_->PutMetricData(request);
52 
53  if (!response.IsSuccess()) {
54 
55  AWS_LOGSTREAM_DEBUG( __func__, "CloudWatchMetricsFacade: failed to send metric request: "
56  << static_cast<int>(response.GetError().GetErrorType()));
57 
58  switch(response.GetError().GetErrorType()) {
59  //case Aws::CloudWatch::CloudWatchErrors::REQUEST_TIMEOUT:
60  case Aws::CloudWatch::CloudWatchErrors::NETWORK_CONNECTION:
61  status = NETWORK_FAILURE;
62  break;
63  case Aws::CloudWatch::CloudWatchErrors::INVALID_PARAMETER_VALUE:
64  case Aws::CloudWatch::CloudWatchErrors::INVALID_PARAMETER_COMBINATION:
65  case Aws::CloudWatch::CloudWatchErrors::MISSING_PARAMETER:
66  status = INVALID_DATA;
67  break;
68  default:
69  status = FAILURE;
70  }
71  }
72  return status;
73 }
74 
76  const std::string & metric_namespace, MetricDatumCollection &metrics)
77 {
78  auto status = SUCCESS;
79  Aws::CloudWatch::Model::PutMetricDataRequest request;
80  Aws::Vector<Aws::CloudWatch::Model::MetricDatum> datums;
81 
82  if (metrics.empty()) {
83  AWS_LOGSTREAM_DEBUG( __func__, "CloudWatchMetricsFacade: no metrics to send");
84  return FAILURE;
85  }
86 
87  request.SetNamespace(metric_namespace.c_str());
88 
89  // Note: this fails an entire set of metrics, even if some are sent back successfully
90  for (auto & metric : metrics) {
91  datums.push_back(metric);
92  if (datums.size() >= MAX_METRIC_DATUMS_PER_REQUEST) {
93 
94  request.SetMetricData(datums);
95  status = SendMetricsRequest(request);
96 
97  // if offline don't attempt to send again (fail fast)
98  if (status == NETWORK_FAILURE) {
99  return status;
100  }
101  datums.clear();
102  }
103  }
104 
105  if (datums.size() > 0) {
106  request.SetMetricData(datums);
107  status = SendMetricsRequest(request);
108  }
109 
110  return status;
111 }
112 
113 } // namespace Utils
114 } // namespace CloudWatchMetrics
115 } // namespace Aws
std::list< Aws::CloudWatch::Model::MetricDatum > MetricDatumCollection
Definition: definitions.h:23
CloudWatchMetricsStatus SendMetricsRequest(const Aws::CloudWatch::Model::PutMetricDataRequest &request)
#define MAX_METRIC_DATUMS_PER_REQUEST
std::shared_ptr< Aws::CloudWatch::CloudWatchClient > cw_client_
virtual CloudWatchMetricsStatus SendMetricsToCloudWatch(const std::string &metric_namespace, MetricDatumCollection &metrics)
Sends a list of metrics to CloudWatch Used to send a list of metrics to CloudWatch.


cloudwatch_metrics_common
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:25