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

Unified Diff: base/metrics/single_sample_metrics.h

Issue 2687583002: Add support for single sample metrics. (Closed)
Patch Set: Add tests. Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: base/metrics/single_sample_metrics.h
diff --git a/base/metrics/single_sample_metrics.h b/base/metrics/single_sample_metrics.h
new file mode 100644
index 0000000000000000000000000000000000000000..eb1d89cc97c7438ce0c6c07130c6fd25a87ac92b
--- /dev/null
+++ b/base/metrics/single_sample_metrics.h
@@ -0,0 +1,100 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
+#define BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
+
+#include <string>
+
+#include "base/base_export.h"
+#include "base/macros.h"
+#include "base/metrics/histogram_base.h"
+
+namespace base {
+
+// See base/metrics/histograms.h for parameter definitions. Must only be used
+// and destroyed from the same thread as construction.
+class BASE_EXPORT SingleSampleMetric {
+ public:
+ virtual ~SingleSampleMetric() {}
+
+ virtual void SetSample(HistogramBase::Sample sample) = 0;
+};
+
+// Factory for creating single sample metrics. A single sample metric only
+// reports its sample once at destruction time. The sample may be changed prior
+// to destruction using the SetSample() method as many times as desired.
+//
+// The metric creation methods are safe to call from any thread, however the
+// returned class must only be used and destroyed from the same thread as
+// construction.
+//
+// |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.
+// usage recommendations and base/metrics/histogram.h for full parameter
+// definitions.
+class BASE_EXPORT SingleSampleMetricsFactory {
+ public:
+ virtual ~SingleSampleMetricsFactory() {}
+
+ // Returns the factory provided by SetFactory(), or if no factory has been set
+ // a default factory will be provided (future calls to SetFactory() will fail
+ // if the default factory is ever vended).
+ static SingleSampleMetricsFactory* Get();
+ static void SetFactory(SingleSampleMetricsFactory* factory);
+
+ // 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.
+ // should avoid leaking it since it sets a global.
+ static void DeleteFactoryForTesting();
+
+ // 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.
+ virtual std::unique_ptr<SingleSampleMetric> CreateCustomCountsMetric(
+ const std::string& name,
+ HistogramBase::Sample min,
+ HistogramBase::Sample max,
+ uint32_t bucket_count) = 0;
+};
+
+// 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.
+class BASE_EXPORT DefaultSingleSampleMetricsFactory
+ : public SingleSampleMetricsFactory {
+ public:
+ DefaultSingleSampleMetricsFactory() {}
+ ~DefaultSingleSampleMetricsFactory() override {}
+
+ // SingleSampleMetricsFactory implementation.
Alexei Svitkine (slow) 2017/05/03 19:59:46 Nit: Prefer "// SingleSampleMetricsFactory:" synta
DaleCurtis 2017/05/03 20:54:47 Done.
+ std::unique_ptr<SingleSampleMetric> CreateCustomCountsMetric(
+ const std::string& name,
+ HistogramBase::Sample min,
+ HistogramBase::Sample max,
+ uint32_t bucket_count) override;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(DefaultSingleSampleMetricsFactory);
+};
+
+class BASE_EXPORT DefaultSingleSampleMetric : public SingleSampleMetric {
+ public:
+ 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.
+ HistogramBase::Sample min,
+ HistogramBase::Sample max,
+ uint32_t bucket_count,
+ int32_t flags);
+ ~DefaultSingleSampleMetric() override;
+
+ // SingleSampleMetric implementation.
+ void SetSample(HistogramBase::Sample sample) override;
+
+ private:
+ HistogramBase* const histogram_;
+
+ // The last sample provided to SetSample(). We use -1 as a sentinel value to
+ // indicate no sample has been set.
+ HistogramBase::Sample sample_ = -1;
+
+ DISALLOW_COPY_AND_ASSIGN(DefaultSingleSampleMetric);
+};
+
+} // namespace base
+
+#endif // BASE_METRICS_SINGLE_SAMPLE_METRICS_H_

Powered by Google App Engine
This is Rietveld 408576698