Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick PR #652: Update h5vcc_metrics.idl interface #734

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cobalt/browser/metrics/cobalt_metrics_service_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions cobalt/browser/metrics/cobalt_metrics_service_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
};

Expand Down
15 changes: 15 additions & 0 deletions cobalt/browser/metrics/cobalt_metrics_service_client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 15 additions & 3 deletions cobalt/h5vcc/h5vcc_metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<browser::metrics::CobaltMetricsServiceClient*>(
Expand All @@ -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 =
Expand All @@ -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::CobaltMetricsServiceClient*>(
browser::metrics::CobaltMetricsServicesManager::GetInstance()
->GetMetricsServiceClient());
DCHECK(client);
client->SetUploadInterval(interval_seconds);
}

} // namespace h5vcc
} // namespace cobalt
20 changes: 14 additions & 6 deletions cobalt/h5vcc/h5vcc_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<cobalt::browser::metrics::CobaltH5vccMetricsUploaderCallback>
uploader_callback_;
bool is_enabled_ = false;
Expand Down
21 changes: 15 additions & 6 deletions cobalt/h5vcc/h5vcc_metrics.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down