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

Add two features for deprecating JSON P3A data #20788

Merged
merged 1 commit into from
Nov 17, 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
14 changes: 14 additions & 0 deletions components/p3a/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/components/p3a/features.h"
#include "brave/components/p3a/metric_log_type.h"

namespace p3a {
namespace features {
Expand All @@ -14,6 +15,12 @@ BASE_FEATURE(kConstellation,
BASE_FEATURE(kConstellationEnclaveAttestation,
"BraveP3AConstellationEnclaveAttestation",
base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kTypicalJSONDeprecation,
"BraveP3ATypicalJSONDeprecation",
base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kOtherJSONDeprecation,
"BraveP3AOtherJSONDeprecation",
base::FEATURE_DISABLED_BY_DEFAULT);

bool IsConstellationEnabled() {
return base::FeatureList::IsEnabled(features::kConstellation);
Expand All @@ -24,5 +31,12 @@ bool IsConstellationEnclaveAttestationEnabled() {
features::kConstellationEnclaveAttestation);
}

bool IsJSONDeprecated(MetricLogType log_type) {
if (log_type == MetricLogType::kTypical) {
return base::FeatureList::IsEnabled(features::kTypicalJSONDeprecation);
}
return base::FeatureList::IsEnabled(features::kOtherJSONDeprecation);
}

} // namespace features
} // namespace p3a
8 changes: 8 additions & 0 deletions components/p3a/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define BRAVE_COMPONENTS_P3A_FEATURES_H_

#include "base/feature_list.h"
#include "brave/components/p3a/metric_log_type.h"

namespace p3a {
namespace features {
Expand All @@ -16,8 +17,15 @@ BASE_DECLARE_FEATURE(kConstellation);
// See https://github.com/brave/brave-browser/issues/31718 for more info.
BASE_DECLARE_FEATURE(kConstellationEnclaveAttestation);

// See https://github.com/brave/brave-browser/issues/34003 for more info.
// Disables JSON measurements for "typical" cadence.
BASE_DECLARE_FEATURE(kTypicalJSONDeprecation);
// Disables JSON measurements for all other cadences.
BASE_DECLARE_FEATURE(kOtherJSONDeprecation);

bool IsConstellationEnabled();
bool IsConstellationEnclaveAttestationEnabled();
bool IsJSONDeprecated(MetricLogType log_type);

} // namespace features
} // namespace p3a
Expand Down
41 changes: 26 additions & 15 deletions components/p3a/message_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ MessageManager::MessageManager(PrefService& local_state,

// Init log stores.
for (MetricLogType log_type : kAllMetricLogTypes) {
json_log_stores_[log_type] =
std::make_unique<MetricLogStore>(*this, *local_state_, false, log_type);
json_log_stores_[log_type]->LoadPersistedUnsentLogs();
if (!features::IsJSONDeprecated(log_type)) {
json_log_stores_[log_type] = std::make_unique<MetricLogStore>(
*this, *local_state_, false, log_type);
json_log_stores_[log_type]->LoadPersistedUnsentLogs();
}
if (features::IsConstellationEnabled()) {
constellation_prep_log_stores_[log_type] =
std::make_unique<MetricLogStore>(*this, *local_state_, true,
Expand Down Expand Up @@ -86,11 +88,13 @@ void MessageManager::Init(
config_.get());

for (MetricLogType log_type : kAllMetricLogTypes) {
json_upload_schedulers_[log_type] = std::make_unique<Scheduler>(
base::BindRepeating(&MessageManager::StartScheduledUpload,
base::Unretained(this), false, log_type),
config_->randomize_upload_interval, config_->average_upload_interval);
json_upload_schedulers_[log_type]->Start();
if (!features::IsJSONDeprecated(log_type)) {
json_upload_schedulers_[log_type] = std::make_unique<Scheduler>(
base::BindRepeating(&MessageManager::StartScheduledUpload,
base::Unretained(this), false, log_type),
config_->randomize_upload_interval, config_->average_upload_interval);
json_upload_schedulers_[log_type]->Start();
}
}

rotation_scheduler_ = std::make_unique<RotationScheduler>(
Expand Down Expand Up @@ -128,9 +132,9 @@ void MessageManager::UpdateMetricValue(
constellation_prep_log_stores_[log_type]->UpdateValue(
std::string(histogram_name), bucket);
}
if (update_for_all || !*only_update_for_constellation) {
json_log_stores_[log_type].get()->UpdateValue(std::string(histogram_name),
bucket);
auto* json_log_store = json_log_stores_[log_type].get();
if ((update_for_all || !*only_update_for_constellation) && json_log_store) {
json_log_store->UpdateValue(std::string(histogram_name), bucket);
}
}

Expand All @@ -139,9 +143,9 @@ void MessageManager::RemoveMetricValue(
absl::optional<bool> only_update_for_constellation) {
bool update_for_all = !only_update_for_constellation.has_value();
for (MetricLogType log_type : kAllMetricLogTypes) {
if (update_for_all || !*only_update_for_constellation) {
json_log_stores_[log_type]->RemoveValueIfExists(
std::string(histogram_name));
auto* json_log_store = json_log_stores_[log_type].get();
if ((update_for_all || !*only_update_for_constellation) && json_log_store) {
json_log_store->RemoveValueIfExists(std::string(histogram_name));
}
if (features::IsConstellationEnabled() &&
(update_for_all || *only_update_for_constellation)) {
Expand All @@ -153,7 +157,10 @@ void MessageManager::RemoveMetricValue(

void MessageManager::DoJsonRotation(MetricLogType log_type) {
VLOG(2) << "MessageManager doing json rotation at " << base::Time::Now();
json_log_stores_[log_type]->ResetUploadStamps();
auto* log_store = json_log_stores_[log_type].get();
if (log_store) {
log_store->ResetUploadStamps();
}
delegate_->OnRotation(log_type, false);
}

Expand Down Expand Up @@ -185,11 +192,13 @@ void MessageManager::OnLogUploadComplete(bool is_ok,
} else {
log_store = (metrics::LogStore*)json_log_stores_[log_type].get();
scheduler = json_upload_schedulers_[log_type].get();
CHECK(log_store);
if (is_ok) {
delegate_->OnMetricCycled(json_log_stores_[log_type]->staged_log_key(),
false);
}
}
CHECK(scheduler);
if (is_ok) {
log_store->MarkStagedLogAsSent();
log_store->DiscardStagedLog();
Expand Down Expand Up @@ -257,6 +266,8 @@ void MessageManager::StartScheduledUpload(bool is_constellation,
log_store = json_log_stores_[log_type].get();
scheduler = json_upload_schedulers_[log_type].get();
}
CHECK(log_store);
CHECK(scheduler);

if (!is_constellation &&
base::Time::Now() -
Expand Down