ESPHome  2024.11.2
prometheus_handler.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
3 #ifdef USE_NETWORK
4 #include <map>
5 #include <utility>
6 
11 
12 namespace esphome {
13 namespace prometheus {
14 
15 class PrometheusHandler : public AsyncWebHandler, public Component {
16  public:
18 
24  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
25 
31  void add_label_id(EntityBase *obj, const std::string &value) { relabel_map_id_.insert({obj, value}); }
32 
38  void add_label_name(EntityBase *obj, const std::string &value) { relabel_map_name_.insert({obj, value}); }
39 
40  bool canHandle(AsyncWebServerRequest *request) override {
41  if (request->method() == HTTP_GET) {
42  if (request->url() == "/metrics")
43  return true;
44  }
45 
46  return false;
47  }
48 
49  void handleRequest(AsyncWebServerRequest *req) override;
50 
51  void setup() override {
52  this->base_->init();
53  this->base_->add_handler(this);
54  }
55  float get_setup_priority() const override {
56  // After WiFi
57  return setup_priority::WIFI - 1.0f;
58  }
59 
60  protected:
61  std::string relabel_id_(EntityBase *obj);
62  std::string relabel_name_(EntityBase *obj);
63  void add_area_label_(AsyncResponseStream *stream, std::string &area);
64  void add_node_label_(AsyncResponseStream *stream, std::string &node);
65  void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name);
66 
67 #ifdef USE_SENSOR
68  void sensor_type_(AsyncResponseStream *stream);
71  void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area, std::string &node,
72  std::string &friendly_name);
73 #endif
74 
75 #ifdef USE_BINARY_SENSOR
76  void binary_sensor_type_(AsyncResponseStream *stream);
79  void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area,
80  std::string &node, std::string &friendly_name);
81 #endif
82 
83 #ifdef USE_FAN
84  void fan_type_(AsyncResponseStream *stream);
87  void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node,
88  std::string &friendly_name);
89 #endif
90 
91 #ifdef USE_LIGHT
92  void light_type_(AsyncResponseStream *stream);
95  void light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area, std::string &node,
96  std::string &friendly_name);
97 #endif
98 
99 #ifdef USE_COVER
100  void cover_type_(AsyncResponseStream *stream);
103  void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node,
104  std::string &friendly_name);
105 #endif
106 
107 #ifdef USE_SWITCH
108  void switch_type_(AsyncResponseStream *stream);
111  void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area, std::string &node,
112  std::string &friendly_name);
113 #endif
114 
115 #ifdef USE_LOCK
116  void lock_type_(AsyncResponseStream *stream);
119  void lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node,
120  std::string &friendly_name);
121 #endif
122 
123 #ifdef USE_TEXT_SENSOR
124  void text_sensor_type_(AsyncResponseStream *stream);
127  void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node,
128  std::string &friendly_name);
129 #endif
130 
132  bool include_internal_{false};
133  std::map<EntityBase *, std::string> relabel_map_id_;
134  std::map<EntityBase *, std::string> relabel_map_name_;
135 };
136 
137 } // namespace prometheus
138 } // namespace esphome
139 #endif
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:63
Base class for all cover devices.
Definition: cover.h:111
void add_node_label_(AsyncResponseStream *stream, std::string &node)
std::string relabel_id_(EntityBase *obj)
void text_sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void switch_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void add_area_label_(AsyncResponseStream *stream, std::string &area)
void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the sensor state as prometheus data point.
bool canHandle(AsyncWebServerRequest *request) override
void add_handler(AsyncWebHandler *handler)
void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the lock Values state as prometheus data point.
void binary_sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name)
void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the sensor state as prometheus data point.
void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the switch Values 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 lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the lock Values state as prometheus data point.
PrometheusHandler(web_server_base::WebServerBase *base)
void lock_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the sensor state as prometheus data point.
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)
void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the switch Values state as prometheus data point.
Implementation of SPI Controller mode.
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
void light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the Light Values state as prometheus data point.
std::map< EntityBase *, std::string > relabel_map_id_
Base class for all locks.
Definition: lock.h:103