ESPHome  2024.4.0
prometheus_handler.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <utility>
5 
10 
11 namespace esphome {
12 namespace prometheus {
13 
14 class PrometheusHandler : public AsyncWebHandler, public Component {
15  public:
17 
23  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
24 
30  void add_label_id(EntityBase *obj, const std::string &value) { relabel_map_id_.insert({obj, value}); }
31 
37  void add_label_name(EntityBase *obj, const std::string &value) { relabel_map_name_.insert({obj, value}); }
38 
39  bool canHandle(AsyncWebServerRequest *request) override {
40  if (request->method() == HTTP_GET) {
41  if (request->url() == "/metrics")
42  return true;
43  }
44 
45  return false;
46  }
47 
48  void handleRequest(AsyncWebServerRequest *req) override;
49 
50  void setup() override {
51  this->base_->init();
52  this->base_->add_handler(this);
53  }
54  float get_setup_priority() const override {
55  // After WiFi
56  return setup_priority::WIFI - 1.0f;
57  }
58 
59  protected:
60  std::string relabel_id_(EntityBase *obj);
61  std::string relabel_name_(EntityBase *obj);
62 
63 #ifdef USE_SENSOR
64  void sensor_type_(AsyncResponseStream *stream);
67  void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj);
68 #endif
69 
70 #ifdef USE_BINARY_SENSOR
71  void binary_sensor_type_(AsyncResponseStream *stream);
74  void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj);
75 #endif
76 
77 #ifdef USE_FAN
78  void fan_type_(AsyncResponseStream *stream);
81  void fan_row_(AsyncResponseStream *stream, fan::Fan *obj);
82 #endif
83 
84 #ifdef USE_LIGHT
85  void light_type_(AsyncResponseStream *stream);
88  void light_row_(AsyncResponseStream *stream, light::LightState *obj);
89 #endif
90 
91 #ifdef USE_COVER
92  void cover_type_(AsyncResponseStream *stream);
95  void cover_row_(AsyncResponseStream *stream, cover::Cover *obj);
96 #endif
97 
98 #ifdef USE_SWITCH
99  void switch_type_(AsyncResponseStream *stream);
102  void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj);
103 #endif
104 
105 #ifdef USE_LOCK
106  void lock_type_(AsyncResponseStream *stream);
109  void lock_row_(AsyncResponseStream *stream, lock::Lock *obj);
110 #endif
111 
113  bool include_internal_{false};
114  std::map<EntityBase *, std::string> relabel_map_id_;
115  std::map<EntityBase *, std::string> relabel_map_name_;
116 };
117 
118 } // namespace prometheus
119 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
void handleRequest(AsyncWebServerRequest *req) override
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj)
Return the switch Values state as prometheus data point.
Base class for all cover devices.
Definition: cover.h:111
std::string relabel_id_(EntityBase *obj)
void switch_type_(AsyncResponseStream *stream)
Return the type for prometheus.
bool canHandle(AsyncWebServerRequest *request) override
void add_handler(AsyncWebHandler *handler)
void light_row_(AsyncResponseStream *stream, light::LightState *obj)
Return the Light Values state as prometheus data point.
void binary_sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj)
Return the sensor state as prometheus data point.
std::map< EntityBase *, std::string > relabel_map_name_
void set_include_internal(bool include_internal)
Determine whether internal components should be exported as metrics.
void light_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void add_label_id(EntityBase *obj, const std::string &value)
Add the value for an entity&#39;s "id" label.
web_server_base::WebServerBase * base_
void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj)
Return the sensor state as prometheus data point.
PrometheusHandler(web_server_base::WebServerBase *base)
void lock_row_(AsyncResponseStream *stream, lock::Lock *obj)
Return the lock Values state as prometheus data point.
void lock_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void add_label_name(EntityBase *obj, const std::string &value)
Add the value for an entity&#39;s "name" label.
void fan_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void cover_type_(AsyncResponseStream *stream)
Return the type for prometheus.
std::string relabel_name_(EntityBase *obj)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
Base-class for all sensors.
Definition: sensor.h:57
std::map< EntityBase *, std::string > relabel_map_id_
void cover_row_(AsyncResponseStream *stream, cover::Cover *obj)
Return the switch Values state as prometheus data point.
Base class for all locks.
Definition: lock.h:103
void fan_row_(AsyncResponseStream *stream, fan::Fan *obj)
Return the sensor state as prometheus data point.