ESPHome  2024.9.0
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 
64 #ifdef USE_SENSOR
65  void sensor_type_(AsyncResponseStream *stream);
68  void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj);
69 #endif
70 
71 #ifdef USE_BINARY_SENSOR
72  void binary_sensor_type_(AsyncResponseStream *stream);
75  void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj);
76 #endif
77 
78 #ifdef USE_FAN
79  void fan_type_(AsyncResponseStream *stream);
82  void fan_row_(AsyncResponseStream *stream, fan::Fan *obj);
83 #endif
84 
85 #ifdef USE_LIGHT
86  void light_type_(AsyncResponseStream *stream);
89  void light_row_(AsyncResponseStream *stream, light::LightState *obj);
90 #endif
91 
92 #ifdef USE_COVER
93  void cover_type_(AsyncResponseStream *stream);
96  void cover_row_(AsyncResponseStream *stream, cover::Cover *obj);
97 #endif
98 
99 #ifdef USE_SWITCH
100  void switch_type_(AsyncResponseStream *stream);
103  void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj);
104 #endif
105 
106 #ifdef USE_LOCK
107  void lock_type_(AsyncResponseStream *stream);
110  void lock_row_(AsyncResponseStream *stream, lock::Lock *obj);
111 #endif
112 
114  bool include_internal_{false};
115  std::map<EntityBase *, std::string> relabel_map_id_;
116  std::map<EntityBase *, std::string> relabel_map_name_;
117 };
118 
119 } // namespace prometheus
120 } // namespace esphome
121 #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: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)
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
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.