23 #if GPERFTOOLS_CPU_PROFILE 24 #include <gperftools/profiler.h> 64 for (
const auto& file : config.
files) {
65 for (
const auto& path : file.paths) {
67 for (
const auto& p : paths)
68 this->
sources_.push_back(std::make_shared<Source>(p));
70 for (
const auto& expression : file.expressions) {
71 this->
expressions_.push_back(std::make_shared<Expression>(expression.regex));
72 for (
const auto& metric : expression.metrics) {
73 this->
matchers_.push_back(std::make_shared<Matcher>(metric));
76 for (
auto itr = this->
sources_.begin() + fileIndex; itr != this->
sources_.end(); itr++)
77 (*itr)->expressions().push_back(this->
expressions_.back());
82 for (
const auto& command : config.
commands) {
83 this->
sources_.push_back(std::make_shared<Source>(command.program, command.arguments));
84 for (
const auto& expression : command.expressions) {
85 this->
expressions_.push_back(std::make_shared<Expression>(expression.regex));
86 for (
const auto& metric : expression.metrics) {
87 this->
matchers_.push_back(std::make_shared<Matcher>(metric));
97 if (this->isCollecting_)
100 this->samplingInterval_ = interval;
102 this->unitsPerSecondFactor_ = 1.0 / interval.count();
104 this->unitsPerSecondFactor_ = 1.0;
119 for (
const auto& itr : this->
metrics_)
131 #if GPERFTOOLS_CPU_PROFILE 132 ProfilerStart(
"/tmp/aa.prof");
136 auto startTime = std::chrono::steady_clock::now();
143 std::this_thread::sleep_for(this->
samplingInterval_ - (std::chrono::steady_clock::now() - startTime));
144 startTime = std::chrono::steady_clock::now();
153 #if GPERFTOOLS_CPU_PROFILE 158 #if GPERFTOOLS_CPU_PROFILE 171 for (
const auto& source : this->
sources_) {
172 auto begin = source->begin();
173 while(begin != source->end()) {
174 auto end = source->getLine(begin);
176 for (
const auto& expression : source->expressions()) {
177 auto& match = expression->apply(begin, end);
178 if (!match.empty()) {
179 for (
const auto& matcher : expression->matchers())
180 this->
parseData(*source, match, *matcher);
184 if (end != source->end())
194 auto value = matcher.getValue(match, source.pathParts());
195 if (!value.has_value())
197 auto newMetric = matcher.getMetric(match, source.pathParts());
198 if (!newMetric.has_value())
201 auto itr = this->metrics_.find(newMetric.value().key());
202 bool isNew = (itr == this->metrics_.end());
204 itr = this->metrics_.insert_or_assign(this->metrics_.begin(), newMetric.value().key(), std::move(newMetric.value()));
206 Metric& metric = itr->second;
207 isNew = (isNew || (metric.
roundKey() != this->roundKey_ - 1));
208 if (metric.
roundKey() != this->roundKey_) {
209 metric.
setNewValue(value.value(), matcher.computeRate(), matcher.convertToUnitsPerSecond() ? this->unitsPerSecondFactor_ : 1.0);
210 if ((!isNew || !matcher.computeRate()))
211 this->updatedMetrics_.push_back(&metric);
213 metric.
updateValue(value.value(), matcher.convertToUnitsPerSecond() ? this->unitsPerSecondFactor_ : 1.0);
void loadConfigFromFile(const std::string &configPath)
Configures sources, expressions and matchers according to config file.
void setTimestamp(std::chrono::system_clock::time_point timestamp) noexcept
Sets the timestamp of the metric.
bool isCollecting() const noexcept
Returns whether the controller is collecting metrics.
void setNewValue(double value, bool computeRate, double unitsPerSecondFactor=1.0) noexcept
Sets a new value to the metric.
void setSamplingInterval(std::chrono::seconds interval) noexcept
Sets the metrics sampling interval.
virtual void contollerCollectedMetrics(const Controller &controller, const std::vector< const Metric *> &metrics)=0
Function called every time the Controller has collected metrics.
std::map< size_t, Metric > metrics_
Map associating keys to their metric.
std::vector< std::shared_ptr< Matcher > > matchers_
Array of matchers.
void parseData(const Source &source, const std::cmatch &match, const Matcher &matcher) noexcept
Updates metrics from a match.
static constexpr std::chrono::seconds defaultSamplingInterval
Default parameter option.
ControllerDelegate & delegate_
Delegate to alert when something happens.
std::vector< Config::command > commands
Controller(ControllerDelegate &delegate) noexcept
Construct a new Controller object.
Class used to represent a source (file or command output) from which metrics can be matched...
Struct used to represent the JSON configuration file.
std::chrono::seconds samplingInterval() const noexcept
Returns the metrics sampling interval.
static std::vector< std::string > filePathsMatchingGlobbingPattern(const std::string &pattern) noexcept
Returns all file paths that match a pattern as defined by POSIX.
Class used to represent a matcher, that is a way to convert expressions matches into a metric...
void updateSources() noexcept
Updates sources (fetches file contents and executes commands)
bool isCollecting_
Whether the receiver is collecting metrics.
size_t roundKey() const noexcept
Returns the key of the collection iteration which created the metric.
size_t roundKey_
Metric collection iteration unique identifier.
std::vector< std::shared_ptr< Source > > sources_
Array of sources.
std::vector< const Metric * > updatedMetrics_
Array of pointers to the iteration's metrics.
void collectMetrics() noexcept
Launch metric collection loop.
ControllerDelegate & delegate() const noexcept
Returns the controller's delegate.
std::vector< Config::file > files
std::vector< std::shared_ptr< Expression > > expressions_
Array of expressions.
Class used to represent a metric object (with a name, unit, tags, a value and a timestamp) ...
Abstract delegate of Controller.
void setRoundKey(size_t roundKey) noexcept
Sets the round key of the metric.
std::vector< const Metric * > availableMetrics() noexcept
Returns the array of all currently matching metrics on the system, without their values.
void computeMatches() noexcept
For each line of each source, executes the source's expressions to find matches.
virtual bool contollerShouldStopCollectingMetrics(const Controller &controller)=0
Function called after each iteration to ask the delegate whether the controller should stop collectin...
std::chrono::seconds samplingInterval_
Metrics sampling interval.
void updateValue(double value, double unitsPerSecondFactor=1.0) noexcept
Adds a new value to the current value.