Fawkes API  Fawkes Development Version
metrics_thread.h
1 /***************************************************************************
2  * metrics_thread.h - Metrics exporter for Prometheus plugin
3  *
4  * Created: Sat May 06 19:43:10 2017 (German Open 2017)
5  * Copyright 2017 Tim Niemueller [www.niemueller.de]
6  ****************************************************************************/
7 
8 /* This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Library General Public License for more details.
17  *
18  * Read the full text in the LICENSE.GPL file in the doc directory.
19  */
20 
21 #ifndef _PLUGINS_METRICS_METRICS_THREAD_H_
22 #define _PLUGINS_METRICS_METRICS_THREAD_H_
23 
24 #include "aspect/metrics_inifin.h"
25 #include "aspect/metrics_supplier.h"
26 
27 #include <aspect/aspect_provider.h>
28 #include <aspect/blackboard.h>
29 #include <aspect/blocked_timing.h>
30 #include <aspect/configurable.h>
31 #include <aspect/logging.h>
32 #include <aspect/webview.h>
33 #include <blackboard/interface_listener.h>
34 #include <blackboard/interface_observer.h>
35 #include <core/threading/thread.h>
36 #include <core/utils/lock_map.h>
37 #include <interfaces/MetricFamilyInterface.h>
38 
39 #include <memory>
40 
42 
43 namespace fawkes {
44 class MetricCounterInterface;
45 class MetricGaugeInterface;
46 class MetricUntypedInterface;
47 class MetricHistogramInterface;
48 //MetricSummaryInterface;
49 } // namespace fawkes
50 
51 namespace io {
52 namespace prometheus {
53 namespace client {
54 class MetricFamily;
55 }
56 } // namespace prometheus
57 } // namespace io
58 
60  public fawkes::LoggingAspect,
63  public fawkes::WebviewAspect,
70 {
71 public:
72  MetricsThread();
73  virtual ~MetricsThread();
74 
75  virtual void init();
76  virtual void loop();
77  virtual void finalize();
78 
79  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
80 protected:
81  virtual void
82  run()
83  {
84  Thread::run();
85  }
86 
87 private:
88  typedef union {
89  fawkes::MetricCounterInterface * counter;
90  fawkes::MetricGaugeInterface * gauge;
91  fawkes::MetricUntypedInterface * untyped;
92  fawkes::MetricHistogramInterface *histogram;
93  //fawkes::MetricSummaryInterface * summary;
94  } MetricFamilyData;
95 
96  typedef struct
97  {
98  fawkes::MetricFamilyInterface * metric_family;
99  fawkes::MetricFamilyInterface::MetricType metric_type;
100  std::list<MetricFamilyData> data;
101  } MetricFamilyBB;
102 
103 private:
104  // for BlackBoardInterfaceObserver
105  virtual void bb_interface_created(const char *type, const char *id) throw();
106 
107  // for BlackBoardInterfaceListener
108  virtual void bb_interface_writer_removed(fawkes::Interface *interface,
109  unsigned int instance_serial) throw();
110  virtual void bb_interface_reader_removed(fawkes::Interface *interface,
111  unsigned int instance_serial) throw();
112  virtual void bb_interface_data_changed(fawkes::Interface *interface) throw();
113 
114  // for MetricsSupplier
115  virtual std::list<io::prometheus::client::MetricFamily> metrics();
116 
117  // for MetricsManager
118  virtual std::list<io::prometheus::client::MetricFamily> all_metrics();
119 
120  virtual void add_supplier(MetricsSupplier *supplier);
121  virtual void remove_supplier(MetricsSupplier *supplier);
122 
123  virtual const fawkes::LockList<MetricsSupplier *> &metrics_suppliers() const;
124 
125  bool conditional_open(const std::string &id, MetricFamilyBB &mfbb);
126  void conditional_close(fawkes::Interface *interface) throw();
127  void parse_labels(const std::string &labels, io::prometheus::client::Metric *m);
128 
129 private:
130  MetricsRequestProcessor * req_proc_;
132 
133  fawkes::MetricsAspectIniFin metrics_aspect_inifin_;
134 
135  // Internal metric families
136  std::shared_ptr<io::prometheus::client::MetricFamily> imf_loop_count_;
137  std::shared_ptr<io::prometheus::client::MetricFamily> imf_metrics_requests_;
138  std::shared_ptr<io::prometheus::client::MetricFamily> imf_metrics_proctime_;
139 
140  std::vector<std::shared_ptr<io::prometheus::client::MetricFamily>> internal_metrics_;
141 
142  fawkes::LockList<MetricsSupplier *> metrics_suppliers_;
143 };
144 
145 #endif
fawkes::LockMap< std::string, MetricFamilyBB >
fawkes::MetricsSupplier
Metrics supplier class.
Definition: metrics_supplier.h:32
fawkes::LockList< MetricsSupplier * >
fawkes::BlackBoardInterfaceListener
BlackBoard interface listener.
Definition: interface_listener.h:42
fawkes::AspectProviderAspect
Thread aspect provide a new aspect.
Definition: aspect_provider.h:36
fawkes::BlockedTimingAspect
Thread aspect to use blocked timing.
Definition: blocked_timing.h:51
MetricsThread::MetricsThread
MetricsThread()
Constructor.
Definition: metrics_thread.cpp:48
fawkes::BlackBoardAspect
Thread aspect to access to BlackBoard.
Definition: blackboard.h:34
fawkes
Fawkes library namespace.
fawkes::LoggingAspect
Thread aspect to log output.
Definition: logging.h:33
MetricsThread::run
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: metrics_thread.h:82
fawkes::BlackBoardInterfaceObserver
BlackBoard interface observer.
Definition: interface_observer.h:37
fawkes::Interface
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
MetricsThread
Thread to export metrics for Prometheus.
Definition: metrics_thread.h:70
fawkes::MetricsAspectIniFin
MetricsAspect initializer/finalizer.
Definition: metrics_inifin.h:38
fawkes::WebviewAspect
Thread aspect to provide web pages via Webview.
Definition: webview.h:37
MetricsRequestProcessor
Metrics web request processor.
Definition: metrics_processor.h:35
fawkes::MetricsManager
Base class for metrics managers.
Definition: metrics_manager.h:32
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
MetricsThread::loop
virtual void loop()
Code to execute in the thread.
Definition: metrics_thread.cpp:143
MetricsThread::finalize
virtual void finalize()
Finalize the thread.
Definition: metrics_thread.cpp:136
fawkes::ConfigurableAspect
Thread aspect to access configuration data.
Definition: configurable.h:33
MetricsThread::init
virtual void init()
Initialize the thread.
Definition: metrics_thread.cpp:62
MetricsThread::~MetricsThread
virtual ~MetricsThread()
Destructor.
Definition: metrics_thread.cpp:57