ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
prometheus_handler.h
Go to the documentation of this file.
1#pragma once
3#if defined(USE_NETWORK) && !defined(USE_ZEPHYR) && !defined(USE_HOST)
4#include <map>
5#include <utility>
6
12#ifdef USE_CLIMATE
13#include "esphome/core/log.h"
14#endif
15
16namespace esphome::prometheus {
17
18class PrometheusHandler final : public AsyncWebHandler, public Component {
19 public:
21
27 void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
28
34 void add_label_id(EntityBase *obj, const std::string &value) { relabel_map_id_.insert({obj, value}); }
35
41 void add_label_name(EntityBase *obj, const std::string &value) { relabel_map_name_.insert({obj, value}); }
42
43 bool canHandle(AsyncWebServerRequest *request) const override {
44 if (request->method() != HTTP_GET)
45 return false;
46#ifdef USE_ESP32
47 char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
48 return request->url_to(url_buf) == "/metrics";
49#else
50 return request->url() == ESPHOME_F("/metrics");
51#endif
52 }
53
54 void handleRequest(AsyncWebServerRequest *req) override;
55
56 void setup() override {
57 this->base_->init();
58 this->base_->add_handler(this);
59 }
60 float get_setup_priority() const override {
61 // After WiFi
62 return setup_priority::WIFI - 1.0f;
63 }
64
65 protected:
66 void print_relabel_id_(AsyncResponseStream *stream, EntityBase *obj);
67 void print_relabel_name_(AsyncResponseStream *stream, EntityBase *obj);
68 void add_area_label_(AsyncResponseStream *stream, std::string &area);
69 void add_node_label_(AsyncResponseStream *stream, std::string &node);
70 void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name);
72 void print_metric_labels_(AsyncResponseStream *stream, ProgmemStr metric_name, EntityBase *obj, std::string &area,
73 std::string &node, std::string &friendly_name);
74
75#ifdef USE_SENSOR
77 void sensor_type_(AsyncResponseStream *stream);
79 void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area, std::string &node,
80 std::string &friendly_name);
81#endif
82
83#ifdef USE_BINARY_SENSOR
85 void binary_sensor_type_(AsyncResponseStream *stream);
87 void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area,
88 std::string &node, std::string &friendly_name);
89#endif
90
91#ifdef USE_FAN
93 void fan_type_(AsyncResponseStream *stream);
95 void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node,
96 std::string &friendly_name);
97#endif
98
99#ifdef USE_LIGHT
101 void light_type_(AsyncResponseStream *stream);
103 void light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area, std::string &node,
104 std::string &friendly_name);
105#endif
106
107#ifdef USE_COVER
109 void cover_type_(AsyncResponseStream *stream);
111 void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node,
112 std::string &friendly_name);
113#endif
114
115#ifdef USE_SWITCH
117 void switch_type_(AsyncResponseStream *stream);
119 void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area, std::string &node,
120 std::string &friendly_name);
121#endif
122
123#ifdef USE_LOCK
125 void lock_type_(AsyncResponseStream *stream);
127 void lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node,
128 std::string &friendly_name);
129#endif
130
131#ifdef USE_EVENT
133 void event_type_(AsyncResponseStream *stream);
135 void event_row_(AsyncResponseStream *stream, event::Event *obj, std::string &area, std::string &node,
136 std::string &friendly_name);
137#endif
138
139#ifdef USE_TEXT
141 void text_type_(AsyncResponseStream *stream);
143 void text_row_(AsyncResponseStream *stream, text::Text *obj, std::string &area, std::string &node,
144 std::string &friendly_name);
145#endif
146
147#ifdef USE_TEXT_SENSOR
149 void text_sensor_type_(AsyncResponseStream *stream);
151 void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node,
152 std::string &friendly_name);
153#endif
154
155#ifdef USE_NUMBER
157 void number_type_(AsyncResponseStream *stream);
159 void number_row_(AsyncResponseStream *stream, number::Number *obj, std::string &area, std::string &node,
160 std::string &friendly_name);
161#endif
162
163#ifdef USE_SELECT
165 void select_type_(AsyncResponseStream *stream);
167 void select_row_(AsyncResponseStream *stream, select::Select *obj, std::string &area, std::string &node,
168 std::string &friendly_name);
169#endif
170
171#ifdef USE_MEDIA_PLAYER
173 void media_player_type_(AsyncResponseStream *stream);
175 void media_player_row_(AsyncResponseStream *stream, media_player::MediaPlayer *obj, std::string &area,
176 std::string &node, std::string &friendly_name);
177#endif
178
179#ifdef USE_UPDATE
181 void update_entity_type_(AsyncResponseStream *stream);
183 void update_entity_row_(AsyncResponseStream *stream, update::UpdateEntity *obj, std::string &area, std::string &node,
184 std::string &friendly_name);
185 void handle_update_state_(AsyncResponseStream *stream, update::UpdateState state);
186#endif
187
188#ifdef USE_VALVE
190 void valve_type_(AsyncResponseStream *stream);
192 void valve_row_(AsyncResponseStream *stream, valve::Valve *obj, std::string &area, std::string &node,
193 std::string &friendly_name);
194#endif
195
196#ifdef USE_CLIMATE
198 void climate_type_(AsyncResponseStream *stream);
200 void climate_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
201 std::string &friendly_name);
202 void climate_failed_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
203 std::string &friendly_name, std::string &category, bool is_failed_value);
204 void climate_setting_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
205 std::string &friendly_name, std::string &setting, const LogString *setting_value);
206 void climate_value_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
207 std::string &friendly_name, std::string &category, const char *climate_value);
208#endif
209
211 bool include_internal_{false};
212 std::map<EntityBase *, std::string> relabel_map_id_;
213 std::map<EntityBase *, std::string> relabel_map_name_;
214};
215
216} // namespace esphome::prometheus
217
218#endif // USE_NETWORK && !USE_ZEPHYR
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:192
Base class for all cover devices.
Definition cover.h:115
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
Base class for all locks.
Definition lock.h:112
Base-class for all numbers.
Definition number.h:29
void number_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void add_label_id(EntityBase *obj, const std::string &value)
Add the value for an entity's "id" label.
void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name)
void add_label_name(EntityBase *obj, const std::string &value)
Add the value for an entity's "name" label.
web_server_base::WebServerBase * base_
void handle_update_state_(AsyncResponseStream *stream, update::UpdateState state)
void climate_setting_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node, std::string &friendly_name, std::string &setting, const LogString *setting_value)
void event_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void media_player_row_(AsyncResponseStream *stream, media_player::MediaPlayer *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the media player state as prometheus data point.
void light_type_(AsyncResponseStream *stream)
Return the type for prometheus.
bool canHandle(AsyncWebServerRequest *request) const override
void print_relabel_name_(AsyncResponseStream *stream, EntityBase *obj)
void text_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void set_include_internal(bool include_internal)
Determine whether internal components should be exported as metrics.
void update_entity_row_(AsyncResponseStream *stream, update::UpdateEntity *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the update state and info as prometheus data point.
std::map< EntityBase *, std::string > relabel_map_id_
void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the fan state as prometheus data point.
void climate_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void binary_sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
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 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 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.
void climate_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the climate state as prometheus data point.
void update_entity_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void climate_failed_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node, std::string &friendly_name, std::string &category, bool is_failed_value)
void print_relabel_id_(AsyncResponseStream *stream, EntityBase *obj)
void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the binary sensor state as prometheus data point.
void number_row_(AsyncResponseStream *stream, number::Number *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the number state as prometheus data point.
void valve_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void switch_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void media_player_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void event_row_(AsyncResponseStream *stream, event::Event *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the event values state as prometheus data point.
void text_sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the text sensor values state as prometheus data point.
void add_area_label_(AsyncResponseStream *stream, std::string &area)
void print_metric_labels_(AsyncResponseStream *stream, ProgmemStr metric_name, EntityBase *obj, std::string &area, std::string &node, std::string &friendly_name)
Print metric name and common labels (id, area, node, friendly_name, name)
void select_row_(AsyncResponseStream *stream, select::Select *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the select state as prometheus data point.
void cover_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void select_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void add_node_label_(AsyncResponseStream *stream, std::string &node)
void handleRequest(AsyncWebServerRequest *req) override
void text_row_(AsyncResponseStream *stream, text::Text *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the text values state as prometheus data point.
PrometheusHandler(web_server_base::WebServerBase *base)
void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the cover values state as prometheus data point.
void lock_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void valve_row_(AsyncResponseStream *stream, valve::Valve *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the valve state as prometheus data point.
void climate_value_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node, std::string &friendly_name, std::string &category, const char *climate_value)
void fan_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
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.
Base-class for all selects.
Definition select.h:28
Base-class for all sensors.
Definition sensor.h:47
Base class for all switches.
Definition switch.h:43
Base-class for all text inputs.
Definition text.h:20
Base class for all valve devices.
Definition valve.h:102
void add_handler(AsyncWebHandler *handler)
bool state
Definition fan.h:2
constexpr float WIFI
Definition component.h:50
const __FlashStringHelper * ProgmemStr
Definition progmem.h:31