summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--metrics/metrics_daemon.cc10
-rw-r--r--metrics/metrics_daemon.h6
-rw-r--r--metrics/metrics_daemon_main.cc5
-rw-r--r--metrics/metrics_daemon_test.cc3
-rw-r--r--metrics/uploader/system_profile_cache.cc53
-rw-r--r--metrics/uploader/system_profile_cache.h13
-rw-r--r--metrics/uploader/upload_service.cc5
-rw-r--r--metrics/uploader/upload_service.h3
-rw-r--r--metrics/uploader/upload_service_test.cc9
9 files changed, 84 insertions, 23 deletions
diff --git a/metrics/metrics_daemon.cc b/metrics/metrics_daemon.cc
index bdd181c641..b0247c692c 100644
--- a/metrics/metrics_daemon.cc
+++ b/metrics/metrics_daemon.cc
@@ -187,7 +187,9 @@ void MetricsDaemon::Run(bool run_as_daemon) {
}
void MetricsDaemon::RunUploaderTest() {
- upload_service_.reset(new UploadService(testing_, server_));
+ upload_service_.reset(new UploadService(new SystemProfileCache(true,
+ config_root_),
+ server_));
upload_service_->Init(upload_interval_secs_, metrics_file_);
upload_service_->UploadEvent();
}
@@ -218,8 +220,10 @@ void MetricsDaemon::Init(bool testing,
const string& cpuinfo_max_freq_path,
int upload_interval_secs,
const string& server,
- const string& metrics_file) {
+ const string& metrics_file,
+ const string& config_root) {
testing_ = testing;
+ config_root_ = config_root;
DCHECK(metrics_lib != nullptr);
metrics_lib_ = metrics_lib;
@@ -321,7 +325,7 @@ void MetricsDaemon::Init(bool testing,
if (uploader_active) {
LOG(INFO) << "uploader enabled";
- upload_service_.reset(new UploadService(testing_, server_));
+ upload_service_.reset(new UploadService(new SystemProfileCache(), server_));
upload_service_->Init(upload_interval_secs_, metrics_file_);
}
}
diff --git a/metrics/metrics_daemon.h b/metrics/metrics_daemon.h
index 07072f9b4c..7c7f8d2f02 100644
--- a/metrics/metrics_daemon.h
+++ b/metrics/metrics_daemon.h
@@ -39,7 +39,8 @@ class MetricsDaemon {
const std::string& scaling_max_freq_path,
int upload_interval_secs,
const std::string& server,
- const std::string& metrics_file);
+ const std::string& metrics_file,
+ const std::string& config_root);
// Does all the work. If |run_as_daemon| is true, daemonizes by
// forking.
@@ -300,6 +301,9 @@ class MetricsDaemon {
// Test mode.
bool testing_;
+ // Root of the configuration files to use.
+ std::string config_root_;
+
// The metrics library handle.
MetricsLibraryInterface* metrics_lib_;
diff --git a/metrics/metrics_daemon_main.cc b/metrics/metrics_daemon_main.cc
index e92ce31e29..f4a660b35b 100644
--- a/metrics/metrics_daemon_main.cc
+++ b/metrics/metrics_daemon_main.cc
@@ -65,6 +65,8 @@ int main(int argc, char** argv) {
DEFINE_string(metrics_file,
"/var/run/metrics/uma-events",
"File to use as a proxy for uploading the metrics");
+ DEFINE_string(config_root,
+ "/", "Root of the configuration files (testing only)");
chromeos::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon");
@@ -83,7 +85,8 @@ int main(int argc, char** argv) {
kCpuinfoMaxFreqPath,
FLAGS_upload_interval_secs,
FLAGS_server,
- FLAGS_metrics_file);
+ FLAGS_metrics_file,
+ FLAGS_config_root);
base::AtExitManager at_exit_manager;
diff --git a/metrics/metrics_daemon_test.cc b/metrics/metrics_daemon_test.cc
index 78559df676..947be87735 100644
--- a/metrics/metrics_daemon_test.cc
+++ b/metrics/metrics_daemon_test.cc
@@ -74,7 +74,8 @@ class MetricsDaemonTest : public testing::Test {
kFakeCpuinfoMaxFreqPath,
1800,
kMetricsServer,
- kMetricsFilePath);
+ kMetricsFilePath,
+ "/");
// Replace original persistent values with mock ones.
daily_active_use_mock_ =
diff --git a/metrics/uploader/system_profile_cache.cc b/metrics/uploader/system_profile_cache.cc
index 604c060cad..b08fbd915b 100644
--- a/metrics/uploader/system_profile_cache.cc
+++ b/metrics/uploader/system_profile_cache.cc
@@ -11,20 +11,35 @@
#include "base/files/file_util.h"
#include "base/guid.h"
#include "base/logging.h"
+#include "base/strings/string_number_conversions.h"
#include "base/sys_info.h"
#include "components/metrics/metrics_log_base.h"
#include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
#include "metrics/persistent_integer.h"
#include "vboot/crossystem.h"
-const char* SystemProfileCache::kPersistentGUIDFile =
- "/var/lib/metrics/Sysinfo.GUID";
-const char* SystemProfileCache::kPersistentSessionIdFilename =
- "Sysinfo.SessionId";
+namespace {
-SystemProfileCache::SystemProfileCache(bool testing)
+const char kPersistentGUIDFile[] = "/var/lib/metrics/Sysinfo.GUID";
+const char kPersistentSessionIdFilename[] = "Sysinfo.SessionId";
+const char kProductIdFieldName[] = "GOOGLE_METRICS_PRODUCT_ID";
+
+} // namespace
+
+
+SystemProfileCache::SystemProfileCache()
+ : initialized_(false),
+ testing_(false),
+ config_root_("/"),
+ session_id_(new chromeos_metrics::PersistentInteger(
+ kPersistentSessionIdFilename)) {
+}
+
+SystemProfileCache::SystemProfileCache(bool testing,
+ const std::string& config_root)
: initialized_(false),
testing_(testing),
+ config_root_(config_root),
session_id_(new chromeos_metrics::PersistentInteger(
kPersistentSessionIdFilename)) {
}
@@ -48,6 +63,12 @@ bool SystemProfileCache::Initialize() {
base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_TRACK", &channel_string);
profile_.channel = ProtoChannelFromString(channel_string);
+ // If the product id is not defined, use the default one from the protobuf.
+ profile_.product_id = metrics::ChromeUserMetricsExtension::CHROME;
+ if (GetProductId(&profile_.product_id)) {
+ DLOG(INFO) << "Set the product id to " << profile_.product_id;
+ }
+
profile_.client_id =
testing_ ? "client_id_test" : GetPersistentGUID(kPersistentGUIDFile);
@@ -78,6 +99,9 @@ void SystemProfileCache::Populate(
metrics::MetricsLogBase::Hash(profile_.client_id));
metrics_proto->set_session_id(profile_.session_id);
+ // Sets the product id.
+ metrics_proto->set_product(profile_.product_id);
+
metrics::SystemProfileProto* profile_proto =
metrics_proto->mutable_system_profile();
profile_proto->mutable_hardware()->set_hardware_class(
@@ -122,6 +146,25 @@ bool SystemProfileCache::GetHardwareId(std::string* hwid) {
return true;
}
+bool SystemProfileCache::GetProductId(int* product_id) const {
+ chromeos::OsReleaseReader reader;
+ if (testing_) {
+ base::FilePath root(config_root_);
+ CHECK(reader.LoadTestingOnly(root)) << "Failed to load os-release fields "
+ "from" << root.value();
+ } else {
+ CHECK(reader.Load()) << "Failed to load os-release fields.";
+ }
+
+ std::string id;
+ if (reader.GetString(kProductIdFieldName, &id)) {
+ CHECK(base::StringToInt(id, product_id)) << "Failed to convert product_id "
+ << id << " to int.";
+ return true;
+ }
+ return false;
+}
+
metrics::SystemProfileProto_Channel SystemProfileCache::ProtoChannelFromString(
const std::string& channel) {
diff --git a/metrics/uploader/system_profile_cache.h b/metrics/uploader/system_profile_cache.h
index f85c1b190f..95f554f8ed 100644
--- a/metrics/uploader/system_profile_cache.h
+++ b/metrics/uploader/system_profile_cache.h
@@ -12,6 +12,7 @@
#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
+#include "chromeos/osrelease_reader.h"
#include "components/metrics/proto/system_profile.pb.h"
#include "metrics/persistent_integer.h"
#include "metrics/uploader/system_profile_setter.h"
@@ -28,6 +29,7 @@ struct SystemProfile {
std::string hardware_class;
std::string client_id;
int32_t session_id;
+ int32_t product_id;
};
// Retrieves general system informations needed by the protobuf for context and
@@ -36,7 +38,9 @@ struct SystemProfile {
// The cache is populated lazily. The only method needed is Populate.
class SystemProfileCache : public SystemProfileSetter {
public:
- explicit SystemProfileCache(bool testing);
+ SystemProfileCache();
+
+ SystemProfileCache(bool testing, const std::string& config_root);
// Populates the ProfileSystem protobuf with system information.
void Populate(metrics::ChromeUserMetricsExtension* profile_proto) override;
@@ -56,9 +60,6 @@ class SystemProfileCache : public SystemProfileSetter {
FRIEND_TEST(UploadServiceTest, SessionIdIncrementedAtInitialization);
FRIEND_TEST(UploadServiceTest, ValuesInConfigFileAreSent);
- static const char* kPersistentGUIDFile;
- static const char* kPersistentSessionIdFilename;
-
// Fetches all informations and populates |profile_|
bool Initialize();
@@ -68,8 +69,12 @@ class SystemProfileCache : public SystemProfileSetter {
// Gets the hardware ID using crossystem
bool GetHardwareId(std::string* hwid);
+ // Gets the product ID from the GOOGLE_METRICS_PRODUCT_ID field.
+ bool GetProductId(int* product_id) const;
+
bool initialized_;
bool testing_;
+ std::string config_root_;
scoped_ptr<chromeos_metrics::PersistentInteger> session_id_;
SystemProfile profile_;
};
diff --git a/metrics/uploader/upload_service.cc b/metrics/uploader/upload_service.cc
index 0ebfc7816a..55d76a406c 100644
--- a/metrics/uploader/upload_service.cc
+++ b/metrics/uploader/upload_service.cc
@@ -24,8 +24,9 @@
const int UploadService::kMaxFailedUpload = 10;
-UploadService::UploadService(bool testing, const std::string& server)
- : system_profile_setter_(new SystemProfileCache(testing)),
+UploadService::UploadService(SystemProfileSetter* setter,
+ const std::string& server)
+ : system_profile_setter_(setter),
histogram_snapshot_manager_(this),
sender_(new HttpSender(server)) {
}
diff --git a/metrics/uploader/upload_service.h b/metrics/uploader/upload_service.h
index c0a1430892..a72349e2f4 100644
--- a/metrics/uploader/upload_service.h
+++ b/metrics/uploader/upload_service.h
@@ -54,7 +54,8 @@ class SystemProfileSetter;
//
class UploadService : public base::HistogramFlattener {
public:
- explicit UploadService(bool testing, const std::string& server);
+ explicit UploadService(SystemProfileSetter* setter,
+ const std::string& server);
void Init(int upload_interval_secs,
const std::string& metrics_file);
diff --git a/metrics/uploader/upload_service_test.cc b/metrics/uploader/upload_service_test.cc
index 9badf64016..2cdc4f5e93 100644
--- a/metrics/uploader/upload_service_test.cc
+++ b/metrics/uploader/upload_service_test.cc
@@ -25,12 +25,11 @@ static const char kMetricsFilePath[] = "/var/run/metrics/uma-events";
class UploadServiceTest : public testing::Test {
protected:
UploadServiceTest()
- : cache_(true),
- upload_service_(true, kMetricsServer),
+ : cache_(true, "/"),
+ upload_service_(new MockSystemProfileSetter(), kMetricsServer),
exit_manager_(new base::AtExitManager()) {
sender_ = new SenderMock;
upload_service_.sender_.reset(sender_);
- upload_service_.system_profile_setter_.reset(new MockSystemProfileSetter());
upload_service_.Init(1800, kMetricsFilePath);
}
@@ -127,7 +126,7 @@ TEST_F(UploadServiceTest, EmptyLogsAreNotSent) {
}
TEST_F(UploadServiceTest, LogEmptyByDefault) {
- UploadService upload_service(true, kMetricsServer);
+ UploadService upload_service(new MockSystemProfileSetter(), kMetricsServer);
// current_log_ should be initialized later as it needs AtExitManager to exit
// in order to gather system information from SysInfo.
@@ -195,7 +194,7 @@ TEST_F(UploadServiceTest, ValuesInConfigFileAreSent) {
base::SysInfo::SetChromeOSVersionInfoForTest(content, base::Time());
scoped_ptr<metrics::MetricSample> histogram =
metrics::MetricSample::SparseHistogramSample("myhistogram", 1);
- SystemProfileCache* local_cache_ = new SystemProfileCache(true);
+ SystemProfileCache* local_cache_ = new SystemProfileCache(true, "/");
local_cache_->session_id_.reset(new chromeos_metrics::PersistentInteger(
dir_.path().Append("session_id").value()));