diff --git a/cobalt/browser/metrics/cobalt_metrics_service_client.cc b/cobalt/browser/metrics/cobalt_metrics_service_client.cc index 56e4068c58d5..f81987f08e63 100644 --- a/cobalt/browser/metrics/cobalt_metrics_service_client.cc +++ b/cobalt/browser/metrics/cobalt_metrics_service_client.cc @@ -153,7 +153,9 @@ CobaltMetricsServiceClient::CreateUploader( } base::TimeDelta CobaltMetricsServiceClient::GetStandardUploadInterval() { - return base::TimeDelta::FromSeconds(kStandardUploadIntervalSeconds); + return custom_upload_interval_ != UINT32_MAX + ? base::TimeDelta::FromSeconds(custom_upload_interval_) + : base::TimeDelta::FromSeconds(kStandardUploadIntervalSeconds); } bool CobaltMetricsServiceClient::IsReportingPolicyManaged() { @@ -196,6 +198,12 @@ std::string CobaltMetricsServiceClient::GetAppPackageName() { return ""; } +void CobaltMetricsServiceClient::SetUploadInterval(uint32_t interval_seconds) { + if (interval_seconds > 0) { + custom_upload_interval_ = interval_seconds; + } +} + } // namespace metrics } // namespace browser } // namespace cobalt diff --git a/cobalt/browser/metrics/cobalt_metrics_service_client.h b/cobalt/browser/metrics/cobalt_metrics_service_client.h index 9e2f3595e44f..c0505e605683 100644 --- a/cobalt/browser/metrics/cobalt_metrics_service_client.h +++ b/cobalt/browser/metrics/cobalt_metrics_service_client.h @@ -148,6 +148,10 @@ class CobaltMetricsServiceClient : public ::metrics::MetricsServiceClient { // platforms. std::string GetAppPackageName() override; + // Setter to override the upload interval default + // (kStandardUploadIntervalSeconds). + void SetUploadInterval(uint32_t interval_seconds); + explicit CobaltMetricsServiceClient( ::metrics::MetricsStateManager* state_manager, PrefService* local_state); @@ -162,6 +166,8 @@ class CobaltMetricsServiceClient : public ::metrics::MetricsServiceClient { CobaltMetricsUploaderCallback* upload_handler_ = nullptr; + uint32_t custom_upload_interval_ = UINT32_MAX; + DISALLOW_COPY_AND_ASSIGN(CobaltMetricsServiceClient); }; diff --git a/cobalt/browser/metrics/cobalt_metrics_service_client_test.cc b/cobalt/browser/metrics/cobalt_metrics_service_client_test.cc index 904693234ac6..544808e4e040 100644 --- a/cobalt/browser/metrics/cobalt_metrics_service_client_test.cc +++ b/cobalt/browser/metrics/cobalt_metrics_service_client_test.cc @@ -105,6 +105,21 @@ TEST_F(CobaltMetricsServiceClientTest, TestStubbedSystemFields) { ASSERT_EQ(client_->GetAppPackageName(), ""); } +TEST_F(CobaltMetricsServiceClientTest, UploadIntervalCanBeOverriden) { + ASSERT_EQ(client_->GetStandardUploadInterval().InSeconds(), 300); + client_->SetUploadInterval(42); + ASSERT_EQ(client_->GetStandardUploadInterval().InSeconds(), 42); + client_->SetUploadInterval(UINT32_MAX); + // UINT32_MAX is the sentinel value to revert back to the default of 5 min. + ASSERT_EQ(client_->GetStandardUploadInterval().InSeconds(), 300); +} + +TEST_F(CobaltMetricsServiceClientTest, UploadIntervalOfZeroIsIgnored) { + ASSERT_EQ(client_->GetStandardUploadInterval().InSeconds(), 300); + client_->SetUploadInterval(0); + ASSERT_EQ(client_->GetStandardUploadInterval().InSeconds(), 300); +} + } // namespace metrics } // namespace browser } // namespace cobalt diff --git a/cobalt/h5vcc/h5vcc_metrics.cc b/cobalt/h5vcc/h5vcc_metrics.cc index ea9b44f83486..14e7e079cfc4 100644 --- a/cobalt/h5vcc/h5vcc_metrics.cc +++ b/cobalt/h5vcc/h5vcc_metrics.cc @@ -26,10 +26,10 @@ namespace cobalt { namespace h5vcc { void H5vccMetrics::OnMetricEvent( - const h5vcc::MetricEventHandlerWrapper::ScriptValue& eventHandler) { + const h5vcc::MetricEventHandlerWrapper::ScriptValue& event_handler) { auto callback = new cobalt::browser::metrics::CobaltH5vccMetricsUploaderCallback( - new h5vcc::MetricEventHandlerWrapper(this, eventHandler)); + new h5vcc::MetricEventHandlerWrapper(this, event_handler)); uploader_callback_.reset(callback); browser::metrics::CobaltMetricsServiceClient* client = static_cast( @@ -38,6 +38,9 @@ void H5vccMetrics::OnMetricEvent( DCHECK(client); client->SetOnUploadHandler(uploader_callback_.get()); } +void H5vccMetrics::Enable() { ToggleMetricsEnabled(true); } + +void H5vccMetrics::Disable() { ToggleMetricsEnabled(false); } void H5vccMetrics::ToggleMetricsEnabled(bool isEnabled) { browser::metrics::CobaltMetricsServicesManagerClient* client = @@ -52,7 +55,16 @@ void H5vccMetrics::ToggleMetricsEnabled(bool isEnabled) { ->UpdateUploadPermissions(isEnabled); } -bool H5vccMetrics::GetMetricsEnabled() { return is_enabled_; } +bool H5vccMetrics::IsEnabled() { return is_enabled_; } + +void H5vccMetrics::SetMetricEventInterval(uint32_t interval_seconds) { + browser::metrics::CobaltMetricsServiceClient* client = + static_cast( + browser::metrics::CobaltMetricsServicesManager::GetInstance() + ->GetMetricsServiceClient()); + DCHECK(client); + client->SetUploadInterval(interval_seconds); +} } // namespace h5vcc } // namespace cobalt diff --git a/cobalt/h5vcc/h5vcc_metrics.h b/cobalt/h5vcc/h5vcc_metrics.h index 75cec7055e03..24b92de62ca9 100644 --- a/cobalt/h5vcc/h5vcc_metrics.h +++ b/cobalt/h5vcc/h5vcc_metrics.h @@ -47,19 +47,27 @@ class H5vccMetrics : public script::Wrappable { // Binds an event handler that will be invoked every time Cobalt wants to // upload a metrics payload. void OnMetricEvent( - const MetricEventHandlerWrapper::ScriptValue& eventHandler); + const MetricEventHandlerWrapper::ScriptValue& event_handler); - // API to explicitly enable or disable Cobalt metrics logging. Pass true - // to turn on and false to disable. If disabled, the metric event handler - // should never get called after that point. - void ToggleMetricsEnabled(bool isEnabled); + // Enable Cobalt metrics logging. + void Enable(); + + // Disable Cobalt metrics logging. + void Disable(); // Returns current enabled state of metrics logging/reporting. - bool GetMetricsEnabled(); + bool IsEnabled(); + + // Sets the interval in which metrics will be aggregated and sent to the + // metric event handler. + void SetMetricEventInterval(uint32 interval_seconds); DEFINE_WRAPPABLE_TYPE(H5vccMetrics); private: + // Internal convenience method for toggling enabled/disabled state. + void ToggleMetricsEnabled(bool isEnabled); + std::unique_ptr uploader_callback_; bool is_enabled_ = false; diff --git a/cobalt/h5vcc/h5vcc_metrics.idl b/cobalt/h5vcc/h5vcc_metrics.idl index 49c505913301..add5085d638b 100644 --- a/cobalt/h5vcc/h5vcc_metrics.idl +++ b/cobalt/h5vcc/h5vcc_metrics.idl @@ -31,15 +31,24 @@ interface H5vccMetrics { // }); void onMetricEvent(H5vccMetricEventHandler eventHandler); - // API to explicitly enable or disable Cobalt metrics logging. Pass true - // to turn on and false to disable. If disabled, the metric event handler - // should never get called afterward. Metrics are disabled by default and - // should be enabled as soon as possible. + // Enable Cobalt metrics logging. Metrics are disabled by default and should + // be enabled as soon as possible. When enabled, at regular intervals, the + // bound onMetricEvent callback will be called with telemetry payloads to + // be uploaded and logged on the server. // TODO(b/286898092): Update docs here when setting is persistent. - void toggleMetricsEnabled(boolean isEnabled); + void enable(); + + // Disable Cobalt metrics logging. If disabled, the metric event handler + // should never get called afterward. + void disable(); // Returns the current enabled state of metrics reporting. - boolean getMetricsEnabled(); + boolean isEnabled(); + + // Sets the frequency in which Cobalt metrics will be snapshotted and sent to + // the metric event handler. Defaults to 5 minutes if not set. Must be > 0, + // else will be ignored. + void setMetricEventInterval(unsigned long intervalSeconds); }; // Callback invoked when a new metric payload is ready to be published. The