mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-23 18:56:38 +00:00
prometheus: Unify memory metrics
The "memory fragmentation" value named "memory_ratio" was always truncated to an integer, so in all likelyhood 0. It didn't work anyway until the incorrect line termination was fixed in an earlier commit. Neither could the Psram metric be parsed correctly due to the the same incorrect line termination. With this change memory usage is reported in line with Prometheus' upstream recommendations (https://prometheus.io/docs/practices/naming/). Labels are no longer used to separate distinct dimensions. Total and free memory as well as the maximum allocation size are reported as separate metrics where available while labels are used to differenciate the separate kinds of memory (heap on all, psram on ESP32). Label values are now also lowercase on ESP32 to match ESP8266. Metrics should report their base values, not the result of a calculation. Therefore the already non-working "fragmentation" metric is dropped. It can easily be calculated in PromQL instead. The renaming of metrics and label values makes this a breaking change, especially on ESP32. With the aforementioned formatting errors which made them unusable that shouldn't be a problem. Signed-off-by: Michael Hanselmann <public@hansmi.ch>
This commit is contained in:
parent
1b96833d6a
commit
718f5fc9ab
@ -22,10 +22,13 @@
|
||||
* Prometheus support
|
||||
*
|
||||
* The text format for metrics, labels and values is documented at [1]. Only
|
||||
* the UTF-8 text encoding is supported.
|
||||
* the UTF-8 text encoding is supported. [2] describes how metrics and labels
|
||||
* should be named.
|
||||
*
|
||||
* [1]
|
||||
* https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md
|
||||
* [2]
|
||||
* https://github.com/prometheus/docs/blob/master/content/docs/practices/naming.md
|
||||
*
|
||||
\*********************************************************************************************/
|
||||
|
||||
@ -161,6 +164,28 @@ void WritePromMetricStr(const char *name, uint8_t flags, const char *value, ...)
|
||||
va_end(labels);
|
||||
}
|
||||
|
||||
// Sentinel value for known memory metrics, chosen to unlikely match actual
|
||||
// values.
|
||||
const uint32_t kPromMemoryUnknown = 0xFFFFFFFF - 1;
|
||||
|
||||
// Write metrics providing information about used and available memory.
|
||||
void WritePromMemoryMetrics(const char *type, uint32_t size, uint32_t avail, uint32_t max_alloc) {
|
||||
if (size != kPromMemoryUnknown) {
|
||||
WritePromMetricInt32(PSTR("memory_size_bytes"), kPromMetricGauge, size,
|
||||
PSTR("memory"), type, nullptr);
|
||||
}
|
||||
|
||||
WritePromMetricInt32(PSTR("memory_free_bytes"), kPromMetricGauge, avail,
|
||||
PSTR("memory"), type, nullptr);
|
||||
|
||||
if (max_alloc != kPromMemoryUnknown) {
|
||||
// The largest contiguous free memory block, useful for checking
|
||||
// fragmentation.
|
||||
WritePromMetricInt32(PSTR("memory_max_alloc_bytes"), kPromMetricGauge, max_alloc,
|
||||
PSTR("memory"), type, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void HandleMetrics(void) {
|
||||
if (!HttpCheckPriviledgedAccess()) { return; }
|
||||
|
||||
@ -211,25 +236,26 @@ void HandleMetrics(void) {
|
||||
nullptr);
|
||||
}
|
||||
|
||||
// Pseudo-metric providing metadata about the free memory.
|
||||
WritePromMemoryMetrics(PSTR("heap"),
|
||||
#ifdef ESP32
|
||||
int32_t freeMaxMem = 100 - (int32_t)(ESP_getMaxAllocHeap() * 100 / ESP_getFreeHeap());
|
||||
|
||||
WritePromMetricInt32(PSTR("memory_bytes"), kPromMetricGauge,
|
||||
ESP_getFreeHeap(), PSTR("memory"), PSTR("Ram"), nullptr);
|
||||
|
||||
// FIXME: Always truncated to integer
|
||||
WritePromMetricInt32(PSTR("memory_ratio"), kPromMetricGauge,
|
||||
freeMaxMem / 100, PSTR("memory"), PSTR("Fragmentation"), nullptr);
|
||||
ESP.getHeapSize(),
|
||||
#else
|
||||
kPromMemoryUnknown,
|
||||
#endif
|
||||
ESP_getFreeHeap(),
|
||||
#ifdef ESP32
|
||||
ESP_getMaxAllocHeap()
|
||||
#else
|
||||
kPromMemoryUnknown
|
||||
#endif
|
||||
);
|
||||
|
||||
#ifdef ESP32
|
||||
if (UsePSRAM()) {
|
||||
WritePromMetricInt32(PSTR("memory_bytes"), kPromMetricGauge,
|
||||
ESP.getFreePsram(), PSTR("memory"), PSTR("Psram"), nullptr);
|
||||
WritePromMemoryMetrics(PSTR("psram"), ESP.getPsramSize(),
|
||||
ESP.getFreePsram(), ESP.getMaxAllocPsram());
|
||||
}
|
||||
#else // ESP32
|
||||
WritePromMetricInt32(PSTR("memory_bytes"), kPromMetricGauge,
|
||||
ESP_getFreeHeap(), PSTR("memory"), PSTR("ram"), nullptr);
|
||||
#endif // ESP32
|
||||
#endif
|
||||
|
||||
#ifdef USE_ENERGY_SENSOR
|
||||
// TODO: Don't disable prefix on energy metrics
|
||||
|
Loading…
x
Reference in New Issue
Block a user