From c67b3e3cbdf944786b53001f98e82e217dc8cc89 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Fri, 24 Feb 2023 19:10:35 +0100 Subject: [PATCH] Prometheus API: Set correct metrics type --- include/WebApi_prometheus.h | 25 +++++++++++++++++++++++++ src/WebApi_prometheus.cpp | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/include/WebApi_prometheus.h b/include/WebApi_prometheus.h index ac2478a..5eb894e 100644 --- a/include/WebApi_prometheus.h +++ b/include/WebApi_prometheus.h @@ -3,6 +3,7 @@ #include #include +#include class WebApiPrometheusClass { public: @@ -15,4 +16,28 @@ private: void addField(AsyncResponseStream* stream, String& serial, uint8_t idx, std::shared_ptr inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, const char* channelName = NULL); AsyncWebServer* _server; + + enum { + METRIC_TYPE_NONE = 0, + METRIC_TYPE_GAUGE, + METRIC_TYPE_COUNTER, + }; + const char* _metricTypes[3] = { 0, "gauge", "counter" }; + + std::map _fieldMetricAssignment { + { FLD_UDC, METRIC_TYPE_GAUGE }, + { FLD_IDC, METRIC_TYPE_GAUGE }, + { FLD_PDC, METRIC_TYPE_GAUGE }, + { FLD_YD, METRIC_TYPE_COUNTER }, + { FLD_YT, METRIC_TYPE_COUNTER }, + { FLD_UAC, METRIC_TYPE_GAUGE }, + { FLD_IAC, METRIC_TYPE_GAUGE }, + { FLD_PAC, METRIC_TYPE_GAUGE }, + { FLD_F, METRIC_TYPE_GAUGE }, + { FLD_T, METRIC_TYPE_GAUGE }, + { FLD_PF, METRIC_TYPE_GAUGE }, + { FLD_EFF, METRIC_TYPE_GAUGE }, + { FLD_IRR, METRIC_TYPE_GAUGE }, + { FLD_PRA, METRIC_TYPE_GAUGE } + }; }; \ No newline at end of file diff --git a/src/WebApi_prometheus.cpp b/src/WebApi_prometheus.cpp index a805ee8..28f0b94 100644 --- a/src/WebApi_prometheus.cpp +++ b/src/WebApi_prometheus.cpp @@ -105,7 +105,7 @@ void WebApiPrometheusClass::addField(AsyncResponseStream* stream, String& serial const char* chanName = (channelName == NULL) ? inv->Statistics()->getChannelFieldName(type, channel, fieldId) : channelName; if (idx == 0 && type == TYPE_AC && channel == 0) { stream->printf("# HELP opendtu_%s in %s\n", chanName, inv->Statistics()->getChannelFieldUnit(type, channel, fieldId)); - stream->printf("# TYPE opendtu_%s gauge\n", chanName); + stream->printf("# TYPE opendtu_%s %s\n", chanName, _metricTypes[_fieldMetricAssignment[fieldId]]); } stream->printf("opendtu_%s{serial=\"%s\",unit=\"%d\",name=\"%s\",type=\"%s\",channel=\"%d\"} %f\n", chanName,