This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
metric_object.h
167 lines (150 loc) · 6.11 KB
/
metric_object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <list>
#include <map>
#include <string>
#include <aws/monitoring/model/PutMetricDataRequest.h>
#include <aws/monitoring/model/StandardUnit.h>
#include <cloudwatch_metrics_common/definitions/definitions.h>
namespace Aws {
namespace CloudWatchMetrics {
namespace Utils {
static std::unordered_map<std::string, Aws::CloudWatch::Model ::StandardUnit> units_mapper = {
{"sec", Aws::CloudWatch::Model::StandardUnit::Seconds},
{"msec", Aws::CloudWatch::Model::StandardUnit::Milliseconds},
{"usec", Aws::CloudWatch::Model::StandardUnit::Microseconds},
{"percent", Aws::CloudWatch::Model::StandardUnit::Percent},
{"count", Aws::CloudWatch::Model::StandardUnit::Count},
{"count_per_sec", Aws::CloudWatch::Model::StandardUnit::Count_Second},
{"bytes", Aws::CloudWatch::Model::StandardUnit::Bytes},
{"kilobytes", Aws::CloudWatch::Model::StandardUnit::Kilobytes},
{"megabytes", Aws::CloudWatch::Model::StandardUnit::Megabytes},
{"gigabytes", Aws::CloudWatch::Model::StandardUnit::Gigabytes},
{"terabytes", Aws::CloudWatch::Model::StandardUnit::Terabytes},
{"bytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Bytes_Second},
{"kilobytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Kilobytes_Second},
{"megabytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Megabytes_Second},
{"gigabytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Gigabytes_Second},
{"terabytes_per_sec", Aws::CloudWatch::Model::StandardUnit::Terabytes_Second},
{"none", Aws::CloudWatch::Model::StandardUnit::None},
{"", Aws::CloudWatch::Model::StandardUnit::None},
};
enum class StatisticValuesType
{
MINIMUM,
MAXIMUM,
SUM,
SAMPLE_COUNT
};
/**
* Wrapper object for the AWS specific Aws::CloudWatch::Model::MetricDatum. This object is meant to be constructed from
* userland provided metric data instead of using the AWS SDK specific object.
*/
struct MetricObject {
/**
* Construct a new instance of MetricObject that contains a scalar value.
*
* @param _name metric name
* @param _value metric scalar value
* @param _unit metric unit
* @param _timestamp metric time stamp
* @param _dimensions dimensions delineating extra details about the metric
* @param _storage_resolution
*/
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)
: metric_name(_name), unit(_unit), timestamp(_timestamp), value(_value),
dimensions(_dimensions), storage_resolution(_storage_resolution) {}
/**
* Construct a new instance of MetricObject that contains a statistical values.
*
* @param _name metric name
* @param _statistic_values statistical values of the metric
* @param _unit metric unit
* @param _timestamp metric time stamp
* @param _dimensions dimensions delineating extra details about the metric
* @param _storage_resolution
*/
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)
: metric_name(_name), unit(_unit), timestamp(_timestamp), statistic_values(_statistic_values),
dimensions(_dimensions), storage_resolution(_storage_resolution) {}
std::string metric_name;
std::string unit;
int64_t timestamp;
double value; // mutually exclusive with statistic_values
std::map<StatisticValuesType, double> statistic_values; // mutually exclusive with value
std::map<std::string, std::string> dimensions;
int storage_resolution;
};
/**
* Helper method to constructor an Aws::CloudWatch::Model::MetricDatum from a MetricObject.
*
* @param metrics input MetricObject
* @param timestamp
* @return Aws::CloudWatch::Model::MetricDatum
*/
static MetricDatum metricObjectToDatum(const MetricObject &metrics, const int64_t timestamp) {
MetricDatum datum;
Aws::String aws_metric_name(metrics.metric_name.c_str());
Aws::Utils::DateTime date_time(timestamp);
datum.WithMetricName(aws_metric_name).WithTimestamp(date_time);
if (metrics.statistic_values.empty()) {
datum.SetValue(metrics.value);
} else {
Aws::CloudWatch::Model::StatisticSet stats;
for (const auto & keyval : metrics.statistic_values) {
if (keyval.first == StatisticValuesType::MINIMUM) {
stats.SetMinimum(keyval.second);
} else if (keyval.first == StatisticValuesType::MAXIMUM) {
stats.SetMaximum(keyval.second);
} else if (keyval.first == StatisticValuesType::SUM) {
stats.SetSum(keyval.second);
} else if (keyval.first == StatisticValuesType::SAMPLE_COUNT) {
stats.SetSampleCount(keyval.second);
}
}
datum.SetStatisticValues(std::move(stats));
}
auto mapped_unit = units_mapper.find(metrics.unit);
if (units_mapper.end() != mapped_unit) {
datum.SetUnit(mapped_unit->second);
} else {
Aws::String unit_name(metrics.unit.c_str());
datum.SetUnit(Aws::CloudWatch::Model::StandardUnitMapper::GetStandardUnitForName(unit_name));
}
for (auto it = metrics.dimensions.begin(); it != metrics.dimensions.end(); ++it) {
Aws::CloudWatch::Model::Dimension dimension;
Aws::String name(it->first.c_str());
Aws::String d_value(it->second.c_str());
dimension.WithName(name).WithValue(d_value);
datum.AddDimensions(dimension);
}
datum.SetStorageResolution(metrics.storage_resolution);
return datum;
}
} // namespace Utils
} // namespace CloudWatchMetrics
} // namespace Aws