AnyCollect  1.1.2
SnapInterface.h
Go to the documentation of this file.
1 //
2 // SnapInterface.h
3 //
4 // Created on October 25th 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 <array>
24 #include <chrono>
25 #include <string>
26 #include <string_view>
27 #include <vector>
28 
29 #include <AnyCollect/Controller.h>
30 #include <snap/plugin.h>
31 
32 using namespace std::literals;
33 
34 
35 namespace AnyCollect {
36  class SnapInterface final : public Plugin::StreamCollectorInterface, public AnyCollect::ControllerDelegate {
37  public:
38  static constexpr int appVersion = 1;
39  static constexpr std::string_view appName = "anycollect"sv;
40 
41  protected:
42  static constexpr std::array appPrefix = {"cfm"sv, "anycollect"sv};
43  static constexpr std::string_view configKeyConfigFile = "ConfigFile"sv;
44  static constexpr std::string_view configKeySamplingInterval = "SamplingInterval"sv;
45  static constexpr std::string_view configKeySendAllMetrics = "SendAllMetrics"sv;
46  static constexpr std::string_view configKeyMaxCollectDuration = "MaxCollectDuration"sv;
47  static constexpr std::string_view configKeyMaxMetricsBuffer = "MaxMetricsBuffer"sv;
48  static constexpr std::array configKeysInt = {configKeySamplingInterval, configKeyMaxCollectDuration, configKeyMaxMetricsBuffer};
49  static constexpr std::array configKeysBool = {configKeySendAllMetrics};
50  static constexpr bool defaultSendAllMetrics = false;
51  static constexpr std::chrono::seconds defaultMaxCollectDuration = 0s;
52  static constexpr size_t defaultMaxMetricsBuffer = 0;
53  static constexpr std::array<int, configKeysInt.size()> configValuesInt = {AnyCollect::Controller::defaultSamplingInterval.count(), SnapInterface::defaultMaxCollectDuration.count(), SnapInterface::defaultMaxMetricsBuffer};
54  static constexpr std::array<int, configKeysBool.size()> configValuesBool = {SnapInterface::defaultSendAllMetrics};
55  static std::hash<std::string> hasher;
56 
58  std::map<size_t, Plugin::Metric> metrics_;
59  std::set<size_t> requestedMetrics_;
60  std::set<size_t> unwantedMetrics_;
62  std::vector<Plugin::Metric*> metricsToSend_;
63 
64 
70  void insertAppPrefixToNamespace(std::vector<std::string>& ns);
71 
77  void setConfig(const Plugin::Config& cfg);
78 
84  void formatName(std::vector<std::string>& name);
85 
92  size_t computeNameKey(const Metric& m);
93 
100  size_t computeNameKey(const Plugin::Metric& m);
101 
108  Plugin::Metric convertToSnapMetric(const Metric& metric);
109 
110  public:
114  SnapInterface();
115 
116 
122  const Plugin::ConfigPolicy get_config_policy() override final;
123 
130  std::vector<Plugin::Metric> get_metric_types(Plugin::Config cfg) override final;
131 
137  void get_metrics_in(std::vector<Plugin::Metric> &metsIn) override final;
138 
142  void stream_metrics() override final;
143 
144 
145  void contollerCollectedMetrics(const Controller& controller, const std::vector<const Metric*>& metrics) override final;
146  bool contollerShouldStopCollectingMetrics(const Controller& controller) override final;
147 
148  };
149 }
150 
151 
155 int main(int argc, char* argv[]);
static constexpr std::chrono::seconds defaultSamplingInterval
Default parameter option.
Definition: Controller.h:48
STL namespace.
std::set< size_t > unwantedMetrics_
Array of keys of non requested metrics.
Definition: SnapInterface.h:60
std::map< size_t, Plugin::Metric > metrics_
Map associating keys to their metric.
Definition: SnapInterface.h:58
static std::hash< std::string > hasher
Object used to compute keys (string hashes)
Definition: SnapInterface.h:55
Struct used to represent the JSON configuration file.
Definition: Config.h:35
AnyCollect::Controller controller_
Controller used to collect statistics.
Definition: SnapInterface.h:57
std::vector< Plugin::Metric * > metricsToSend_
Array of pointers to Snap metrics to be sent.
Definition: SnapInterface.h:62
int main(int argc, char *argv[])
AnyCollect&#39;s main function.
Class used to represent a metric object (with a name, unit, tags, a value and a timestamp) ...
Definition: Metric.h:33
Abstract delegate of Controller.
Definition: Controller.h:142
std::set< size_t > requestedMetrics_
Array of keys of requested metrics.
Definition: SnapInterface.h:59
bool sendAllMetrics_
Whether to send all metrics, regardless of which are requested.
Definition: SnapInterface.h:61