Skip to content

Commit

Permalink
Use persistent settings for telemetry controls. (youtube#755)
Browse files Browse the repository at this point in the history
* Use persistent settings for telemetry controls.

This allows telemetry state to persist through app lifecycles (important
since the web app has a different lifecyle).

b/286898092

Change-Id: I819f8d31420af8d6c9d4f2ce7f5afeada893fd0e

* Remove is_enabled_ and set interval default

b/286898092

Change-Id: Idb96655fc9bb56cf04a6a0c29e481ea824fcc47f
  • Loading branch information
joeltine authored Jun 28, 2023
1 parent 4a0aee2 commit ff1b81e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
41 changes: 27 additions & 14 deletions cobalt/browser/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -693,25 +693,14 @@ Application::Application(const base::Closure& quit_closure, bool should_preload,
// URLRequestContext;
base::TaskScheduler::CreateAndStartWithDefaultParams("Cobalt TaskScheduler");

// Must be called early as it initializes global state which is then read by
// all threads without synchronization.
// RecordAction task runner must be called before metric initialization.
base::SetRecordActionTaskRunner(base::ThreadTaskRunnerHandle::Get());
metrics_services_manager_ =
metrics::CobaltMetricsServicesManager::GetInstance();
// Metric recording state initialization _must_ happen before we bootstrap
// otherwise we crash.
metrics_services_manager_->GetMetricsService()
->InitializeMetricsRecordingState();
// UpdateUploadPermissions bootstraps the whole metric reporting, scheduling,
// and uploading cycle.
metrics_services_manager_->UpdateUploadPermissions(false);

// Initializes persistent settings.
persistent_settings_ =
std::make_unique<persistent_storage::PersistentSettings>(
kPersistentSettingsJson);

// Initialize telemetry/metrics.
InitMetrics();

// Initializes Watchdog.
watchdog::Watchdog* watchdog =
watchdog::Watchdog::CreateInstance(persistent_settings_.get());
Expand Down Expand Up @@ -1558,6 +1547,30 @@ void Application::DispatchDeepLinkIfNotConsumed() {
}
}

void Application::InitMetrics() {
// Must be called early as it initializes global state which is then read by
// all threads without synchronization.
// RecordAction task runner must be called before metric initialization.
base::SetRecordActionTaskRunner(base::ThreadTaskRunnerHandle::Get());
metrics_services_manager_ =
metrics::CobaltMetricsServicesManager::GetInstance();
// Before initializing metrics manager, set any persisted settings like if
// it's enabled or upload interval.
bool is_metrics_enabled = persistent_settings_->GetPersistentSettingAsBool(
metrics::kMetricEnabledSettingName, false);
metrics_services_manager_->SetUploadInterval(
persistent_settings_->GetPersistentSettingAsInt(
metrics::kMetricEventIntervalSettingName, 300));
metrics_services_manager_->ToggleMetricsEnabled(is_metrics_enabled);
// Metric recording state initialization _must_ happen before we bootstrap
// otherwise we crash.
metrics_services_manager_->GetMetricsService()
->InitializeMetricsRecordingState();
// UpdateUploadPermissions bootstraps the whole metric reporting, scheduling,
// and uploading cycle.
metrics_services_manager_->UpdateUploadPermissions(is_metrics_enabled);
}

} // namespace browser
} // namespace cobalt

