metric_object.h
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 <list>
17 #include <map>
18 #include <string>
19 
20 #include <aws/monitoring/model/PutMetricDataRequest.h>
21 #include <aws/monitoring/model/StandardUnit.h>
23 
24 namespace Aws {
25 namespace CloudWatchMetrics {
26 namespace Utils {
27 
28 static std::unordered_map<std::string, Aws::CloudWatch::Model ::StandardUnit> units_mapper = {
29  {"sec", Aws::CloudWatch::Model::StandardUnit::Seconds},
30  {"msec", Aws::CloudWatch::Model::StandardUnit::Milliseconds},
31  {"usec", Aws::CloudWatch::Model::StandardUnit::Microseconds},
32  {"percent", Aws::CloudWatch::Model::StandardUnit::Percent},
33  {"count", Aws::CloudWatch::Model::StandardUnit::Count},
34  {"count_per_sec", Aws::CloudWatch::Model::StandardUnit::Count_Second},
35  {"bytes", Aws::CloudWatch::Model::StandardUnit::Bytes},
36  {"kilobytes", Aws::CloudWatch::Model::StandardUnit::Kilobytes},
37  {"megabytes", Aws::CloudWatch::Model::StandardUnit::Megabytes},
38  {"gigabytes", Aws::CloudWatch::Model::StandardUnit::Gigabytes},
39  {"terabytes", Aws::CloudWatch::Model::StandardUnit::Terabytes},
40  {"bytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Bytes_Second},
41  {"kilobytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Kilobytes_Second},
42  {"megabytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Megabytes_Second},
43  {"gigabytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Gigabytes_Second},
44  {"terabytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Terabytes_Second},
45  {"none", Aws::CloudWatch::Model::StandardUnit::None},
46  {"", Aws::CloudWatch::Model::StandardUnit::None},
47 };
48 
50 {
51  MINIMUM,
52  MAXIMUM,
53  SUM,
55 };
56 
61 struct MetricObject {
73  const std::string & _name,
74  const double _value,
75  const std::string & _unit,
76  const int64_t _timestamp,
77  const std::map<std::string, std::string> & _dimensions,
78  const int _storage_resolution)
79  : metric_name(_name), unit(_unit), timestamp(_timestamp), value(_value),
80  dimensions(_dimensions), storage_resolution(_storage_resolution) {}
81 
93  const std::string & _name,
94  const std::map<StatisticValuesType, double> & _statistic_values,
95  const std::string & _unit,
96  const int64_t _timestamp,
97  const std::map<std::string, std::string> & _dimensions,
98  const int _storage_resolution)
99  : metric_name(_name), unit(_unit), timestamp(_timestamp), statistic_values(_statistic_values),
100  dimensions(_dimensions), storage_resolution(_storage_resolution) {}
101 
102  std::string metric_name;
103  std::string unit;
104  int64_t timestamp;
105  double value{}; // mutually exclusive with statistic_values
106  std::map<StatisticValuesType, double> statistic_values; // mutually exclusive with value
107  std::map<std::string, std::string> dimensions;
109 };
110 
118 static MetricDatum metricObjectToDatum(const MetricObject &metrics, const int64_t timestamp) {
119 
120  MetricDatum datum;
121  Aws::String aws_metric_name(metrics.metric_name.c_str());
122  Aws::Utils::DateTime date_time(timestamp);
123 
124  datum.WithMetricName(aws_metric_name).WithTimestamp(date_time);
125 
126  if (metrics.statistic_values.empty()) {
127  datum.SetValue(metrics.value);
128  } else {
129  Aws::CloudWatch::Model::StatisticSet stats;
130  for (const auto & keyval : metrics.statistic_values) {
131  if (keyval.first == StatisticValuesType::MINIMUM) {
132  stats.SetMinimum(keyval.second);
133  } else if (keyval.first == StatisticValuesType::MAXIMUM) {
134  stats.SetMaximum(keyval.second);
135  } else if (keyval.first == StatisticValuesType::SUM) {
136  stats.SetSum(keyval.second);
137  } else if (keyval.first == StatisticValuesType::SAMPLE_COUNT) {
138  stats.SetSampleCount(keyval.second);
139  }
140  }
141  datum.SetStatisticValues(stats);
142  }
143 
144  auto mapped_unit = units_mapper.find(metrics.unit);
145  if (units_mapper.end() != mapped_unit) {
146  datum.SetUnit(mapped_unit->second);
147  } else {
148  Aws::String unit_name(metrics.unit.c_str());
149  datum.SetUnit(Aws::CloudWatch::Model::StandardUnitMapper::GetStandardUnitForName(unit_name));
150  }
151 
152  for (const auto & it : metrics.dimensions) {
153  Aws::CloudWatch::Model::Dimension dimension;
154  Aws::String name(it.first.c_str());
155  Aws::String d_value(it.second.c_str());
156  dimension.WithName(name).WithValue(d_value);
157  datum.AddDimensions(dimension);
158  }
159 
160  datum.SetStorageResolution(metrics.storage_resolution);
161 
162  return datum;
163 }
164 
165 } // namespace Utils
166 } // namespace CloudWatchMetrics
167 } // namespace Aws
static std::unordered_map< std::string, Aws::CloudWatch::Model::StandardUnit > units_mapper
Definition: metric_object.h:28
std::map< std::string, std::string > dimensions
MetricObject(const std::string &_name, const std::map< StatisticValuesType, double > &_statistic_values, const std::string &_unit, const int64_t _timestamp, const std::map< std::string, std::string > &_dimensions, const int _storage_resolution)
Definition: metric_object.h:92
MetricObject(const std::string &_name, const double _value, const std::string &_unit, const int64_t _timestamp, const std::map< std::string, std::string > &_dimensions, const int _storage_resolution)
Definition: metric_object.h:72
Aws::CloudWatch::Model::MetricDatum MetricDatum
Definition: definitions.h:22
static MetricDatum metricObjectToDatum(const MetricObject &metrics, const int64_t timestamp)
std::map< StatisticValuesType, double > statistic_values


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