1 // Copyright 2014 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.
5 #ifndef COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_
6 #define COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
16 // This class is used by libmetrics (ChromeOS) to serialize
17 // and deserialize measurements to send them to a metrics sending service.
18 // It is meant to be a simple container with serialization functions.
21 // Types of metric sample used.
32 // Returns true if the sample is valid (can be serialized without ambiguity).
34 // This function should be used to filter bad samples before serializing them.
37 // Getters for type and name. All types of metrics have these so we do not
38 // need to check the type.
39 SampleType
type() const { return type_
; }
40 const std::string
& name() const { return name_
; }
42 // Getters for sample, min, max, bucket_count.
43 // Check the metric type to make sure the request make sense. (ex: a crash
44 // sample does not have a bucket_count so we crash if we call bucket_count()
46 const int sample() const;
47 const int min() const;
48 const int max() const;
49 const int bucket_count() const;
51 // Returns a serialized version of the sample.
53 // The serialized message for each type is:
54 // crash: crash\0|name_|\0
55 // user action: useraction\0|name_|\0
56 // histogram: histogram\0|name_| |sample_| |min_| |max_| |bucket_count_|\0
57 // sparsehistogram: sparsehistogram\0|name_| |sample_|\0
58 // linearhistogram: linearhistogram\0|name_| |sample_| |max_|\0
59 std::string
ToString() const;
61 // Builds a crash sample.
62 static scoped_ptr
<MetricSample
> CrashSample(const std::string
& crash_name
);
64 // Builds a histogram sample.
65 static scoped_ptr
<MetricSample
> HistogramSample(
66 const std::string
& histogram_name
,
71 // Deserializes a histogram sample.
72 static scoped_ptr
<MetricSample
> ParseHistogram(const std::string
& serialized
);
74 // Builds a sparse histogram sample.
75 static scoped_ptr
<MetricSample
> SparseHistogramSample(
76 const std::string
& histogram_name
,
78 // Deserializes a sparse histogram sample.
79 static scoped_ptr
<MetricSample
> ParseSparseHistogram(
80 const std::string
& serialized
);
82 // Builds a linear histogram sample.
83 static scoped_ptr
<MetricSample
> LinearHistogramSample(
84 const std::string
& histogram_name
,
87 // Deserializes a linear histogram sample.
88 static scoped_ptr
<MetricSample
> ParseLinearHistogram(
89 const std::string
& serialized
);
91 // Builds a user action sample.
92 static scoped_ptr
<MetricSample
> UserActionSample(
93 const std::string
& action_name
);
95 // Returns true if sample and this object represent the same sample (type,
96 // name, sample, min, max, bucket_count match).
97 bool IsEqual(const MetricSample
& sample
);
100 MetricSample(SampleType sample_type
,
101 const std::string
& metric_name
,
105 const int bucket_count
);
107 const SampleType type_
;
108 const std::string name_
;
112 const int bucket_count_
;
114 DISALLOW_COPY_AND_ASSIGN(MetricSample
);
117 } // namespace metrics
119 #endif // COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_