ESPHome 2025.5.0
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) override {
44 if (request->method() == HTTP_GET) {
45 if (request->url() == "/metrics")
46 return true;
47 }
48
49 return false;
50 }
51
52 void handleRequest(AsyncWebServerRequest *req) override;
53
54 void setup() override {
55 this->base_->init();
56 this->base_->add_handler(this);
57 }
58 float get_setup_priority() const override {
59 // After WiFi
60 return setup_priority::WIFI - 1.0f;
61 }
62
63 protected:
64 std::string relabel_id_(EntityBase *obj);
65 std::string relabel_name_(EntityBase *obj);
66 void add_area_label_(AsyncResponseStream *stream, std::string &area);
67 void add_node_label_(AsyncResponseStream *stream, std::string &node);
68 void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name);
69
70#ifdef USE_SENSOR
72 void sensor_type_(AsyncResponseStream *stream);
74 void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area, std::string &node,
75 std::string &friendly_name);
76#endif
77
78#ifdef USE_BINARY_SENSOR
80 void binary_sensor_type_(AsyncResponseStream *stream);
82 void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area,
83 std::string &node, std::string &friendly_name);
84#endif
85
86#ifdef USE_FAN
88 void fan_type_(AsyncResponseStream *stream);
90 void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node,
91 std::string &friendly_name);
92#endif
93
94#ifdef USE_LIGHT
96 void light_type_(AsyncResponseStream *stream);
98 void light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area, std::string &node,
99 std::string &friendly_name);
100#endif
101
102#ifdef USE_COVER
104 void cover_type_(AsyncResponseStream *stream);
106 void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node,
107 std::string &friendly_name);
108#endif
109
110#ifdef USE_SWITCH
112 void switch_type_(AsyncResponseStream *stream);
114 void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area, std::string &node,
115 std::string &friendly_name);
116#endif
117
118#ifdef USE_LOCK
120 void lock_type_(AsyncResponseStream *stream);
122 void lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node,
123 std::string &friendly_name);
124#endif
125
126#ifdef USE_TEXT_SENSOR
128 void text_sensor_type_(AsyncResponseStream *stream);
130 void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node,
131 std::string &friendly_name);
132#endif
133
134#ifdef USE_NUMBER
136 void number_type_(AsyncResponseStream *stream);
138 void number_row_(AsyncResponseStream *stream, number::Number *obj, std::string &area, std::string &node,
139 std::string &friendly_name);
140#endif
141
142#ifdef USE_SELECT
144 void select_type_(AsyncResponseStream *stream);
146 void select_row_(AsyncResponseStream *stream, select::Select *obj, std::string &area, std::string &node,
147 std::string &friendly_name);
148#endif
149
150#ifdef USE_MEDIA_PLAYER
152 void media_player_type_(AsyncResponseStream *stream);
154 void media_player_row_(AsyncResponseStream *stream, media_player::MediaPlayer *obj, std::string &area,
155 std::string &node, std::string &friendly_name);
156#endif
157
158#ifdef USE_UPDATE
160 void update_entity_type_(AsyncResponseStream *stream);
162 void update_entity_row_(AsyncResponseStream *stream, update::UpdateEntity *obj, std::string &area, std::string &node,
163 std::string &friendly_name);
164 void handle_update_state_(AsyncResponseStream *stream, update::UpdateState state);
165#endif
166
167#ifdef USE_VALVE
169 void valve_type_(AsyncResponseStream *stream);
171 void valve_row_(AsyncResponseStream *stream, valve::Valve *obj, std::string &area, std::string &node,
172 std::string &friendly_name);
173#endif
174
175#ifdef USE_CLIMATE
177 void climate_type_(AsyncResponseStream *stream);
179 void climate_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
180 std::string &friendly_name);
181 void climate_failed_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
182 std::string &friendly_name, std::string &category, bool is_failed_value);
183 void climate_setting_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
184 std::string &friendly_name, std::string &setting, const LogString *setting_value);
185 void climate_value_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node,
186 std::string &friendly_name, std::string &category, std::string &climate_value);
187#endif
188
190 bool include_internal_{false};
191 std::map<EntityBase *, std::string> relabel_map_id_;
192 std::map<EntityBase *, std::string> relabel_map_name_;
193};
194
195} // namespace prometheus
196} // namespace esphome
197#endif
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:168
Base class for all cover devices.
Definition cover.h:111
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 locks.
Definition lock.h:103
Base-class for all numbers.
Definition number.h:39
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 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.
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 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 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.
bool canHandle(AsyncWebServerRequest *request) override
void add_node_label_(AsyncResponseStream *stream, std::string &node)
void handleRequest(AsyncWebServerRequest *req) override
void climate_value_row_(AsyncResponseStream *stream, climate::Climate *obj, std::string &area, std::string &node, std::string &friendly_name, std::string &category, std::string &climate_value)
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 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:31
Base-class for all sensors.
Definition sensor.h:57
Base class for all switches.
Definition switch.h:39
Base class for all valve devices.
Definition valve.h:105
void add_handler(AsyncWebHandler *handler)
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7