main.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 
16 
17 #include <aws/core/Aws.h>
18 #include <aws/core/client/ClientConfiguration.h>
19 #include <aws/core/utils/StringUtils.h>
20 #include <aws/core/utils/logging/AWSLogging.h>
21 #include <aws/core/utils/logging/LogMacros.h>
22 #include <aws/monitoring/CloudWatchClient.h>
23 #include <aws/monitoring/model/PutMetricDataRequest.h>
27 #include <ros/ros.h>
28 #include <ros_monitoring_msgs/MetricData.h>
29 #include <ros_monitoring_msgs/MetricDimension.h>
30 #include <ros_monitoring_msgs/MetricList.h>
31 #include <std_msgs/String.h>
32 
36 #include <string>
37 #include <vector>
38 #include <map>
39 
41 
44 
48 
49 int main(int argc, char * argv[])
50 {
51  int status = 0;
52  ros::init(argc, argv, kNodeName);
53  ros::NodeHandle node_handle;
54 
55  std::vector<ros::Subscriber> subscriptions;
56 
57  // initialize SDK logging
58  Aws::Utils::Logging::InitializeAWSLogging(Aws::MakeShared<Aws::Utils::Logging::AWSROSLogger>(kNodeName.c_str()));
59 
60  //-----------Start Read Configuration Parameters---------------------
61 
62  std::shared_ptr<Aws::Client::ParameterReaderInterface> parameter_reader =
63  std::make_shared<Aws::Client::Ros1NodeParameterReader>();
64 
65  // SDK client config
66  Aws::Client::ClientConfigurationProvider client_config_provider(parameter_reader);
67  Aws::Client::ClientConfiguration client_config = client_config_provider.GetClientConfiguration();
68 
69  Aws::SDKOptions sdk_options;
70 
71  double publish_frequency;
72 
73  std::string metric_namespace;
74  Aws::String dimensions_param;
75  std::map<std::string, std::string> default_metric_dims;
76 
77  // Load the storage resolution
78  int storage_resolution = kNodeDefaultMetricDatumStorageResolution;
80 
81  Aws::CloudWatchMetrics::Utils::ReadPublishFrequency(parameter_reader, publish_frequency);
82  Aws::CloudWatchMetrics::Utils::ReadMetricNamespace(parameter_reader, metric_namespace);
83  Aws::CloudWatchMetrics::Utils::ReadMetricDimensions(parameter_reader, dimensions_param, default_metric_dims);
84  Aws::CloudWatchMetrics::Utils::ReadStorageResolution(parameter_reader, storage_resolution);
85 
86  Aws::CloudWatchMetrics::Utils::ReadCloudWatchOptions(parameter_reader, cloudwatch_options);
87  //-----------------End read configuration parameters-----------------------
88 
89  // create the metric collector
91 
92  // initialize with options read from the config file
93  metrics_collector.Initialize(
94  metric_namespace,
95  default_metric_dims,
96  storage_resolution,
97  node_handle,
98  client_config,
99  sdk_options,
100  cloudwatch_options);
101 
102  ros::ServiceServer service = node_handle.advertiseService(kNodeName,
104  &metrics_collector);
105 
106  // start the collection process
107  metrics_collector.start();
108 
109  bool publish_when_size_reached = cloudwatch_options.uploader_options.batch_trigger_publish_size
111 
112  ros::Timer timer;
113  // Publish on a timer if we are not publishing on a size limit.
114  if (!publish_when_size_reached) {
115  timer =
116  node_handle.createTimer(ros::Duration(publish_frequency),
118  &metrics_collector);
119  }
120 
121  ros::spin();
122 
123  AWS_LOG_INFO(__func__, "Shutting down Metrics Collector ...");
124  metrics_collector.shutdown();
125  Aws::Utils::Logging::ShutdownAWSLogging();
126  ros::shutdown();
127  return status;
128 }
void ReadCloudWatchOptions(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, Aws::CloudWatchMetrics::CloudWatchOptions &cloudwatch_options)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void Initialize(std::string metric_namespace, std::map< std::string, std::string > &default_dimensions, int storage_resolution, const ros::NodeHandle &node_handle, const Aws::Client::ClientConfiguration &config, const Aws::SDKOptions &sdk_options, const Aws::CloudWatchMetrics::CloudWatchOptions &cloudwatch_options, const std::shared_ptr< MetricServiceFactory > &metric_service_factory=std::make_shared< MetricServiceFactory >())
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
void ReadMetricNamespace(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, std::string &metric_namespace)
static constexpr UploaderOptions kDefaultUploaderOptions
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
ROSCPP_DECL void spin()
void ReadPublishFrequency(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, double &publish_frequency)
Aws::DataFlow::UploaderOptions uploader_options
ROSCPP_DECL void shutdown()
bool checkIfOnline(std_srvs::Trigger::Request &request, std_srvs::Trigger::Response &response)
void ReadStorageResolution(const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameter_reader, int &storage_resolution)
int main(int argc, char *argv[])
Definition: main.cpp:49
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 Fri Mar 5 2021 03:38:40