AnyCollect  1.1.2
Controller.h
Go to the documentation of this file.
1 //
2 // Controller.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 <chrono>
24 #include <memory>
25 #include <regex>
26 #include <vector>
27 
28 #include "Source.h"
29 #include "Expression.h"
30 #include "Matcher.h"
31 #include "Metric.h"
32 
33 using namespace std::literals;
34 
35 
36 namespace AnyCollect {
37  class ControllerDelegate;
38 
43  class Controller {
44  public:
45 #if PROFILING
46  static constexpr std::chrono::seconds defaultSamplingInterval = 0s;
47 #else
48  static constexpr std::chrono::seconds defaultSamplingInterval = 10s;
49 #endif
50 
51  protected:
53 
55  std::chrono::seconds samplingInterval_;
57  size_t roundKey_;
58 
59  std::vector<std::shared_ptr<Source>> sources_;
60  std::vector<std::shared_ptr<Expression>> expressions_;
61  std::vector<std::shared_ptr<Matcher>> matchers_;
62  std::map<size_t, Metric> metrics_;
63  std::vector<const Metric*> updatedMetrics_;
64 
68  void updateSources() noexcept;
69 
73  void computeMatches() noexcept;
74 
82  void parseData(const Source& source, const std::cmatch& match, const Matcher& matcher) noexcept;
83 
84  public:
90  Controller(ControllerDelegate& delegate) noexcept;
91 
92 
96  ControllerDelegate& delegate() const noexcept;
97 
98 
102  bool isCollecting() const noexcept;
103 
107  std::chrono::seconds samplingInterval() const noexcept;
108 
109 
115  void loadConfigFromFile(const std::string& configPath);
116 
120  void setSamplingInterval(std::chrono::seconds interval) noexcept;
121 
122 
128  std::vector<const Metric*> availableMetrics() noexcept;
129 
133  void collectMetrics() noexcept;
134  };
135 
136 
143  public:
150  virtual void contollerCollectedMetrics(const Controller& controller, const std::vector<const Metric*>& metrics) = 0;
151 
159  virtual bool contollerShouldStopCollectingMetrics(const Controller& controller) = 0;
160  };
161 }
std::map< size_t, Metric > metrics_
Map associating keys to their metric.
Definition: Controller.h:62
std::vector< std::shared_ptr< Matcher > > matchers_
Array of matchers.
Definition: Controller.h:61
ControllerDelegate & delegate_
Delegate to alert when something happens.
Definition: Controller.h:52
STL namespace.
Class used to represent a source (file or command output) from which metrics can be matched...
Definition: Source.h:39
Class used to represent a matcher, that is a way to convert expressions matches into a metric...
Definition: Matcher.h:40
bool isCollecting_
Whether the receiver is collecting metrics.
Definition: Controller.h:54
size_t roundKey_
Metric collection iteration unique identifier.
Definition: Controller.h:57
std::vector< std::shared_ptr< Source > > sources_
Array of sources.
Definition: Controller.h:59
std::vector< const Metric * > updatedMetrics_
Array of pointers to the iteration&#39;s metrics.
Definition: Controller.h:63
std::vector< std::shared_ptr< Expression > > expressions_
Array of expressions.
Definition: Controller.h:60
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::chrono::seconds samplingInterval_
Metrics sampling interval.
Definition: Controller.h:55
double unitsPerSecondFactor_
Factor to convert metric differences to units per second.
Definition: Controller.h:56