MetroCollect  2.3.4
SnapStreamInterface.h
Go to the documentation of this file.
1 //
2 // SnapStreamInterface.h
3 //
4 // Created on July 27th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #pragma once
22 
23 #include <functional>
24 #include <array>
25 #include <chrono>
26 #include <map>
27 #include <string>
28 #include <string_view>
29 #include <vector>
30 
32 #include <snap/plugin.h>
33 
34 using namespace std::literals;
35 
36 
37 namespace SnapInterface {
41  class SnapStreamInterface final : public Plugin::StreamCollectorInterface, public MetroCollect::MetricsControllerDelegate {
42  public:
43  static constexpr int appVersion = 2;
44  static constexpr std::string_view appName = "metrocollect"sv;
45 
49  struct MetricsPackage {
53  struct TimedMetrics {
54  std::chrono::system_clock::time_point timestamp;
55  std::vector<MetroCollect::DiffValueType> values;
56  };
57 
58  std::vector<Plugin::Metric> computedMetrics;
59  std::vector<Plugin::Metric*> metricsToSend;
60  std::unique_ptr<TimedMetrics> currentMetrics;
61  std::unique_ptr<TimedMetrics> nextMetrics;
62  std::vector<size_t> metricsTimeout;
63  std::vector<size_t*> metricsTimeoutPointer;
64  std::vector<bool> secondaryMetrics;
65 
71  void clear(size_t metricsCount) {
72  this->computedMetrics.clear();
73  this->metricsToSend.clear();
74  this->currentMetrics = nullptr;
75  this->nextMetrics = nullptr;
76  this->metricsTimeout.clear();
77  this->metricsTimeout.resize(metricsCount, ((size_t)-1));
78  this->metricsTimeoutPointer.clear();
79  this->secondaryMetrics.clear();
80  }
81  };
82 
83  protected:
84  static constexpr std::array appPrefix = {"cfm"sv/*, "metrocollect"sv*/};
85  static constexpr std::string_view configKeySendValues = "SendValues"sv;
86  static constexpr std::string_view configKeySendStats = "SendStats"sv;
87  static constexpr std::string_view configKeySamplingInterval = "SamplingInterval"sv;
88  static constexpr std::string_view configKeyProcessingWindowLength = "ProcessingWindowLength"sv;
89  static constexpr std::string_view configKeyProcessingWindowOverlap = "ProcessingWindowOverlap"sv;
90  static constexpr std::string_view configKeyConvertToUnitsPerSecond = "ConvertToUnitsPerSecond"sv;
91  static constexpr std::string_view configKeyUnchangedMetricTimeout = "UnchangedMetricTimeout"sv;
92  static constexpr std::string_view configKeyMaxCollectDuration = "MaxCollectDuration"sv;
93  static constexpr std::string_view configKeyMaxMetricsBuffer = "MaxMetricsBuffer"sv;
94  static constexpr std::array configKeysInt = {configKeySamplingInterval, configKeyProcessingWindowLength, configKeyProcessingWindowOverlap, configKeyUnchangedMetricTimeout, configKeyMaxCollectDuration, configKeyMaxMetricsBuffer};
95  static constexpr std::array configKeysBool = {configKeySendValues, configKeySendStats, configKeyConvertToUnitsPerSecond};
96  static constexpr bool defaultSendValues = false;
97  static constexpr bool defaultSendStats = true;
98  static constexpr size_t defaultUnchangedMetricTimeout = 300;
99  static constexpr std::chrono::seconds defaultMaxCollectDuration = 0s;
100  static constexpr size_t defaultMaxMetricsBuffer = 0;
101  static constexpr std::array<int, configKeysInt.size()> configValuesInt = {MetroCollect::MetricsController::defaultSamplingInterval.count(), MetroCollect::MetricsController::defaultProcessingWindowLength, MetroCollect::MetricsController::defaultProcessingWindowOverlap, SnapStreamInterface::defaultUnchangedMetricTimeout, SnapStreamInterface::defaultMaxCollectDuration.count(), SnapStreamInterface::defaultMaxMetricsBuffer};
102  static constexpr std::array<bool, configKeysBool.size()> configValuesBool = {defaultSendValues, defaultSendStats, MetroCollect::MetricsController::defaultConvertToUnitsPerSecond};
103  static constexpr std::string_view statNamespaceLastComponent = "value"sv;
104  static constexpr std::string_view statNamespaceDescription = "statistic"sv;
105 
107 
110 
113 
114 
120  void setConfig(const Plugin::Config& cfg);
121 
122 
128  void parseSnapMetrics(const std::vector<Plugin::Metric>& metrics);
129 
130 
136  void insertAppPrefixToNamespace(std::vector<std::string>& ns);
137 
145  void createRequestedMetrics(MetricsPackage& package, std::string suffix, std::function<bool(MetroCollect::Statistics::Stats, bool*)> condition);
146 
152  void fillMetricsPackage(MetricsPackage& package);
153 
154 
161  void sendMetrics(MetricsPackage& package, std::function<void(MetricsPackage::TimedMetrics&)> copyMetrics);
162 
163  public:
168 
169 
175  const Plugin::ConfigPolicy get_config_policy() override final;
176 
183  std::vector<Plugin::Metric> get_metric_types(Plugin::Config cfg) override final;
184 
190  void get_metrics_in(std::vector<Plugin::Metric> &metsIn) override final;
191 
195  void stream_metrics() override final;
196 
197 
198  void metricsContollerCollectedMetricsValues(const MetroCollect::MetricsController& metricsController, const MetroCollect::MetricsDiffArray& metricsDiff, const MetroCollect::MetricsDataArray& previousMetrics, const MetroCollect::MetricsDataArray& currentMetrics) override final;
199  void metricsContollerCollectedMetricsStats(const MetroCollect::MetricsController& metricsController, const MetroCollect::MetricsController::MetricsStats& metricsStats) override final;
200  bool metricsContollerShouldStopCollectingMetrics(const MetroCollect::MetricsController& metricsController) override final;
201  };
202 }
203 
204 
208 int main(int argc, char* argv[]);
static constexpr std::chrono::milliseconds defaultSamplingInterval
Default parameter option.
static constexpr bool defaultConvertToUnitsPerSecond
Default parameter option.
Interface class between MetricsController and Snap&#39;s streaming-collector plugin class.
MetroCollect::MetricsArray< MetroCollect::Statistics::Stats > requestedMetrics_
Array of requested metrics.
MetroCollect::MetricsController metricsController_
MetricsController used to collect statistics.
static constexpr size_t defaultProcessingWindowLength
Default parameter option.
size_t unchangedMetricsTimeout_
Sub-sampling period for constant metrics.
STL namespace.
std::vector< bool > secondaryMetrics
Array to mark the most important metric stat (average)
Strcut to store Snap metric objects and metric data to make it faster to send them to Snap...
int main(int argc, char *argv[])
MetroCollect&#39;s main function.
Stats
Bitwise type te select statistics.
Definition: Statistics.h:64
std::vector< size_t > metricsTimeout
Sub-sampling counters for constant metrics.
std::vector< size_t * > metricsTimeoutPointer
Array to match metrics to their sub-sampling counter (one is shared for all statistics) ...
std::vector< Plugin::Metric * > metricsToSend
Array of pointers to Snap metrics to be sent.
std::vector< Plugin::Metric > computedMetrics
Cache for Snap metrics.
static constexpr size_t defaultProcessingWindowOverlap
Default parameter option.
std::unique_ptr< TimedMetrics > currentMetrics
Earlier values that may be sent.
void clear(size_t metricsCount)
Reinitializes the receiver and allocates space for metrics.
std::chrono::system_clock::time_point timestamp
Timestamp of the metrics.
MetricsPackage statsPackage_
MetricsPackage for metrics from computed statistics.
std::unique_ptr< TimedMetrics > nextMetrics
Latest values kept to be sent on the next iteration.
Abstract delegate of MetricsController.
std::vector< MetroCollect::DiffValueType > values
Array of metrics.
Class to control the collection of metrics.
MetricsPackage valuesPackage_
MetricsPackage for metrics from raw counters variations.