tracing: Added a macro to report the metric values from the sdk to the tracing framework. (#32032)
* Added metric support
* Addressed the review comments
diff --git a/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp b/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp
index bd02f36..f116def 100644
--- a/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp
+++ b/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp
@@ -163,6 +163,7 @@
{
rssi.SetNonNull(value);
ChipLogProgress(Zcl, "The current RSSI of the Node’s Wi-Fi radio in dB: %d", value);
+ MATTER_TRACE_METRIC("wifi_rssi", value);
}
else
{
diff --git a/src/tracing/backend.h b/src/tracing/backend.h
index 3d8bec8..c57e97d 100644
--- a/src/tracing/backend.h
+++ b/src/tracing/backend.h
@@ -64,6 +64,7 @@
virtual void TraceInstant(const char * label, const char * group) {}
virtual void TraceCounter(const char * label) {}
+ virtual void TraceMetric(const char * label, int32_t value) {}
virtual void LogMessageSend(MessageSendInfo &) { TraceInstant("MessageSent", "Messaging"); }
virtual void LogMessageReceived(MessageReceivedInfo &) { TraceInstant("MessageReceived", "Messaging"); }
diff --git a/src/tracing/esp32_trace/esp32_tracing.cpp b/src/tracing/esp32_trace/esp32_tracing.cpp
index 7937772..6193587 100644
--- a/src/tracing/esp32_trace/esp32_tracing.cpp
+++ b/src/tracing/esp32_trace/esp32_tracing.cpp
@@ -153,6 +153,19 @@
{
::Insights::ESPInsightsCounter::GetInstance(label)->ReportMetrics();
}
+
+void ESP32Backend::TraceMetric(const char * label, int32_t value)
+{
+ if (!mRegistered)
+ {
+ esp_diag_metrics_register("SYS_MTR" /*Tag of metrics */, label /* Unique key 8 */, label /* label displayed on dashboard */,
+ "insights.mtr" /* hierarchical path */, ESP_DIAG_DATA_TYPE_INT /* data_type */);
+ mRegistered = true;
+ }
+ ESP_LOGI("mtr", "The value of %s is %ld ", label, value);
+ esp_diag_metrics_add_int(label, value);
+}
+
void ESP32Backend::TraceBegin(const char * label, const char * group)
{
HashValue hashValue = MurmurHash(group);
diff --git a/src/tracing/esp32_trace/esp32_tracing.h b/src/tracing/esp32_trace/esp32_tracing.h
index 6d9a277..3eb127c 100644
--- a/src/tracing/esp32_trace/esp32_tracing.h
+++ b/src/tracing/esp32_trace/esp32_tracing.h
@@ -29,6 +29,7 @@
void TraceInstant(const char * label, const char * group) override;
void TraceCounter(const char * label) override;
+ void TraceMetric(const char * label, int32_t value) override;
void LogMessageSend(MessageSendInfo &) override;
void LogMessageReceived(MessageReceivedInfo &) override;
@@ -36,6 +37,9 @@
void LogNodeLookup(NodeLookupInfo &) override;
void LogNodeDiscovered(NodeDiscoveredInfo &) override;
void LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo &) override;
+
+private:
+ bool mRegistered = false;
};
} // namespace Insights
diff --git a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h
index b95c207..8ebde17 100644
--- a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h
+++ b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h
@@ -29,6 +29,7 @@
#define MATTER_TRACE_END(label, group) ::chip::Tracing::Internal::End(label, group)
#define MATTER_TRACE_INSTANT(label, group) ::chip::Tracing::Internal::Instant(label, group)
#define MATTER_TRACE_COUNTER(label) ::chip::Tracing::Internal::Counter(label)
+#define MATTER_TRACE_METRIC(label, value) ::chip::Tracing::Internal::Metric(label, value)
namespace chip {
namespace Tracing {
diff --git a/src/tracing/json/json_tracing.cpp b/src/tracing/json/json_tracing.cpp
index af66e12..64627f0 100644
--- a/src/tracing/json/json_tracing.cpp
+++ b/src/tracing/json/json_tracing.cpp
@@ -295,6 +295,15 @@
OutputValue(value);
}
+void JsonBackend::TraceMetric(const char * label, int32_t val)
+{
+ ::Json::Value value;
+ value["label"] = label;
+ value["value"] = val;
+
+ OutputValue(value);
+}
+
void JsonBackend::LogMessageSend(MessageSendInfo & info)
{
::Json::Value value;
diff --git a/src/tracing/json/json_tracing.h b/src/tracing/json/json_tracing.h
index 302c4bd..e8ad8ee 100644
--- a/src/tracing/json/json_tracing.h
+++ b/src/tracing/json/json_tracing.h
@@ -52,6 +52,7 @@
void TraceEnd(const char * label, const char * group) override;
void TraceInstant(const char * label, const char * group) override;
void TraceCounter(const char * label) override;
+ void TraceMetric(const char * label, int32_t val) override;
void LogMessageSend(MessageSendInfo &) override;
void LogMessageReceived(MessageReceivedInfo &) override;
void LogNodeLookup(NodeLookupInfo &) override;
diff --git a/src/tracing/macros.h b/src/tracing/macros.h
index b456137..0643a46 100644
--- a/src/tracing/macros.h
+++ b/src/tracing/macros.h
@@ -26,6 +26,14 @@
// MATTER_TRACE_END(label, group)
// MATTER_TRACE_INSTANT(label, group)
// MATTER_TRACE_SCOPE(label, group)
+
+// Tracing macro to trace monotonically increasing counter values.
+// MATTER_TRACE_COUNTER(label)
+
+// Tracing macro to represent historical metric data i.e the data points which represent different
+// values at different point of time.
+// MATTER_TRACE_METRIC(label, value)
+
#include <matter/tracing/macros_impl.h>
#include <tracing/log_declares.h>
#include <tracing/registry.h>
@@ -79,6 +87,7 @@
#define MATTER_TRACE_INSTANT(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_TRACE_SCOPE(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_TRACE_COUNTER(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
+#define MATTER_TRACE_METRIC(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_LOG_MESSAGE_SEND(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_LOG_MESSAGE_RECEIVED(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
diff --git a/src/tracing/multiplexed/include/matter/tracing/macros_impl.h b/src/tracing/multiplexed/include/matter/tracing/macros_impl.h
index 8b3e728..eda5f2e 100644
--- a/src/tracing/multiplexed/include/matter/tracing/macros_impl.h
+++ b/src/tracing/multiplexed/include/matter/tracing/macros_impl.h
@@ -29,6 +29,7 @@
#define MATTER_TRACE_END(label, group) ::chip::Tracing::Internal::End(label, group)
#define MATTER_TRACE_INSTANT(label, group) ::chip::Tracing::Internal::Instant(label, group)
#define MATTER_TRACE_COUNTER(label) ::chip::Tracing::Internal::Counter(label)
+#define MATTER_TRACE_METRIC(label, value) ::chip::Tracing::Internal::Metric(label, value)
namespace chip {
namespace Tracing {
diff --git a/src/tracing/none/include/matter/tracing/macros_impl.h b/src/tracing/none/include/matter/tracing/macros_impl.h
index 3119dce..1fc9406 100644
--- a/src/tracing/none/include/matter/tracing/macros_impl.h
+++ b/src/tracing/none/include/matter/tracing/macros_impl.h
@@ -32,3 +32,4 @@
#define MATTER_TRACE_INSTANT(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_TRACE_SCOPE(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_TRACE_COUNTER(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
+#define MATTER_TRACE_METRIC(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
diff --git a/src/tracing/perfetto/include/matter/tracing/macros_impl.h b/src/tracing/perfetto/include/matter/tracing/macros_impl.h
index 9b05d2f..559e877 100644
--- a/src/tracing/perfetto/include/matter/tracing/macros_impl.h
+++ b/src/tracing/perfetto/include/matter/tracing/macros_impl.h
@@ -37,3 +37,5 @@
static int count##_label = 0; \
TRACE_COUNTER("Matter", label, ++count##_label); \
} while (0)
+
+#define MATTER_TRACE_METRIC(label, value) TRACE_COUNTER("Matter", label, value)
diff --git a/src/tracing/registry.cpp b/src/tracing/registry.cpp
index 7c43442..ee46b54 100644
--- a/src/tracing/registry.cpp
+++ b/src/tracing/registry.cpp
@@ -84,6 +84,14 @@
}
}
+void Metric(const char * label, int32_t value)
+{
+ for (auto & backend : gTracingBackends)
+ {
+ backend.TraceMetric(label, value);
+ }
+}
+
void LogMessageSend(::chip::Tracing::MessageSendInfo & info)
{
for (auto & backend : gTracingBackends)
diff --git a/src/tracing/registry.h b/src/tracing/registry.h
index 083ebcb..5f3b3c1 100644
--- a/src/tracing/registry.h
+++ b/src/tracing/registry.h
@@ -77,6 +77,7 @@
void End(const char * label, const char * group);
void Instant(const char * label, const char * group);
void Counter(const char * label);
+void Metric(const char * label, int32_t value);
void LogMessageSend(::chip::Tracing::MessageSendInfo & info);
void LogMessageReceived(::chip::Tracing::MessageReceivedInfo & info);