Expand Down
7 changes: 7 additions & 0 deletions cobalt/browser/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ class Application {
void DispatchDeepLink(const char* link, SbTimeMonotonic timestamp);
void DispatchDeepLinkIfNotConsumed();


// Initializes all code necessary to start telemetry/metrics gathering and
// reporting. See go/cobalt-telemetry.
void InitMetrics();

// Reference to the current metrics manager, the highest level control point
// for metrics/telemetry.
metrics::CobaltMetricsServicesManager* metrics_services_manager_;

DISALLOW_COPY_AND_ASSIGN(Application);
Expand Down
7 changes: 7 additions & 0 deletions cobalt/browser/metrics/cobalt_metrics_services_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ namespace cobalt {
namespace browser {
namespace metrics {


// Persistent setting name for the metric event/upload interval.
constexpr char kMetricEventIntervalSettingName[] = "metricEventInterval";

// Persistent setting name for whether metrics are enabled or disabled.
constexpr char kMetricEnabledSettingName[] = "metricsEnabledState";

// A static wrapper around CobaltMetricsServicesManager. We need a way
// to provide a static instance of the "Cobaltified" MetricsServicesManager (and
// its public APIs) to control metrics behavior outside of //cobalt/browser
Expand Down
2 changes: 1 addition & 1 deletion cobalt/h5vcc/h5vcc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ H5vcc::H5vcc(const Settings& settings) {
audio_config_array_ = new H5vccAudioConfigArray();
c_val_ = new dom::CValView();
crash_log_ = new H5vccCrashLog();
metrics_ = new H5vccMetrics();
metrics_ = new H5vccMetrics(settings.persistent_settings);
runtime_ = new H5vccRuntime(settings.event_dispatcher);
settings_ =
new H5vccSettings(settings.set_web_setting_func, settings.media_module,
Expand Down
13 changes: 11 additions & 2 deletions cobalt/h5vcc/h5vcc_metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <string>

#include "base/values.h"
#include "cobalt/browser/metrics/cobalt_metrics_service_client.h"
#include "cobalt/browser/metrics/cobalt_metrics_services_manager.h"
#include "cobalt/h5vcc/h5vcc_metric_type.h"
Expand Down Expand Up @@ -59,14 +60,22 @@ void H5vccMetrics::Enable() { ToggleMetricsEnabled(true); }
void H5vccMetrics::Disable() { ToggleMetricsEnabled(false); }

void H5vccMetrics::ToggleMetricsEnabled(bool is_enabled) {
is_enabled_ = is_enabled;
persistent_settings_->SetPersistentSetting(
browser::metrics::kMetricEnabledSettingName,
std::make_unique<base::Value>(is_enabled));
browser::metrics::CobaltMetricsServicesManager::GetInstance()
->ToggleMetricsEnabled(is_enabled);
}

bool H5vccMetrics::IsEnabled() { return is_enabled_; }
bool H5vccMetrics::IsEnabled() {
return browser::metrics::CobaltMetricsServicesManager::GetInstance()
->IsMetricsReportingEnabled();
}

void H5vccMetrics::SetMetricEventInterval(uint32_t interval_seconds) {
persistent_settings_->SetPersistentSetting(
browser::metrics::kMetricEventIntervalSettingName,
std::make_unique<base::Value>(static_cast<int>(interval_seconds)));
browser::metrics::CobaltMetricsServicesManager::GetInstance()
->SetUploadInterval(interval_seconds);
}
Expand Down
10 changes: 7 additions & 3 deletions cobalt/h5vcc/h5vcc_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "cobalt/browser/metrics/cobalt_metrics_uploader_callback.h"
#include "cobalt/h5vcc/h5vcc_metric_type.h"
#include "cobalt/h5vcc/metric_event_handler_wrapper.h"
#include "cobalt/persistent_storage/persistent_settings.h"
#include "cobalt/script/callback_function.h"
#include "cobalt/script/script_value.h"
#include "cobalt/script/wrappable.h"
Expand All @@ -41,7 +42,10 @@ class H5vccMetrics : public script::Wrappable {
// its entirety.
typedef MetricEventHandler H5vccMetricEventHandler;

H5vccMetrics() : task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
explicit H5vccMetrics(
persistent_storage::PersistentSettings* persistent_settings)
: task_runner_(base::ThreadTaskRunnerHandle::Get()),
persistent_settings_(persistent_settings) {}

H5vccMetrics(const H5vccMetrics&) = delete;
H5vccMetrics& operator=(const H5vccMetrics&) = delete;
Expand Down Expand Up @@ -82,9 +86,9 @@ class H5vccMetrics : public script::Wrappable {
std::unique_ptr<cobalt::browser::metrics::CobaltMetricsUploaderCallback>
run_event_handler_callback_;

bool is_enabled_ = false;

scoped_refptr<base::SingleThreadTaskRunner> const task_runner_;

persistent_storage::PersistentSettings* persistent_settings_;
};

} // namespace h5vcc
Expand Down

0 comments on commit ff1b81e

Please sign in to comment.