Commit 5025355f authored by tby's avatar tby Committed by Commit Bot

[Structured metrics] Prepare for recording independent metrics

This implements the ProvideIndependentMetrics API within the structured
metrics provider, ready to upload independent metrics. We can't actually
enable this yet, because of issues described in the TODO.

Unit tests to come once a related CL is also landed.

Bug: 1148168
Change-Id: I4fb739c7cbb2ecea54da82fe8382140cdf562946
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2543882Reviewed-by: default avatarRachel Wong <wrong@chromium.org>
Commit-Queue: Tony Yeoman <tby@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828589}
parent ba1144b1
......@@ -107,8 +107,29 @@ void StructuredMetricsProvider::PrefStoreErrorDelegate::OnError(
LogPrefReadError(error);
}
base::Value* StructuredMetricsProvider::GetEventsList(
StructuredMetricsProvider::StorageType
StructuredMetricsProvider::StorageTypeForIdType(
EventBase::IdentifierType type) {
switch (type) {
case EventBase::IdentifierType::kUmaId:
return StructuredMetricsProvider::StorageType::kAssociated;
case EventBase::IdentifierType::kProjectId:
case EventBase::IdentifierType::kUnidentified:
return StructuredMetricsProvider::StorageType::kIndependent;
}
}
base::StringPiece StructuredMetricsProvider::ListKeyForStorageType(
StructuredMetricsProvider::StorageType type) {
switch (type) {
case StructuredMetricsProvider::StorageType::kAssociated:
return base::StringPiece(kAssociatedEventsKey);
case StructuredMetricsProvider::StorageType::kIndependent:
return base::StringPiece(kIndependentEventsKey);
}
}
base::Value* StructuredMetricsProvider::GetEventsList(StorageType type) {
// Ensure the events key exists. The "events" key was a list of event objects,
// and is now a dict of lists. Migrate to the new layout if needed.
base::Value* events = nullptr;
......@@ -132,17 +153,7 @@ base::Value* StructuredMetricsProvider::GetEventsList(
// Choose the key for |type|, ensure the list Value actually exists, and
// return it.
base::StringPiece list_key;
switch (type) {
case EventBase::IdentifierType::kUmaId:
list_key = base::StringPiece(kAssociatedEventsKey);
break;
case EventBase::IdentifierType::kProjectId:
case EventBase::IdentifierType::kUnidentified:
list_key = base::StringPiece(kIndependentEventsKey);
break;
}
const base::StringPiece list_key = ListKeyForStorageType(type);
base::Value* events_list = events->FindKey(list_key);
if (events_list) {
return events_list;
......@@ -202,8 +213,7 @@ void StructuredMetricsProvider::OnRecord(const EventBase& event) {
// Add the event to |storage_|.
// TODO(crbug.com/1016655): Choose the event list based on the identifier type
// of the event subclass.
GetEventsList(EventBase::IdentifierType::kUmaId)
->Append(std::move(event_value));
GetEventsList(StorageType::kAssociated)->Append(std::move(event_value));
}
void StructuredMetricsProvider::OnProfileAdded(
......@@ -265,12 +275,48 @@ void StructuredMetricsProvider::ProvideCurrentSessionData(
if (!recording_enabled_ || !initialized_)
return;
base::Value* events = GetEventsList(EventBase::IdentifierType::kUmaId);
base::Value* events = GetEventsList(StorageType::kAssociated);
PopulateUmaProto(events, uma_proto);
LogNumEventsInUpload(events->GetList().size());
events->ClearList();
}
bool StructuredMetricsProvider::HasIndependentMetrics() {
// TODO(crbug.com/1148168): We cannot enable independent metrics uploads yet,
// because we will overwhelm the unsent log store shared across UMA, resulting
// in logs being dropped for long sessions.
return false;
}
void StructuredMetricsProvider::ProvideIndependentMetrics(
base::OnceCallback<void(bool)> done_callback,
ChromeUserMetricsExtension* uma_proto,
base::HistogramSnapshotManager*) {
DCHECK(base::CurrentUIThread::IsSet());
if (!recording_enabled_ || !initialized_) {
std::move(done_callback).Run(false);
return;
}
base::Value* events = GetEventsList(StorageType::kIndependent);
const size_t num_events = events->GetList().size();
if (num_events == 0) {
std::move(done_callback).Run(false);
return;
}
// TODO(crbug.com/1148168): Add histograms for independent metrics upload
// size.
// Independent events should not be associated with the client_id, so clear
// it.
uma_proto->clear_client_id();
PopulateUmaProto(events, uma_proto);
events->ClearList();
std::move(done_callback).Run(true);
}
void StructuredMetricsProvider::CommitPendingWriteForTest() {
storage_->CommitPendingWrite();
}
......
......@@ -86,6 +86,11 @@ class StructuredMetricsProvider : public metrics::MetricsProvider,
friend class Recorder;
friend class StructuredMetricsProviderTest;
enum class StorageType {
kAssociated,
kIndependent,
};
// An error delegate called when |storage_| has finished reading prefs from
// disk.
class PrefStoreErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
......@@ -97,13 +102,21 @@ class StructuredMetricsProvider : public metrics::MetricsProvider,
void OnError(PersistentPrefStore::PrefReadError error) override;
};
base::Value* GetEventsList(EventBase::IdentifierType type);
StructuredMetricsProvider::StorageType StorageTypeForIdType(
EventBase::IdentifierType type);
base::StringPiece ListKeyForStorageType(
StructuredMetricsProvider::StorageType type);
base::Value* GetEventsList(StorageType type);
// metrics::MetricsProvider:
void OnRecordingEnabled() override;
void OnRecordingDisabled() override;
void ProvideCurrentSessionData(
metrics::ChromeUserMetricsExtension* uma_proto) override;
bool HasIndependentMetrics() override;
void ProvideIndependentMetrics(base::OnceCallback<void(bool)> done_callback,
ChromeUserMetricsExtension* uma_proto,
base::HistogramSnapshotManager*) override;
// Recorder::Observer:
void OnRecord(const EventBase& event) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment