single_sample_metrics.h 3.8 KB

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