single_sample_metrics.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
  5. #define BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/base_export.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/metrics/histogram_base.h"
  11. namespace base {
  12. // See base/metrics/histograms.h for parameter definitions. Must only be used
  13. // and destroyed from the same thread as construction.
  14. class BASE_EXPORT SingleSampleMetric {
  15. public:
  16. virtual ~SingleSampleMetric() = default;
  17. virtual void SetSample(HistogramBase::Sample sample) = 0;
  18. };
  19. // Factory for creating single sample metrics. A single sample metric only
  20. // reports its sample once at destruction time. The sample may be changed prior
  21. // to destruction using the SetSample() method as many times as desired.
  22. //
  23. // The metric creation methods are safe to call from any thread, however the
  24. // returned class must only be used and destroyed from the same thread as
  25. // construction.
  26. //
  27. // See base/metrics/histogram_macros.h for usage recommendations and
  28. // base/metrics/histogram.h for full parameter definitions.
  29. class BASE_EXPORT SingleSampleMetricsFactory {
  30. public:
  31. virtual ~SingleSampleMetricsFactory() = default;
  32. // Returns the factory provided by SetFactory(), or if no factory has been set
  33. // a default factory will be provided (future calls to SetFactory() will fail
  34. // if the default factory is ever vended).
  35. static SingleSampleMetricsFactory* Get();
  36. static void SetFactory(std::unique_ptr<SingleSampleMetricsFactory> factory);
  37. // The factory normally persists until process shutdown, but in testing we
  38. // should avoid leaking it since it sets a global.
  39. static void DeleteFactoryForTesting();
  40. // The methods below return a single sample metric for counts histograms; see
  41. // method comments for the corresponding histogram macro.
  42. // UMA_HISTOGRAM_CUSTOM_COUNTS()
  43. virtual std::unique_ptr<SingleSampleMetric> CreateCustomCountsMetric(
  44. const std::string& histogram_name,
  45. HistogramBase::Sample min,
  46. HistogramBase::Sample max,
  47. uint32_t bucket_count) = 0;
  48. };
  49. // Default implementation for when no factory has been provided to the process.
  50. // Samples are only recorded within the current process in this case, so samples
  51. // will be lost in the event of sudden process termination.
  52. class BASE_EXPORT DefaultSingleSampleMetricsFactory
  53. : public SingleSampleMetricsFactory {
  54. public:
  55. DefaultSingleSampleMetricsFactory() = default;
  56. DefaultSingleSampleMetricsFactory(const DefaultSingleSampleMetricsFactory&) =
  57. delete;
  58. DefaultSingleSampleMetricsFactory& operator=(
  59. const DefaultSingleSampleMetricsFactory&) = delete;
  60. ~DefaultSingleSampleMetricsFactory() override = default;
  61. // SingleSampleMetricsFactory:
  62. std::unique_ptr<SingleSampleMetric> CreateCustomCountsMetric(
  63. const std::string& histogram_name,
  64. HistogramBase::Sample min,
  65. HistogramBase::Sample max,
  66. uint32_t bucket_count) override;
  67. };
  68. class BASE_EXPORT DefaultSingleSampleMetric : public SingleSampleMetric {
  69. public:
  70. DefaultSingleSampleMetric(const std::string& histogram_name,
  71. HistogramBase::Sample min,
  72. HistogramBase::Sample max,
  73. uint32_t bucket_count,
  74. int32_t flags);
  75. DefaultSingleSampleMetric(const DefaultSingleSampleMetric&) = delete;
  76. DefaultSingleSampleMetric& operator=(const DefaultSingleSampleMetric&) =
  77. delete;
  78. ~DefaultSingleSampleMetric() override;
  79. // SingleSampleMetric:
  80. void SetSample(HistogramBase::Sample sample) override;
  81. private:
  82. const raw_ptr<HistogramBase> histogram_;
  83. // The last sample provided to SetSample(). We use -1 as a sentinel value to
  84. // indicate no sample has been set.
  85. HistogramBase::Sample sample_ = -1;
  86. };
  87. } // namespace base
  88. #endif // BASE_METRICS_SINGLE_SAMPLE_METRICS_H_