libstatistics_collector  master
Lightweight aggregation utilities to collect statistics and measure message metrics.
received_message_age.hpp
Go to the documentation of this file.
1 // Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef LIBSTATISTICS_COLLECTOR__TOPIC_STATISTICS_COLLECTOR__RECEIVED_MESSAGE_AGE_HPP_
16 #define LIBSTATISTICS_COLLECTOR__TOPIC_STATISTICS_COLLECTOR__RECEIVED_MESSAGE_AGE_HPP_
17 
18 #include <chrono>
19 #include <string>
20 #include <sstream>
21 #include <type_traits>
22 #include <utility>
23 
24 #include "constants.hpp"
25 
27 
28 #include "rcl/time.h"
29 #include "rcutils/logging_macros.h"
30 
32 {
33 namespace topic_statistics_collector
34 {
35 
40 template<typename M, typename = void>
41 struct HasHeader : public std::false_type {};
42 
47 template<typename M>
48 struct HasHeader<M, decltype((void) M::header)>: std::true_type {};
49 
56 template<typename M, typename Enable = void>
57 struct TimeStamp
58 {
59  static std::pair<bool, int64_t> value(const M &)
60  {
61  return std::make_pair(false, 0);
62  }
63 };
64 
70 template<typename M>
71 struct TimeStamp<M, typename std::enable_if<HasHeader<M>::value>::type>
72 {
73  static std::pair<bool, int64_t> value(const M & m)
74  {
75  const auto stamp = m.header.stamp;
76  const auto nanos = RCL_S_TO_NS(static_cast<int64_t>(stamp.sec)) + stamp.nanosec;
77  return std::make_pair(true, nanos);
78  }
79 };
80 
86 template<typename T>
88 {
89 public:
90  ReceivedMessageAgeCollector() = default;
91 
92  virtual ~ReceivedMessageAgeCollector() = default;
93 
101  const T & received_message,
102  const rcl_time_point_value_t now_nanoseconds) override
103  {
104  const auto timestamp_from_header = TimeStamp<T>::value(received_message);
105 
106  if (timestamp_from_header.first) {
107  // only compare if non-zero
108  if (timestamp_from_header.second && now_nanoseconds) {
109  const std::chrono::nanoseconds age_nanos{now_nanoseconds - timestamp_from_header.second};
110  const auto age_millis = std::chrono::duration_cast<std::chrono::milliseconds>(age_nanos);
111 
112  collector::Collector::AcceptData(static_cast<double>(age_millis.count()));
113  } // else no valid time to compute age
114  }
115  }
116 
122  std::string GetMetricName() const override
123  {
125  }
126 
132  std::string GetMetricUnit() const override
133  {
135  }
136 
137 protected:
138  bool SetupStart() override
139  {
140  return true;
141  }
142 
143  bool SetupStop() override
144  {
145  return true;
146  }
147 };
148 
149 } // namespace topic_statistics_collector
150 } // namespace libstatistics_collector
151 
152 #endif // LIBSTATISTICS_COLLECTOR__TOPIC_STATISTICS_COLLECTOR__RECEIVED_MESSAGE_AGE_HPP_
libstatistics_collector::topic_statistics_collector::ReceivedMessageAgeCollector::GetMetricUnit
std::string GetMetricUnit() const override
Definition: received_message_age.hpp:132
libstatistics_collector::topic_statistics_collector::ReceivedMessageAgeCollector::OnMessageReceived
void OnMessageReceived(const T &received_message, const rcl_time_point_value_t now_nanoseconds) override
Definition: received_message_age.hpp:100
libstatistics_collector::topic_statistics_collector::TopicStatisticsCollector
Definition: topic_statistics_collector.hpp:37
libstatistics_collector::topic_statistics_collector::ReceivedMessageAgeCollector::ReceivedMessageAgeCollector
ReceivedMessageAgeCollector()=default
libstatistics_collector::topic_statistics_collector::ReceivedMessageAgeCollector::SetupStart
bool SetupStart() override
Definition: received_message_age.hpp:138
libstatistics_collector
Definition: collector.hpp:29
topic_statistics_collector.hpp
libstatistics_collector::topic_statistics_collector::topic_statistics_constants::kMillisecondUnitName
constexpr const char kMillisecondUnitName[]
Definition: constants.hpp:29
libstatistics_collector::topic_statistics_collector::ReceivedMessageAgeCollector::GetMetricName
std::string GetMetricName() const override
Definition: received_message_age.hpp:122
constants.hpp
libstatistics_collector::topic_statistics_collector::ReceivedMessageAgeCollector
Definition: received_message_age.hpp:87
libstatistics_collector::topic_statistics_collector::ReceivedMessageAgeCollector::~ReceivedMessageAgeCollector
virtual ~ReceivedMessageAgeCollector()=default
libstatistics_collector::topic_statistics_collector::HasHeader
Definition: received_message_age.hpp:41
libstatistics_collector::topic_statistics_collector::ReceivedMessageAgeCollector::SetupStop
bool SetupStop() override
Definition: received_message_age.hpp:143
libstatistics_collector::topic_statistics_collector::topic_statistics_constants::kMsgAgeStatName
constexpr const char kMsgAgeStatName[]
Definition: constants.hpp:27
libstatistics_collector::topic_statistics_collector::TimeStamp< M, typename std::enable_if< HasHeader< M >::value >::type >::value
static std::pair< bool, int64_t > value(const M &m)
Definition: received_message_age.hpp:73
libstatistics_collector::collector::Collector::AcceptData
virtual void AcceptData(const double measurement)
libstatistics_collector::topic_statistics_collector::TimeStamp::value
static std::pair< bool, int64_t > value(const M &)
Definition: received_message_age.hpp:59
libstatistics_collector::topic_statistics_collector::TimeStamp
Definition: received_message_age.hpp:57