From d1fd2425f744ab46a87dec4ed4d3f0680e819dbc Mon Sep 17 00:00:00 2001 From: Bookatz Date: Wed, 22 Nov 2017 15:21:03 -0800 Subject: [PATCH] statsd informs incidentd of anomalies When an anomaly is detected by statsd, it passes to incidentd the information specified in the config. Test: manual (using the fake config) saw in logcat that incidentd was called (reportIncident) Change-Id: I0a257b49db706d7b14a2976fb7e62c3c3535725f --- cmds/statsd/src/anomaly/AnomalyTracker.cpp | 28 +++++++++++++++++++++- cmds/statsd/src/anomaly/AnomalyTracker.h | 3 +++ cmds/statsd/src/config/ConfigManager.cpp | 6 +++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/cmds/statsd/src/anomaly/AnomalyTracker.cpp b/cmds/statsd/src/anomaly/AnomalyTracker.cpp index c2bf233c103b5..7bacb441ee48a 100644 --- a/cmds/statsd/src/anomaly/AnomalyTracker.cpp +++ b/cmds/statsd/src/anomaly/AnomalyTracker.cpp @@ -19,6 +19,9 @@ #include "AnomalyTracker.h" +#include +#include +#include #include namespace android { @@ -213,7 +216,7 @@ void AnomalyTracker::declareAnomaly(const uint64_t& timestampNs) { // TODO: Can construct a name based on the criteria (and/or relay the criteria). ALOGW("An anomaly (nameless) has occurred! Informing incidentd."); } - // TODO: informIncidentd(); + informIncidentd(); } else { ALOGW("An anomaly has occurred! (But informing incidentd not requested.)"); } @@ -314,6 +317,29 @@ void AnomalyTracker::informAlarmsFired(const uint64_t& timestampNs, } } +void AnomalyTracker::informIncidentd() { + VLOG("informIncidentd called."); + if (!mAlert.has_incidentd_details()) { + ALOGE("Attempted to call incidentd without any incidentd_details."); + return; + } + sp service = interface_cast( + defaultServiceManager()->getService(android::String16("incident"))); + if (service == NULL) { + ALOGW("Couldn't get the incident service."); + return; + } + + IncidentReportArgs incidentReport; + const Alert::IncidentdDetails& details = mAlert.incidentd_details(); + for (int i = 0; i < details.section_size(); i++) { + incidentReport.addSection(details.section(i)); + } + // TODO: Pass in mAlert.name() into the addHeader? + + service->reportIncident(incidentReport); +} + } // namespace statsd } // namespace os } // namespace android diff --git a/cmds/statsd/src/anomaly/AnomalyTracker.h b/cmds/statsd/src/anomaly/AnomalyTracker.h index afa6fee99c6bc..49e83235f73be 100644 --- a/cmds/statsd/src/anomaly/AnomalyTracker.h +++ b/cmds/statsd/src/anomaly/AnomalyTracker.h @@ -148,6 +148,9 @@ protected: // Resets all bucket data. For use when all the data gets stale. void resetStorage(); + // Informs the incident service that an anomaly has occurred. + void informIncidentd(); + FRIEND_TEST(AnomalyTrackerTest, TestConsecutiveBuckets); FRIEND_TEST(AnomalyTrackerTest, TestSparseBuckets); FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection); diff --git a/cmds/statsd/src/config/ConfigManager.cpp b/cmds/statsd/src/config/ConfigManager.cpp index ac2192cdb3c58..445ee59947e9f 100644 --- a/cmds/statsd/src/config/ConfigManager.cpp +++ b/cmds/statsd/src/config/ConfigManager.cpp @@ -210,6 +210,9 @@ StatsdConfig build_fake_config() { alert->set_number_of_buckets(6); alert->set_trigger_if_sum_gt(10); alert->set_refractory_period_secs(30); + Alert::IncidentdDetails* details = alert->mutable_incidentd_details(); + details->add_section(12); + details->add_section(13); // Count process state changes, slice by uid. metric = config.add_count_metric(); @@ -226,6 +229,9 @@ StatsdConfig build_fake_config() { alert->set_number_of_buckets(4); alert->set_trigger_if_sum_gt(30); alert->set_refractory_period_secs(20); + details = alert->mutable_incidentd_details(); + details->add_section(14); + details->add_section(15); // Count process state changes, slice by uid, while SCREEN_IS_OFF metric = config.add_count_metric();