mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-14 19:59:53 +03:00
chore: reserve string capacity for prometheus metrics
This commit is contained in:
@@ -1699,15 +1699,33 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
|
|||||||
std::string result;
|
std::string result;
|
||||||
std::unordered_map<std::string, bool> seen_metrics;
|
std::unordered_map<std::string, bool> seen_metrics;
|
||||||
|
|
||||||
|
// Helper function to check if a device value type is supported for Prometheus metrics
|
||||||
|
auto is_supported_type = [](uint8_t type) -> bool {
|
||||||
|
return type == DeviceValueType::BOOL || type == DeviceValueType::UINT8 || type == DeviceValueType::INT8
|
||||||
|
|| type == DeviceValueType::UINT16 || type == DeviceValueType::INT16 || type == DeviceValueType::UINT24
|
||||||
|
|| type == DeviceValueType::UINT32 || type == DeviceValueType::TIME || type == DeviceValueType::ENUM;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Dynamically reserve memory for the result
|
||||||
|
size_t entity_count = 0;
|
||||||
|
for (const auto & dv : devicevalues_) {
|
||||||
|
if (tag >= 0 && tag != dv.tag) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// only count supported types
|
||||||
|
if (is_supported_type(dv.type)) {
|
||||||
|
entity_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.reserve(160 * entity_count);
|
||||||
|
|
||||||
for (auto & dv : devicevalues_) {
|
for (auto & dv : devicevalues_) {
|
||||||
if (tag >= 0 && tag != dv.tag) {
|
if (tag >= 0 && tag != dv.tag) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// only process number, boolean and enum types
|
// only process number, boolean and enum types
|
||||||
if (dv.type != DeviceValueType::BOOL && dv.type != DeviceValueType::UINT8 && dv.type != DeviceValueType::INT8 && dv.type != DeviceValueType::UINT16
|
if (!is_supported_type(dv.type)) {
|
||||||
&& dv.type != DeviceValueType::INT16 && dv.type != DeviceValueType::UINT24 && dv.type != DeviceValueType::UINT32 && dv.type != DeviceValueType::TIME
|
|
||||||
&& dv.type != DeviceValueType::ENUM) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1884,6 +1902,8 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
|
|||||||
result += "\n";
|
result += "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.shrink_to_fit();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1568,6 +1568,8 @@ std::string System::get_metrics_prometheus() {
|
|||||||
std::string result;
|
std::string result;
|
||||||
std::unordered_map<std::string, bool> seen_metrics;
|
std::unordered_map<std::string, bool> seen_metrics;
|
||||||
|
|
||||||
|
result.reserve(16000);
|
||||||
|
|
||||||
// get system data
|
// get system data
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonObject root = doc.to<JsonObject>();
|
JsonObject root = doc.to<JsonObject>();
|
||||||
@@ -1797,6 +1799,8 @@ std::string System::get_metrics_prometheus() {
|
|||||||
// process root object
|
// process root object
|
||||||
process_object(root, "");
|
process_object(root, "");
|
||||||
|
|
||||||
|
result.shrink_to_fit();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user