flags_ui_metrics.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2020 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. #include "components/flags_ui/flags_ui_metrics.h"
  5. #include <set>
  6. #include <string>
  7. #include "base/logging.h"
  8. #include "base/metrics/histogram_functions.h"
  9. #include "base/metrics/metrics_hashes.h"
  10. #include "base/notreached.h"
  11. #include "base/strings/string_util.h"
  12. namespace flags_ui {
  13. namespace {
  14. // Records a set of feature switches (prefixed with "--").
  15. void ReportAboutFlagsHistogramSwitches(const std::string& uma_histogram_name,
  16. const std::set<std::string>& switches) {
  17. for (const std::string& flag : switches) {
  18. int uma_id = testing::kBadSwitchFormatHistogramId;
  19. if (base::StartsWith(flag, "--", base::CompareCase::SENSITIVE)) {
  20. // Skip '--' before switch name.
  21. std::string switch_name(flag.substr(2));
  22. // Kill value, if any.
  23. const size_t value_pos = switch_name.find('=');
  24. if (value_pos != std::string::npos)
  25. switch_name.resize(value_pos);
  26. uma_id = GetSwitchUMAId(switch_name);
  27. } else {
  28. NOTREACHED() << "ReportAboutFlagsHistogram(): flag '" << flag
  29. << "' has incorrect format.";
  30. }
  31. DVLOG(1) << "ReportAboutFlagsHistogram(): histogram='" << uma_histogram_name
  32. << "' '" << flag << "', uma_id=" << uma_id;
  33. base::UmaHistogramSparse(uma_histogram_name, uma_id);
  34. }
  35. }
  36. // Records a set of FEATURE_VALUE_TYPE features (suffixed with ":enabled" or
  37. // "disabled", depending on their state).
  38. void ReportAboutFlagsHistogramFeatures(const std::string& uma_histogram_name,
  39. const std::set<std::string>& features) {
  40. for (const std::string& feature : features) {
  41. int uma_id = GetSwitchUMAId(feature);
  42. DVLOG(1) << "ReportAboutFlagsHistogram(): histogram='" << uma_histogram_name
  43. << "' '" << feature << "', uma_id=" << uma_id;
  44. base::UmaHistogramSparse(uma_histogram_name, uma_id);
  45. }
  46. }
  47. } // namespace
  48. base::HistogramBase::Sample GetSwitchUMAId(const std::string& switch_name) {
  49. return static_cast<base::HistogramBase::Sample>(
  50. base::HashMetricName(switch_name));
  51. }
  52. void ReportAboutFlagsHistogram(const std::string& uma_histogram_name,
  53. const std::set<std::string>& switches,
  54. const std::set<std::string>& features) {
  55. ReportAboutFlagsHistogramSwitches(uma_histogram_name, switches);
  56. ReportAboutFlagsHistogramFeatures(uma_histogram_name, features);
  57. }
  58. namespace testing {
  59. const base::HistogramBase::Sample kBadSwitchFormatHistogramId = 0;
  60. } // namespace testing
  61. } // namespace flags_ui