Add more prometheus metrics (#7683)

This commit is contained in:
Jordan Zucker 2024-10-28 20:43:02 -07:00 committed by GitHub
parent 444c0fc67f
commit 90b076eccd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 64 additions and 0 deletions

View file

@ -50,6 +50,12 @@ void PrometheusHandler::handleRequest(AsyncWebServerRequest *req) {
this->lock_row_(stream, obj); this->lock_row_(stream, obj);
#endif #endif
#ifdef USE_TEXT_SENSOR
this->text_sensor_type_(stream);
for (auto *obj : App.get_text_sensors())
this->text_sensor_row_(stream, obj);
#endif
req->send(stream); req->send(stream);
} }
@ -349,6 +355,43 @@ void PrometheusHandler::lock_row_(AsyncResponseStream *stream, lock::Lock *obj)
} }
#endif #endif
// Type-specific implementation
#ifdef USE_TEXT_SENSOR
void PrometheusHandler::text_sensor_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_text_sensor_value gauge\n"));
stream->print(F("#TYPE esphome_text_sensor_failed gauge\n"));
}
void PrometheusHandler::text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj) {
if (obj->is_internal() && !this->include_internal_)
return;
if (obj->has_state()) {
// We have a valid value, output this value
stream->print(F("esphome_text_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n"));
// Data itself
stream->print(F("esphome_text_sensor_value{id=\""));
stream->print(relabel_id_(obj).c_str());
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\",value=\""));
stream->print(obj->state.c_str());
stream->print(F("\"} "));
stream->print(F("1.0"));
stream->print(F("\n"));
} else {
// Invalid state
stream->print(F("esphome_text_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 1\n"));
}
}
#endif
} // namespace prometheus } // namespace prometheus
} // namespace esphome } // namespace esphome
#endif #endif

View file

@ -110,6 +110,13 @@ class PrometheusHandler : public AsyncWebHandler, public Component {
void lock_row_(AsyncResponseStream *stream, lock::Lock *obj); void lock_row_(AsyncResponseStream *stream, lock::Lock *obj);
#endif #endif
#ifdef USE_TEXT_SENSOR
/// Return the type for prometheus
void text_sensor_type_(AsyncResponseStream *stream);
/// Return the lock Values state as prometheus data point
void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj);
#endif
web_server_base::WebServerBase *base_; web_server_base::WebServerBase *base_;
bool include_internal_{false}; bool include_internal_{false};
std::map<EntityBase *, std::string> relabel_map_id_; std::map<EntityBase *, std::string> relabel_map_id_;

View file

@ -13,9 +13,23 @@ sensor:
} }
update_interval: 60s update_interval: 60s
text_sensor:
- platform: template
id: template_text_sensor1
lambda: |-
if (millis() > 10000) {
return {"Hello World"};
} else {
return {"Goodbye (cruel) World"};
}
update_interval: 60s
prometheus: prometheus:
include_internal: true include_internal: true
relabel: relabel:
template_sensor1: template_sensor1:
id: hellow_world id: hellow_world
name: Hello World name: Hello World
template_text_sensor1:
id: hello_text
name: Text Substitution