ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
prometheus_handler.h
Go to the documentation of this file.
1#pragma once
3#ifdef USE_NETWORK
4#include <map>
5#include <utility>
6
11#ifdef USE_CLIMATE
12#include "esphome/core/log.h"
13#endif
14
15namespace esphome {
16namespace prometheus {
17
18class PrometheusHandler : 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 std::string relabel_id_(EntityBase *obj);
67 std::string relabel_name_(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#ifdef USE_ESP8266
73 void print_metric_labels_(AsyncResponseStream *stream, const __FlashStringHelper *metric_name, EntityBase *obj,
74 std::string &area, std::string &node, std::string &friendly_name);
75#else
76 void print_metric_labels_(AsyncResponseStream *stream, const char *metric_name, EntityBase *obj, std::string &area,
77 std::string &node, std::string &friendly_name);
78#endif
79
80#ifdef USE_SENSOR
82 void sensor_type_(AsyncResponseStream *stream);
84 void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area, std::string &node,
85 std::string &friendly_name);
86#endif
87
88#ifdef USE_BINARY_SENSOR
90 void binary_sensor_type_(AsyncResponseStream *stream);
92 void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area,
93 std::string &node, std::string &friendly_name);
94#endif
95
96#ifdef USE_FAN
98 void fan_type_(AsyncResponseStream *stream);
100 void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node,
101 std::string &friendly_name);
102#endif
103
104#ifdef USE_LIGHT
106 void light_type_(AsyncResponseStream *stream);
108 void light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area, std::string &node,
109 std::string &friendly_name);
110#endif
111
112#ifdef USE_COVER
114 void cover_type_(AsyncResponseStream *stream);
116 void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node,
117 std::string &friendly_name);
118#endif
119
120#ifdef USE_SWITCH
122 void switch_type_(AsyncResponseStream *stream);
124 void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area, std::string &node,
125 std::string &friendly_name);
126#endif
127
128#ifdef USE_LOCK
130 void lock_type_(AsyncResponseStream *stream);
132 void lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node,
133 std::string &friendly_name);
134#endif
135
136#ifdef USE_EVENT
138 void event_type_(AsyncResponseStream *stream);
140 void event_row_(AsyncResponseStream *stream, event::Event *obj, std::string &area, std::string &node,
141 std::string &friendly_name);
142#endif
143
144#ifdef USE_TEXT
146 void text_type_(AsyncResponseStream *stream);
148 void text_row_(AsyncResponseStream *stream, text::Text *obj, std::string &area, std::string &node,
149 std::string &friendly_name);
150#endif
151
152#ifdef USE_TEXT_SENSOR
154 void text_sensor_type_(AsyncResponseStream *stream);
156 void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node,
157 std::string &friendly_name);
158#endif
159
160#ifdef USE_NUMBER
162 void number_type_(AsyncResponseStream *stream);
164 void number_row_(AsyncResponseStream *stream, number::Number *obj, std::string &area, std::string &node,
165 std::string &friendly_name);
166#endif
167
168#ifdef USE_SELECT
170 void select_type_(AsyncResponseStream *stream);
172 void select_row_(AsyncResponseStream *stream, select::Select *obj, std::string &area, std::string &node,
173 std::string &friendly_name);
174#endif
175
176#ifdef USE_MEDIA_PLAYER
178 void media_player_type_(AsyncResponseStream *stream);
180 void media_player_row_(AsyncResponseStream *stream, media_player::MediaPlayer *obj, std::string &area,
181 std::string &node, std::string &friendly_name);
182#endif
183
184#ifdef USE_UPDATE
186 void update_entity_type_(AsyncResponseStream *stream);
188 void update_entity_row_(AsyncResponseStream *stream, update::UpdateEntity *obj, std::string &area, std::string &node,
189 std::string &friendly_name);
190 void handle_update_state_(AsyncResponseStream *stream, update::UpdateState state);
191#endif
192
193#ifdef USE_VALVE
195 void valve_type_(AsyncResponseStream *stream);
197 void valve_row_(AsyncResponseStream *stream, valve::Valve *obj, std::string &area, std::string &node,
198 std::string &friendly_name);
199#endif
200
201#ifdef USE_CLIMATE
203 void climate_type_(AsyncResponseStream *stream);
205 void climate_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
206 std::string &friendly_name);
207 void climate_failed_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
208 std::string &friendly_name, std::string &category, bool is_failed_value);
209 void climate_setting_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
210 std::string &friendly_name, std::string &setting, const LogString *setting_value);
211 void climate_value_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
212 std::string &friendly_name, std::string &category, const char *climate_value);
213#endif
214
216 bool include_internal_{false};
217 std::map<EntityBase *, std::string> relabel_map_id_;
218 std::map<EntityBase *, std::string> relabel_map_name_;
219};
220
221} // namespace prometheus
222} // namespace esphome
223#endif
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:186
Base class for all cover devices.
Definition cover.h:110
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
Base class for all locks.
Definition lock.h:110
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_metric_labels_(AsyncResponseStream *stream, const char *metric_name, EntityBase *obj, std::string &area, std::string &node, std::string &friendly_name)
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 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 print_metric_labels_(AsyncResponseStream *stream, const __FlashStringHelper *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 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.
std::string relabel_id_(EntityBase *obj)
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 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.
std::string relabel_name_(EntityBase *obj)
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:29
Base-class for all sensors.
Definition sensor.h:47
Base class for all switches.
Definition switch.h:38
Base-class for all text inputs.
Definition text.h:21
Base class for all valve devices.
Definition valve.h:104
void add_handler(AsyncWebHandler *handler)
bool state
Definition fan.h:2
constexpr float WIFI
Definition component.h:36
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7