Merge pull request #11931 from twollweber/twollweber-patch-1

Fix Prometheus metrics parse error on DS18x20 and align other metrics
This commit is contained in:
Adrian Scillato 2021-04-28 18:11:54 -03:00 committed by GitHub
commit 70d222a62d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 5 deletions

View File

@ -50,6 +50,9 @@ const char *UnitfromType(const char *type) // find unit for measurment type
if (strcmp(type, "humidity") == 0) { if (strcmp(type, "humidity") == 0) {
return "percentage"; return "percentage";
} }
if (strcmp(type, "id") == 0) {
return "untyped";
}
return ""; return "";
} }
@ -86,15 +89,15 @@ void HandleMetrics(void) {
if (!isnan(TasmotaGlobal.temperature_celsius)) { if (!isnan(TasmotaGlobal.temperature_celsius)) {
dtostrfd(TasmotaGlobal.temperature_celsius, Settings.flag2.temperature_resolution, parameter); dtostrfd(TasmotaGlobal.temperature_celsius, Settings.flag2.temperature_resolution, parameter);
WSContentSend_P(PSTR("# TYPE tasmotaglobal_temperature_celsius gauge\ntasmotaglobal_temperature_celsius %s\n"), parameter); WSContentSend_P(PSTR("# TYPE tasmota_global_temperature_celsius gauge\ntasmota_global_temperature_celsius %s\n"), parameter);
} }
if (TasmotaGlobal.humidity != 0) { if (TasmotaGlobal.humidity != 0) {
dtostrfd(TasmotaGlobal.humidity, Settings.flag2.humidity_resolution, parameter); dtostrfd(TasmotaGlobal.humidity, Settings.flag2.humidity_resolution, parameter);
WSContentSend_P(PSTR("# TYPE tasmotaglobal_humidity gauge\ntasmotaglobal_humidity %s\n"), parameter); WSContentSend_P(PSTR("# TYPE tasmota_global_humidity gauge\ntasmota_global_humidity_percentage %s\n"), parameter);
} }
if (TasmotaGlobal.pressure_hpa != 0) { if (TasmotaGlobal.pressure_hpa != 0) {
dtostrfd(TasmotaGlobal.pressure_hpa, Settings.flag2.pressure_resolution, parameter); dtostrfd(TasmotaGlobal.pressure_hpa, Settings.flag2.pressure_resolution, parameter);
WSContentSend_P(PSTR("# TYPE tasmotaglobal_pressure_hpa gauge\ntasmotaglobal_pressure_hpa %s\n"), parameter); WSContentSend_P(PSTR("# TYPE tasmota_global_pressure_hpa gauge\ntasmota_global_pressure_hpa %s\n"), parameter);
} }
#ifdef USE_ENERGY_SENSOR #ifdef USE_ENERGY_SENSOR
@ -148,12 +151,17 @@ void HandleMetrics(void) {
String type = FormatMetricName(key2.getStr()); String type = FormatMetricName(key2.getStr());
const char *unit = UnitfromType(type.c_str()); const char *unit = UnitfromType(type.c_str());
if (strcmp(type.c_str(), "totalstarttime") != 0) { // this metric causes prometheus of fail if (strcmp(type.c_str(), "totalstarttime") != 0) { // this metric causes prometheus of fail
if (strcmp(type.c_str(), "id") == 0) { // this metric is NaN, so convert it to a label, see Wi-Fi metrics above
WSContentSend_P(PSTR("# TYPE tasmota_sensors_%s_%s gauge\ntasmota_sensors_%s_%s{sensor=\"%s\",id=\"%s\"} 1\n"),
type.c_str(), unit, type.c_str(), unit, sensor.c_str(), value);
} else {
WSContentSend_P(PSTR("# TYPE tasmota_sensors_%s_%s gauge\ntasmota_sensors_%s_%s{sensor=\"%s\"} %s\n"), WSContentSend_P(PSTR("# TYPE tasmota_sensors_%s_%s gauge\ntasmota_sensors_%s_%s{sensor=\"%s\"} %s\n"),
type.c_str(), unit, type.c_str(), unit, sensor.c_str(), value); type.c_str(), unit, type.c_str(), unit, sensor.c_str(), value);
} }
} }
} }
} }
}
} else { } else {
const char *value = value1.getStr(nullptr); const char *value = value1.getStr(nullptr);
String sensor = FormatMetricName(key1.getStr()); String sensor = FormatMetricName(key1.getStr());