ukm_reporting_service.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // ReportingService specialized to report UKM metrics.
  5. #include "components/ukm/ukm_reporting_service.h"
  6. #include <memory>
  7. #include "base/command_line.h"
  8. #include "base/metrics/field_trial_params.h"
  9. #include "base/metrics/histogram_functions.h"
  10. #include "base/metrics/histogram_macros.h"
  11. #include "build/build_config.h"
  12. #include "components/metrics/metrics_service_client.h"
  13. #include "components/metrics/metrics_switches.h"
  14. #include "components/prefs/pref_registry_simple.h"
  15. #include "components/ukm/ukm_pref_names.h"
  16. #include "components/ukm/ukm_service.h"
  17. #include "components/ukm/unsent_log_store_metrics_impl.h"
  18. #include "third_party/zlib/google/compression_utils.h"
  19. #if BUILDFLAG(IS_IOS)
  20. #include "components/ukm/ios/ukm_reporting_ios_util.h"
  21. #endif
  22. namespace ukm {
  23. namespace {
  24. // The UKM server's URL.
  25. constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm";
  26. // The UKM server's MIME type.
  27. constexpr char kMimeType[] = "application/vnd.chrome.ukm";
  28. // The number of UKM logs that will be stored in UnsentLogStore before logs
  29. // start being dropped.
  30. constexpr int kMinUnsentLogCount = 8;
  31. // The number of bytes UKM logs that will be stored in UnsentLogStore before
  32. // logs start being dropped.
  33. // This ensures that a reasonable amount of history will be stored even if there
  34. // is a long series of very small logs.
  35. constexpr int kMinUnsentLogBytes = 300000;
  36. // If an upload fails, and the transmission was over this byte count, then we
  37. // will discard the log, and not try to retransmit it. We also don't persist
  38. // the log to the prefs for transmission during the next chrome session if this
  39. // limit is exceeded.
  40. constexpr size_t kMaxLogRetransmitSize = 100 * 1024;
  41. GURL GetServerUrl() {
  42. #ifndef NDEBUG
  43. // Only allow overriding the server URL through the command line in debug
  44. // builds. This is to prevent, for example, rerouting metrics due to malware.
  45. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  46. if (command_line->HasSwitch(metrics::switches::kUkmServerUrl)) {
  47. return GURL(
  48. command_line->GetSwitchValueASCII(metrics::switches::kUkmServerUrl));
  49. }
  50. #endif // NDEBUG
  51. std::string server_url =
  52. base::GetFieldTrialParamValueByFeature(kUkmFeature, "ServerUrl");
  53. if (!server_url.empty())
  54. return GURL(server_url);
  55. return GURL(kDefaultServerUrl);
  56. }
  57. } // namespace
  58. // static
  59. void UkmReportingService::RegisterPrefs(PrefRegistrySimple* registry) {
  60. registry->RegisterListPref(prefs::kUkmUnsentLogStore);
  61. // Base class already registered by MetricsReportingService::RegisterPrefs
  62. // ReportingService::RegisterPrefs(registry);
  63. }
  64. UkmReportingService::UkmReportingService(metrics::MetricsServiceClient* client,
  65. PrefService* local_state)
  66. : ReportingService(client, local_state, kMaxLogRetransmitSize),
  67. unsent_log_store_(std::make_unique<ukm::UnsentLogStoreMetricsImpl>(),
  68. local_state,
  69. prefs::kUkmUnsentLogStore,
  70. nullptr,
  71. kMinUnsentLogCount,
  72. kMinUnsentLogBytes,
  73. kMaxLogRetransmitSize,
  74. client->GetUploadSigningKey()) {}
  75. UkmReportingService::~UkmReportingService() {}
  76. metrics::LogStore* UkmReportingService::log_store() {
  77. return &unsent_log_store_;
  78. }
  79. GURL UkmReportingService::GetUploadUrl() const {
  80. return GetServerUrl();
  81. }
  82. GURL UkmReportingService::GetInsecureUploadUrl() const {
  83. return GURL();
  84. }
  85. base::StringPiece UkmReportingService::upload_mime_type() const {
  86. return kMimeType;
  87. }
  88. metrics::MetricsLogUploader::MetricServiceType
  89. UkmReportingService::service_type() const {
  90. return metrics::MetricsLogUploader::UKM;
  91. }
  92. void UkmReportingService::LogCellularConstraint(bool upload_canceled) {
  93. UMA_HISTOGRAM_BOOLEAN("UKM.LogUpload.Canceled.CellularConstraint",
  94. upload_canceled);
  95. }
  96. void UkmReportingService::LogResponseOrErrorCode(int response_code,
  97. int error_code,
  98. bool was_https) {
  99. // |was_https| is ignored since all UKM logs are received over HTTPS.
  100. base::UmaHistogramSparse("UKM.LogUpload.ResponseOrErrorCode",
  101. response_code >= 0 ? response_code : error_code);
  102. }
  103. void UkmReportingService::LogSuccessLogSize(size_t log_size) {
  104. #if BUILDFLAG(IS_IOS)
  105. IncrementUkmLogSizeOnSuccessCounter();
  106. #endif
  107. UMA_HISTOGRAM_COUNTS_10000("UKM.LogSize.OnSuccess", log_size / 1024);
  108. }
  109. void UkmReportingService::LogSuccessMetadata(const std::string& staged_log) {
  110. // Recover the report from the compressed staged log.
  111. // Note: We don't use metrics::DecodeLogDataToProto() since we to use
  112. // |uncompressed_log_data| later in the function.
  113. std::string uncompressed_log_data;
  114. bool uncompress_successful =
  115. compression::GzipUncompress(staged_log, &uncompressed_log_data);
  116. DCHECK(uncompress_successful);
  117. Report report;
  118. report.ParseFromString(uncompressed_log_data);
  119. // Log the relative size of the report with relevant UKM data omitted. This
  120. // helps us to estimate the bandwidth usage of logs upload that is not
  121. // directly attributed to UKM data, for example the system profile info.
  122. // Note that serialized logs are further compressed before upload, thus the
  123. // percentages here are not the exact percentage of bandwidth they ended up
  124. // taking.
  125. std::string log_without_ukm_data;
  126. report.clear_sources();
  127. report.clear_source_counts();
  128. report.clear_entries();
  129. report.clear_aggregates();
  130. report.SerializeToString(&log_without_ukm_data);
  131. int non_ukm_percentage =
  132. log_without_ukm_data.length() * 100 / uncompressed_log_data.length();
  133. DCHECK_GE(non_ukm_percentage, 0);
  134. DCHECK_LE(non_ukm_percentage, 100);
  135. base::UmaHistogramPercentage("UKM.ReportSize.NonUkmPercentage",
  136. non_ukm_percentage);
  137. }
  138. void UkmReportingService::LogLargeRejection(size_t log_size) {}
  139. } // namespace ukm