Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: base/metrics/single_sample_metrics.h

Issue 2687583002: Add support for single sample metrics. (Closed)
Patch Set: Add tests. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #ifndef BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
6 #define BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
7
8 #include <string>
9
10 #include "base/base_export.h"
11 #include "base/macros.h"
12 #include "base/metrics/histogram_base.h"
13
14 namespace base {
15
16 // See base/metrics/histograms.h for parameter definitions. Must only be used
17 // and destroyed from the same thread as construction.
18 class BASE_EXPORT SingleSampleMetric {
19 public:
20 virtual ~SingleSampleMetric() {}
21
22 virtual void SetSample(HistogramBase::Sample sample) = 0;
23 };
24
25 // Factory for creating single sample metrics. A single sample metric only
26 // reports its sample once at destruction time. The sample may be changed prior
27 // to destruction using the SetSample() method as many times as desired.
28 //
29 // The metric creation methods are safe to call from any thread, however the
30 // returned class must only be used and destroyed from the same thread as
31 // construction.
32 //
33 // |name| is the name of the histogram. See base/metrics/histogram_macros.h for
Alexei Svitkine (slow) 2017/05/03 19:59:46 Nit: I'd actually just name the param |histogram_n
DaleCurtis 2017/05/03 20:54:47 Done.
34 // usage recommendations and base/metrics/histogram.h for full parameter
35 // definitions.
36 class BASE_EXPORT SingleSampleMetricsFactory {
37 public:
38 virtual ~SingleSampleMetricsFactory() {}
39
40 // Returns the factory provided by SetFactory(), or if no factory has been set
41 // a default factory will be provided (future calls to SetFactory() will fail
42 // if the default factory is ever vended).
43 static SingleSampleMetricsFactory* Get();
44 static void SetFactory(SingleSampleMetricsFactory* factory);
45
46 // The factory normally persist until process shutdown, but in testing we
Alexei Svitkine (slow) 2017/05/03 19:59:46 Nit: persist -> persists
DaleCurtis 2017/05/03 20:54:47 Done.
47 // should avoid leaking it since it sets a global.
48 static void DeleteFactoryForTesting();
49
50 // The methods below return a single sample metric for counts histograms.
Alexei Svitkine (slow) 2017/05/03 19:59:46 Nit: Mention corresponding macro this is a parrale
DaleCurtis 2017/05/03 20:54:47 Done.
51 virtual std::unique_ptr<SingleSampleMetric> CreateCustomCountsMetric(
52 const std::string& name,
53 HistogramBase::Sample min,
54 HistogramBase::Sample max,
55 uint32_t bucket_count) = 0;
56 };
57
58 // Default implementation for when no factory has been provided to the process.
Alexei Svitkine (slow) 2017/05/03 19:59:46 Nit: Expand comment to mention data is logged loca
DaleCurtis 2017/05/03 20:54:47 Done.
59 class BASE_EXPORT DefaultSingleSampleMetricsFactory
60 : public SingleSampleMetricsFactory {
61 public:
62 DefaultSingleSampleMetricsFactory() {}
63 ~DefaultSingleSampleMetricsFactory() override {}
64
65 // SingleSampleMetricsFactory implementation.
Alexei Svitkine (slow) 2017/05/03 19:59:46 Nit: Prefer "// SingleSampleMetricsFactory:" synta
DaleCurtis 2017/05/03 20:54:47 Done.
66 std::unique_ptr<SingleSampleMetric> CreateCustomCountsMetric(
67 const std::string& name,
68 HistogramBase::Sample min,
69 HistogramBase::Sample max,
70 uint32_t bucket_count) override;
71
72 private:
73 DISALLOW_COPY_AND_ASSIGN(DefaultSingleSampleMetricsFactory);
74 };
75
76 class BASE_EXPORT DefaultSingleSampleMetric : public SingleSampleMetric {
77 public:
78 DefaultSingleSampleMetric(const std::string& name,
Alexei Svitkine (slow) 2017/05/03 19:59:46 Nit: histogram_name
DaleCurtis 2017/05/03 20:54:47 Done.
79 HistogramBase::Sample min,
80 HistogramBase::Sample max,
81 uint32_t bucket_count,
82 int32_t flags);
83 ~DefaultSingleSampleMetric() override;
84
85 // SingleSampleMetric implementation.
86 void SetSample(HistogramBase::Sample sample) override;
87
88 private:
89 HistogramBase* const histogram_;
90
91 // The last sample provided to SetSample(). We use -1 as a sentinel value to
92 // indicate no sample has been set.
93 HistogramBase::Sample sample_ = -1;
94
95 DISALLOW_COPY_AND_ASSIGN(DefaultSingleSampleMetric);
96 };
97
98 } // namespace base
99
100 #endif // BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698