Commit c3dfc5b5 authored by olivierrobin's avatar olivierrobin Committed by Commit bot

Metrics log modification to handle external components.

This CL adds methods to extract the content of a log in an external module
and import it to upload in the main application.

BUG=474625

Review URL: https://codereview.chromium.org/1030503003

Cr-Commit-Position: refs/heads/master@{#324085}
parent 4b219b20
......@@ -27,8 +27,8 @@
#include "chrome/browser/ui/browser_otr_state.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/crash_keys.h"
#include "chrome/common/metrics/version_utils.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
#include "components/metrics/call_stack_profile_metrics_provider.h"
......@@ -81,24 +81,6 @@ namespace {
// data.
const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds.
metrics::SystemProfileProto::Channel AsProtobufChannel(
chrome::VersionInfo::Channel channel) {
switch (channel) {
case chrome::VersionInfo::CHANNEL_UNKNOWN:
return metrics::SystemProfileProto::CHANNEL_UNKNOWN;
case chrome::VersionInfo::CHANNEL_CANARY:
return metrics::SystemProfileProto::CHANNEL_CANARY;
case chrome::VersionInfo::CHANNEL_DEV:
return metrics::SystemProfileProto::CHANNEL_DEV;
case chrome::VersionInfo::CHANNEL_BETA:
return metrics::SystemProfileProto::CHANNEL_BETA;
case chrome::VersionInfo::CHANNEL_STABLE:
return metrics::SystemProfileProto::CHANNEL_STABLE;
}
NOTREACHED();
return metrics::SystemProfileProto::CHANNEL_UNKNOWN;
}
// Standard interval between log uploads, in seconds.
#if defined(OS_ANDROID) || defined(OS_IOS)
const int kStandardUploadIntervalSeconds = 5 * 60; // Five minutes.
......@@ -199,18 +181,11 @@ bool ChromeMetricsServiceClient::GetBrand(std::string* brand_code) {
}
metrics::SystemProfileProto::Channel ChromeMetricsServiceClient::GetChannel() {
return AsProtobufChannel(chrome::VersionInfo::GetChannel());
return metrics::AsProtobufChannel(chrome::VersionInfo::GetChannel());
}
std::string ChromeMetricsServiceClient::GetVersionString() {
chrome::VersionInfo version_info;
std::string version = version_info.Version();
#if defined(ARCH_CPU_64_BITS)
version += "-64";
#endif // defined(ARCH_CPU_64_BITS)
if (!version_info.IsOfficialBuild())
version.append("-devel");
return version;
return metrics::GetVersionString();
}
void ChromeMetricsServiceClient::OnLogUploadComplete() {
......
......@@ -79,6 +79,8 @@
'common/media/webrtc_logging_message_data.h',
'common/media/webrtc_logging_messages.h',
'common/media_galleries/metadata_types.h',
'common/metrics/version_utils.cc',
'common/metrics/version_utils.h',
'common/multi_process_lock.h',
'common/multi_process_lock_linux.cc',
'common/multi_process_lock_mac.cc',
......
include_rules = [
"+components/metrics/proto/system_profile.pb.h",
]
asvitkine@chromium.org
isherman@chromium.org
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/metrics/version_utils.h"
#include "base/logging.h"
#include "chrome/common/chrome_version_info_values.h"
namespace metrics {
std::string GetVersionString() {
std::string version = PRODUCT_VERSION;
#if defined(ARCH_CPU_64_BITS)
version += "-64";
#endif // defined(ARCH_CPU_64_BITS)
if (!IS_OFFICIAL_BUILD)
version.append("-devel");
return version;
}
SystemProfileProto::Channel AsProtobufChannel(
chrome::VersionInfo::Channel channel) {
switch (channel) {
case chrome::VersionInfo::CHANNEL_UNKNOWN:
return SystemProfileProto::CHANNEL_UNKNOWN;
case chrome::VersionInfo::CHANNEL_CANARY:
return SystemProfileProto::CHANNEL_CANARY;
case chrome::VersionInfo::CHANNEL_DEV:
return SystemProfileProto::CHANNEL_DEV;
case chrome::VersionInfo::CHANNEL_BETA:
return SystemProfileProto::CHANNEL_BETA;
case chrome::VersionInfo::CHANNEL_STABLE:
return SystemProfileProto::CHANNEL_STABLE;
}
NOTREACHED();
return SystemProfileProto::CHANNEL_UNKNOWN;
}
} // namespace metrics
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_COMMON_METRICS_VERSION_UTILS_H_
#define CHROME_COMMON_METRICS_VERSION_UTILS_H_
#include <string>
#include "chrome/common/chrome_version_info.h"
#include "components/metrics/proto/system_profile.pb.h"
namespace metrics {
// Build a string including the Chrome app version, suffixed by "-64" on 64-bit
// platforms, and "-devel" on developer builds.
std::string GetVersionString();
// Translates chrome::VersionInfo::Channel to the equivalent
// SystemProfileProto::Channel.
SystemProfileProto::Channel AsProtobufChannel(
chrome::VersionInfo::Channel channel);
} // namespace metrics
#endif // CHROME_COMMON_METRICS_VERSION_UTILS_H_
......@@ -56,17 +56,6 @@ bool IsTestingID(const std::string& id) {
return id.size() < 16;
}
// Returns the date at which the current metrics client ID was created as
// a string containing seconds since the epoch, or "0" if none was found.
std::string GetMetricsEnabledDate(PrefService* pref) {
if (!pref) {
NOTREACHED();
return "0";
}
return pref->GetString(prefs::kMetricsReportingEnabledTimestamp);
}
// Computes a SHA-1 hash of |data| and returns it as a hex string.
std::string ComputeSHA1(const std::string& data) {
const std::string sha1 = base::SHA1HashString(data);
......@@ -302,7 +291,8 @@ void MetricsLog::WriteRealtimeStabilityAttributes(
void MetricsLog::RecordEnvironment(
const std::vector<MetricsProvider*>& metrics_providers,
const std::vector<variations::ActiveGroupId>& synthetic_trials,
int64 install_date) {
int64 install_date,
int64 metrics_reporting_enabled_date) {
DCHECK(!HasEnvironment());
SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
......@@ -311,13 +301,9 @@ void MetricsLog::RecordEnvironment(
if (client_->GetBrand(&brand_code))
system_profile->set_brand_code(brand_code);
int enabled_date;
bool success =
base::StringToInt(GetMetricsEnabledDate(local_state_), &enabled_date);
DCHECK(success);
// Reduce granularity of the enabled_date field to nearest hour.
system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date));
system_profile->set_uma_enabled_date(
RoundSecondsToHour(metrics_reporting_enabled_date));
// Reduce granularity of the install_date field to nearest hour.
system_profile->set_install_date(RoundSecondsToHour(install_date));
......
......@@ -93,7 +93,8 @@ class MetricsLog {
void RecordEnvironment(
const std::vector<MetricsProvider*>& metrics_providers,
const std::vector<variations::ActiveGroupId>& synthetic_trials,
int64 install_date);
int64 install_date,
int64 metrics_reporting_enabled_date);
// Loads the environment proto that was saved by the last RecordEnvironment()
// call from prefs and clears the pref value. Returns true on success or false
......@@ -146,6 +147,9 @@ class MetricsLog {
std::vector<variations::ActiveGroupId>* field_trial_ids) const;
ChromeUserMetricsExtension* uma_proto() { return &uma_proto_; }
// Exposed to allow subclass to access to export the uma_proto. Can be used
// by external components to export logs to Chrome.
const ChromeUserMetricsExtension* uma_proto() const {
return &uma_proto_;
}
......
......@@ -90,10 +90,11 @@ class MetricsLogManager {
// Loads any unsent logs from persistent storage.
void LoadPersistedUnsentLogs();
private:
// Saves |log_data| as the given type.
// Saves |log_data| as the given type. Public to allow to push log created by
// external components.
void StoreLog(const std::string& log_data, MetricsLog::LogType log_type);
private:
// Tracks whether unsent logs (if any) have been loaded from the serializer.
bool unsent_logs_loaded_;
......
......@@ -250,7 +250,7 @@ TEST_F(MetricsLogTest, RecordEnvironment) {
log.RecordEnvironment(std::vector<MetricsProvider*>(),
synthetic_trials,
kInstallDate);
kInstallDate, kEnabledDate);
// Check that the system profile on the log has the correct values set.
CheckSystemProfile(log.system_profile());
......@@ -286,7 +286,7 @@ TEST_F(MetricsLogTest, LoadSavedEnvironmentFromPrefs) {
kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
log.RecordEnvironment(std::vector<MetricsProvider*>(),
std::vector<variations::ActiveGroupId>(),
kInstallDate);
kInstallDate, kEnabledDate);
EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
}
......@@ -310,7 +310,7 @@ TEST_F(MetricsLogTest, LoadSavedEnvironmentFromPrefs) {
// Call RecordEnvironment() to record the pref again.
log.RecordEnvironment(std::vector<MetricsProvider*>(),
std::vector<variations::ActiveGroupId>(),
kInstallDate);
kInstallDate, kEnabledDate);
}
{
......@@ -335,7 +335,7 @@ TEST_F(MetricsLogTest, InitialLogStabilityMetrics) {
std::vector<MetricsProvider*> metrics_providers;
log.RecordEnvironment(metrics_providers,
std::vector<variations::ActiveGroupId>(),
kInstallDate);
kInstallDate, kEnabledDate);
log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(),
base::TimeDelta());
const SystemProfileProto_Stability& stability =
......@@ -358,7 +358,7 @@ TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) {
std::vector<MetricsProvider*> metrics_providers;
log.RecordEnvironment(metrics_providers,
std::vector<variations::ActiveGroupId>(),
kInstallDate);
kInstallDate, kEnabledDate);
log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(),
base::TimeDelta());
const SystemProfileProto_Stability& stability =
......
......@@ -355,6 +355,10 @@ int64 MetricsService::GetInstallDate() {
return local_state_->GetInt64(prefs::kInstallDate);
}
int64 MetricsService::GetMetricsReportingEnabledDate() {
return local_state_->GetInt64(prefs::kMetricsReportingEnabledTimestamp);
}
scoped_ptr<const base::FieldTrial::EntropyProvider>
MetricsService::CreateEntropyProvider() {
// TODO(asvitkine): Refactor the code so that MetricsService does not expose
......@@ -522,6 +526,10 @@ void MetricsService::ClearSavedStabilityMetrics() {
local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
}
void MetricsService::PushExternalLog(const std::string& log) {
log_manager_.StoreLog(log, MetricsLog::ONGOING_LOG);
}
//------------------------------------------------------------------------------
// private methods
//------------------------------------------------------------------------------
......@@ -1072,7 +1080,7 @@ void MetricsService::RecordCurrentEnvironment(MetricsLog* log) {
std::vector<variations::ActiveGroupId> synthetic_trials;
GetCurrentSyntheticFieldTrials(&synthetic_trials);
log->RecordEnvironment(metrics_providers_.get(), synthetic_trials,
GetInstallDate());
GetInstallDate(), GetMetricsReportingEnabledDate());
UMA_HISTOGRAM_COUNTS_100("UMA.SyntheticTrials.Count",
synthetic_trials.size());
}
......
......@@ -151,6 +151,10 @@ class MetricsService : public base::HistogramFlattener {
// Returns the install date of the application, in seconds since the epoch.
int64 GetInstallDate();
// Returns the date at which the current metrics client ID was created as
// an int64 containing seconds since the epoch.
int64 GetMetricsReportingEnabledDate();
// Returns the preferred entropy provider used to seed persistent activities
// based on whether or not metrics reporting will be permitted on this client.
//
......@@ -243,6 +247,9 @@ class MetricsService : public base::HistogramFlattener {
// Clears the stability metrics that are saved in local state.
void ClearSavedStabilityMetrics();
// Pushes a log that has been generated by an external component.
void PushExternalLog(const std::string& log);
protected:
// Exposed for testing.
MetricsLogManager* log_manager() { return &log_manager_; }
......
......@@ -214,8 +214,7 @@ TEST_F(MetricsServiceTest, InitialStabilityLogAtProviderRequest) {
TestMetricsServiceClient client;
TestMetricsLog log("client", 1, &client, GetLocalState());
log.RecordEnvironment(std::vector<MetricsProvider*>(),
std::vector<variations::ActiveGroupId>(),
0);
std::vector<variations::ActiveGroupId>(), 0, 0);
// Record stability build time and version from previous session, so that
// stability metrics (including exited cleanly flag) won't be cleared.
......@@ -280,8 +279,7 @@ TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) {
TestMetricsServiceClient client;
TestMetricsLog log("client", 1, &client, GetLocalState());
log.RecordEnvironment(std::vector<MetricsProvider*>(),
std::vector<variations::ActiveGroupId>(),
0);
std::vector<variations::ActiveGroupId>(), 0, 0);
// Record stability build time and version from previous session, so that
// stability metrics (including exited cleanly flag) won't be cleared.
......
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