metrics_collector_parameter_helper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 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 
18 #include <ros/ros.h>
19 #include <unordered_set>
20 #include <iostream>
21 #include <aws/core/utils/logging/LogMacros.h>
24 
26 
27 
28 namespace Aws {
29 namespace CloudWatchMetrics {
30 namespace Utils {
31 
41  const std::shared_ptr<Aws::Client::ParameterReaderInterface>& parameter_reader,
42  double & publish_frequency) {
43 
44  Aws::AwsError ret =
45  parameter_reader->ReadParam(ParameterPath(kNodeParamPublishFrequencyKey), publish_frequency);
46 
47  switch (ret) {
48  case Aws::AwsError::AWS_ERR_NOT_FOUND:
49  publish_frequency = kNodePublishFrequencyDefaultValue;
50  AWS_LOGSTREAM_INFO(__func__,
51  "Publish frequency configuration not found, setting to default value: "
53  break;
54  case Aws::AwsError::AWS_ERR_OK:
55  AWS_LOGSTREAM_INFO(__func__, "Publish frequency is set to: " << publish_frequency);
56  break;
57  default:
58  publish_frequency = kNodePublishFrequencyDefaultValue;
59  AWS_LOGSTREAM_ERROR(__func__,
60  "Error " << ret << " retrieving publish frequency, setting to default value: "
62 
63  }
64 }
65 
73  const std::shared_ptr<Aws::Client::ParameterReaderInterface>& parameter_reader,
74  std::string & metric_namespace) {
75 
76  // Load the metric namespace
77  Aws::AwsError read_namespace_status =
78  parameter_reader->ReadParam(ParameterPath(kNodeParamMetricNamespaceKey), metric_namespace);
79  if (Aws::AWS_ERR_OK == read_namespace_status) {
80  AWS_LOGSTREAM_INFO(__func__, "Namespace: " << metric_namespace);
81  } else {
82  AWS_LOGSTREAM_INFO(
83  __func__,
84  "No namespace configuration found. Falling back to default namespace: " << kNodeDefaultMetricNamespace);
85  metric_namespace = kNodeDefaultMetricNamespace;
86  }
87 }
88 
97  const std::shared_ptr<Aws::Client::ParameterReaderInterface>& parameter_reader,
98  Aws::String & dimensions_param,
99  std::map<std::string, std::string> & metric_dims) {
100 
101  // Load the default dimensions
102  Aws::AwsError read_dimensions_status =
103  parameter_reader->ReadParam(ParameterPath(kNodeParamDefaultMetricDimensionsKey), dimensions_param);
104 
105  Aws::OStringStream logging_stream;
106  logging_stream << "Default Metric Dimensions: { ";
107  if (Aws::AWS_ERR_OK == read_dimensions_status) {
108  auto dims = Aws::Utils::StringUtils::Split(dimensions_param, ';');
109  for (auto & dim : dims) {
110  if (!dim.empty()) {
111  auto dim_vec = Aws::Utils::StringUtils::Split(dim, ':');
112  if (dim_vec.size() == 2) {
113  metric_dims.emplace(dim_vec[0].c_str(), dim_vec[1].c_str());
114  logging_stream << dim_vec[0] << ": " << dim_vec[1] << ", ";
115  } else {
116  AWS_LOGSTREAM_WARN(
117  __func__, "Could not parse dimension: "
118  << dim << ". Should be in the format <DimensionName>:<DimensionValue>");
119  }
120  }
121  }
122  }
123  logging_stream << " }";
124  AWS_LOGSTREAM_INFO(__func__, logging_stream.str());
125 }
126 
134  const std::shared_ptr<Aws::Client::ParameterReaderInterface>& parameter_reader,
135  int & storage_resolution) {
136 
137  // Load the storage resolution
138  storage_resolution = kNodeDefaultMetricDatumStorageResolution;
139 
140  Aws::AwsError read_storage_resolution_status =
141  parameter_reader->ReadParam(ParameterPath(kNodeParamMetricDatumStorageResolutionKey), storage_resolution);
142 
143  if (Aws::AWS_ERR_OK == read_storage_resolution_status) {
144  if (kNodeParamMetricDatumStorageResolutionValidValues.find(storage_resolution) ==
146  AWS_LOGSTREAM_WARN(__func__,
147  "Storage Resolution value of ["
148  << storage_resolution
149  << "] is not allowed. Falling back to default Storage Resolution: "
151  storage_resolution = kNodeDefaultMetricDatumStorageResolution;
152  } else {
153  AWS_LOGSTREAM_INFO(__func__, "Storage Resolution: " << storage_resolution);
154  }
155  } else {
156  AWS_LOGSTREAM_INFO(
157  __func__,
158  "No Storage Resolution configuration found. Falling back to default Storage Resolution: "
159  << storage_resolution);
160  }
161 }
162 
163 void ReadTopics(std::vector<std::string> & topics) {
164 
165  std::string param_key;
167  ros::param::get(param_key, topics);
168  }
169  if (topics.empty()) {
170  AWS_LOGSTREAM_INFO(
171  __func__, "Topic list not defined or empty. Listening on topic: " << kNodeDefaulMetricsTopic);
172  topics.push_back(kNodeDefaulMetricsTopic);
173  }
174 }
175 
177  const std::shared_ptr<Aws::Client::ParameterReaderInterface>& parameter_reader,
178  Aws::CloudWatchMetrics::CloudWatchOptions & cloudwatch_options) {
179 
180  Aws::DataFlow::UploaderOptions uploader_options{};
181  Aws::FileManagement::FileManagerStrategyOptions file_manager_strategy_options;
182 
183  ReadUploaderOptions(parameter_reader, uploader_options);
184  ReadFileManagerStrategyOptions(parameter_reader, file_manager_strategy_options);
185 
186  cloudwatch_options = {
187  uploader_options,
188  file_manager_strategy_options
189  };
190 }
191 
193  const std::shared_ptr<Aws::Client::ParameterReaderInterface>& parameter_reader,
194  Aws::DataFlow::UploaderOptions & uploader_options) {
195 
196  ReadOption(
197  parameter_reader,
199  Aws::DataFlow::kDefaultUploaderOptions.file_upload_batch_size,
200  uploader_options.file_upload_batch_size
201  );
202 
203  ReadOption(
204  parameter_reader,
206  Aws::DataFlow::kDefaultUploaderOptions.file_max_queue_size,
207  uploader_options.file_max_queue_size
208  );
209 
210  ReadOption(
211  parameter_reader,
213  Aws::DataFlow::kDefaultUploaderOptions.batch_max_queue_size,
214  uploader_options.batch_max_queue_size
215  );
216 
217  ReadOption(
218  parameter_reader,
220  Aws::DataFlow::kDefaultUploaderOptions.batch_trigger_publish_size,
221  uploader_options.batch_trigger_publish_size
222  );
223 
224  ReadOption(
225  parameter_reader,
227  Aws::DataFlow::kDefaultUploaderOptions.stream_max_queue_size,
228  uploader_options.stream_max_queue_size
229  );
230 }
231 
233  const std::shared_ptr<Aws::Client::ParameterReaderInterface>& parameter_reader,
234  Aws::FileManagement::FileManagerStrategyOptions & file_manager_strategy_options) {
235 
236  ReadOption(
237  parameter_reader,
240  file_manager_strategy_options.storage_directory);
241 
242  ReadOption(
243  parameter_reader,
246  file_manager_strategy_options.file_prefix);
247 
248  ReadOption(
249  parameter_reader,
252  file_manager_strategy_options.file_extension);
253 
254  ReadOption(
255  parameter_reader,
258  file_manager_strategy_options.maximum_file_size_in_kb);
259 
260  ReadOption(
261  parameter_reader,
264  file_manager_strategy_options.storage_limit_in_kb);
265 }
266 
268  const std::shared_ptr<Aws::Client::ParameterReaderInterface>& parameter_reader,
269  const std::string & option_key,
270  const std::string & default_value,
271  std::string & option_value) {
272  Aws::AwsError ret = parameter_reader->ReadParam(ParameterPath(option_key), option_value);
273  switch (ret) {
274  case Aws::AwsError::AWS_ERR_NOT_FOUND:
275  option_value = default_value;
276  AWS_LOGSTREAM_INFO(__func__,
277  option_key << " parameter not found, setting to default value: " << default_value);
278  break;
279  case Aws::AwsError::AWS_ERR_OK:
280  AWS_LOGSTREAM_INFO(__func__, option_key << " is set to: " << option_value);
281  break;
282  default:
283  option_value = default_value;
284  AWS_LOGSTREAM_ERROR(__func__,
285  "Error " << ret << " retrieving option " << option_key << ", setting to default value: " << default_value);
286  }
287 }
288 
290  const std::shared_ptr<Aws::Client::ParameterReaderInterface>& parameter_reader,
291  const std::string & option_key,
292  const size_t & default_value,
293  size_t & option_value) {
294 
295  int param_value = 0;
296  Aws::AwsError ret = parameter_reader->ReadParam(ParameterPath(option_key), param_value);
297  switch (ret) {
298  case Aws::AwsError::AWS_ERR_NOT_FOUND:
299  option_value = default_value;
300  AWS_LOGSTREAM_INFO(__func__,
301  option_key << " parameter not found, setting to default value: " << default_value);
302  break;
303  case Aws::AwsError::AWS_ERR_OK:
304  option_value = static_cast<size_t>(param_value);
305  AWS_LOGSTREAM_INFO(__func__, option_key << " is set to: " << option_value);
306  break;
307  default:
308  option_value = default_value;
309  AWS_LOGSTREAM_ERROR(__func__,
310  "Error " << ret << " retrieving option " << option_key << ", setting to default value: " << default_value);
311  }
312 }
313 
314 } // namespace Utils
315 } // namespace CloudWatchMetrics
316 } // namespace Aws
void ReadCloudWatchOptions(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, Aws::CloudWatchMetrics::CloudWatchOptions &cloudwatch_options)
void ReadUploaderOptions(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, Aws::DataFlow::UploaderOptions &uploader_options)
void ReadOption(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, const std::string &option_key, const std::string &default_value, std::string &option_value)
void ReadMetricNamespace(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, std::string &metric_namespace)
static constexpr UploaderOptions kDefaultUploaderOptions
void ReadTopics(std::vector< std::string > &topics)
const std::string kNodeParamMonitorTopicsListKey("aws_monitored_metric_topics")
ROSCPP_DECL bool get(const std::string &key, std::string &s)
void ReadPublishFrequency(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, double &publish_frequency)
void ReadFileManagerStrategyOptions(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, Aws::FileManagement::FileManagerStrategyOptions &file_manager_strategy_options)
const std::set< int > kNodeParamMetricDatumStorageResolutionValidValues
void ReadStorageResolution(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, int &storage_resolution)
ROSCPP_DECL bool search(const std::string &ns, const std::string &key, std::string &result)
AWS_ERR_OK
static const Aws::FileManagement::FileManagerStrategyOptions kDefaultMetricFileManagerStrategyOptions
void ReadMetricDimensions(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, Aws::String &dimensions_param, std::map< std::string, std::string > &metric_dims)


cloudwatch_metrics_collector
Author(s): AWS RoboMaker
autogenerated on Thu Apr 2 2020 03:39:59