Add Prometheus metrics relabeling (#3734)

Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
This commit is contained in:
Jan Grewe
2022-08-30 00:55:55 +02:00
committed by GitHub
parent 2819166539
commit 84bac8356a
6 changed files with 117 additions and 52 deletions
@@ -2,6 +2,9 @@
#ifdef USE_ARDUINO
#include <map>
#include <utility>
#include "esphome/components/web_server_base/web_server_base.h"
#include "esphome/core/controller.h"
#include "esphome/core/component.h"
@@ -20,6 +23,20 @@ class PrometheusHandler : public AsyncWebHandler, public Component {
*/
void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
/** Add the value for an entity's "id" label.
*
* @param obj The entity for which to set the "id" label
* @param value The value for the "id" label
*/
void add_label_id(EntityBase *obj, const std::string &value) { relabel_map_id_.insert({obj, value}); }
/** Add the value for an entity's "name" label.
*
* @param obj The entity for which to set the "name" label
* @param value The value for the "name" label
*/
void add_label_name(EntityBase *obj, const std::string &value) { relabel_map_name_.insert({obj, value}); }
bool canHandle(AsyncWebServerRequest *request) override {
if (request->method() == HTTP_GET) {
if (request->url() == "/metrics")
@@ -41,6 +58,9 @@ class PrometheusHandler : public AsyncWebHandler, public Component {
}
protected:
std::string relabel_id_(EntityBase *obj);
std::string relabel_name_(EntityBase *obj);
#ifdef USE_SENSOR
/// Return the type for prometheus
void sensor_type_(AsyncResponseStream *stream);
@@ -92,6 +112,8 @@ class PrometheusHandler : public AsyncWebHandler, public Component {
web_server_base::WebServerBase *base_;
bool include_internal_{false};
std::map<EntityBase *, std::string> relabel_map_id_;
std::map<EntityBase *, std::string> relabel_map_name_;
};
} // namespace prometheus