AnyCollect  1.1.2
AnyCollectValues.cc
Go to the documentation of this file.
1 //
2 // AnyCollectValues.cc
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 #include <iostream>
22 #include <iomanip>
23 #include <sstream>
24 
25 #include <AnyCollect/Controller.h>
26 
27 
28 template<typename Clock, typename Duration>
29 std::ostream &operator<<(std::ostream &stream,
30  const std::chrono::time_point<Clock, Duration> &time_point) {
31  const time_t time = Clock::to_time_t(time_point);
32  struct tm tm;
33  localtime_r(&time, &tm);
34  return stream << std::put_time(&tm, "%c");
35 }
36 
37 
39  std::string name = m.name()[0];
40  for (size_t i = 1; i < m.name().size(); i++)
41  name += "/" + m.name()[i];
42  std::string tags;
43  for (const auto& [k, v] : m.tags())
44  tags += "'" + k + "'='" + v + "', ";
45  std::cout << std::setw(54) << std::left << name;
46  std::cout << std::setw(20) << std::right << std::setprecision(18) << m.value() << " ";
47  std::cout << std::setw(16) << std::left << m.unit();
48  std::cout << std::setw(54) << std::left << tags;
49  std::cout << std::endl;
50 }
51 
52 
55 
56 #if PRINT_METRICS
57  void contollerCollectedMetrics(const AnyCollect::Controller& , const std::vector<const AnyCollect::Metric*>& metrics) override {
58  for (const auto& m : metrics)
59  printMetric(*m);
60  std::cout << std::endl << "----------------" << std::endl << std::endl;
61 #else
62  void contollerCollectedMetrics(const AnyCollect::Controller& , const std::vector<const AnyCollect::Metric*>& ) override {
63 #endif
64  }
65 
68  return iterationCount == 0;
69  }
70 };
71 
72 
73 int main(int argc, char* argv[]) {
75  std::string configPath = "";
77 
78  if (argc > 1)
79  samplingInterval = std::chrono::seconds(std::atol(argv[1]));
80  if (argc > 2)
81  configPath = argv[2];
82  if (argc > 3)
83  d.iterationCount = std::atol(argv[3]);
84  else
85  d.iterationCount = 0;
86 
87  AnyCollect::Controller controller(d);
88  controller.setSamplingInterval(samplingInterval);
89  controller.loadConfigFromFile(configPath);
90 
91  controller.collectMetrics();
92 
93  return 0;
94 }
void loadConfigFromFile(const std::string &configPath)
Configures sources, expressions and matchers according to config file.
Definition: Controller.cc:54
int main(int argc, char *argv[])
AnyCollect&#39;s main function.
void setSamplingInterval(std::chrono::seconds interval) noexcept
Sets the metrics sampling interval.
Definition: Controller.cc:96
void contollerCollectedMetrics(const AnyCollect::Controller &, const std::vector< const AnyCollect::Metric *> &) override
Function called every time the Controller has collected metrics.
static constexpr std::chrono::seconds defaultSamplingInterval
Default parameter option.
Definition: Controller.h:48
bool contollerShouldStopCollectingMetrics(const AnyCollect::Controller &) override
Function called after each iteration to ask the delegate whether the controller should stop collectin...
const std::string & unit() const noexcept
Returns the unit of the metric.
Definition: Metric.cc:115
const std::vector< std::string > & name() const noexcept
Returns the name of the metric.
Definition: Metric.cc:95
void printMetric(const AnyCollect::Metric &m)
std::ostream & operator<<(std::ostream &stream, const std::chrono::time_point< Clock, Duration > &time_point)
void collectMetrics() noexcept
Launch metric collection loop.
Definition: Controller.cc:128
const std::map< std::string, std::string > & tags() const noexcept
Returns the tags of the metric.
Definition: Metric.cc:119
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
double value() const noexcept
Returns the value of the metric.
Definition: Metric.cc:107