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

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

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 #include "base/metrics/single_sample_metrics.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "base/metrics/histogram.h"
9
10 namespace base {
11
12 static SingleSampleMetricsFactory* g_factory = nullptr;
13
14 // static
15 SingleSampleMetricsFactory* SingleSampleMetricsFactory::Get() {
16 if (!g_factory)
17 g_factory = new DefaultSingleSampleMetricsFactory();
18
19 return g_factory;
20 }
21
22 // static
23 void SingleSampleMetricsFactory::SetFactory(
24 SingleSampleMetricsFactory* factory) {
25 DCHECK(!g_factory);
26 g_factory = factory;
27 }
28
29 // static
30 void SingleSampleMetricsFactory::DeleteFactoryForTesting() {
31 DCHECK(g_factory);
32 delete g_factory;
33 g_factory = nullptr;
34 }
35
36 std::unique_ptr<SingleSampleMetric>
37 DefaultSingleSampleMetricsFactory::CreateCustomCountsMetric(
38 const std::string& name,
39 HistogramBase::Sample min,
40 HistogramBase::Sample max,
41 uint32_t bucket_count) {
42 return MakeUnique<DefaultSingleSampleMetric>(
43 name, min, max, bucket_count, HistogramBase::kUmaTargetedHistogramFlag);
44 }
45
46 DefaultSingleSampleMetric::DefaultSingleSampleMetric(const std::string& name,
47 HistogramBase::Sample min,
48 HistogramBase::Sample max,
49 uint32_t bucket_count,
50 int32_t flags)
51 : histogram_(Histogram::FactoryGet(name, min, max, bucket_count, flags)) {
52 DCHECK(histogram_);
53 }
54
55 DefaultSingleSampleMetric::~DefaultSingleSampleMetric() {
56 if (sample_ < 0)
57 return;
58 histogram_->Add(sample_);
59 }
60
61 void DefaultSingleSampleMetric::SetSample(HistogramBase::Sample sample) {
62 DCHECK_GE(sample, 0);
63 sample_ = sample;
64 }
65
66 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698