• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
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 COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_
6 #define COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_
7 
8 #include <memory>
9 #include <string>
10 
11 namespace metrics {
12 
13 // This class is used by libmetrics (ChromeOS) to serialize
14 // and deserialize measurements to send them to a metrics sending service.
15 // It is meant to be a simple container with serialization functions.
16 class MetricSample {
17  public:
18   // Types of metric sample used.
19   enum SampleType {
20     CRASH,
21     HISTOGRAM,
22     LINEAR_HISTOGRAM,
23     SPARSE_HISTOGRAM,
24     USER_ACTION
25   };
26 
27   // Use one of the static methods in this class instead of calling the
28   // constructor directly.
29   //
30   // The constructor is exposed for std::make_unique.
31   MetricSample(SampleType sample_type,
32                const std::string& metric_name,
33                const int sample,
34                const int min,
35                const int max,
36                const int bucket_count);
37 
38   MetricSample(const MetricSample&) = delete;
39   MetricSample& operator=(const MetricSample&) = delete;
40 
41   ~MetricSample();
42 
43   // Returns true if the sample is valid (can be serialized without ambiguity).
44   //
45   // This function should be used to filter bad samples before serializing them.
46   bool IsValid() const;
47 
48   // Getters for type and name. All types of metrics have these so we do not
49   // need to check the type.
type()50   SampleType type() const { return type_; }
name()51   const std::string& name() const { return name_; }
52 
53   // Getters for sample, min, max, bucket_count.
54   // Check the metric type to make sure the request make sense. (ex: a crash
55   // sample does not have a bucket_count so we crash if we call bucket_count()
56   // on it.)
57   int sample() const;
58   int min() const;
59   int max() const;
60   int bucket_count() const;
61 
62   // Returns a serialized version of the sample.
63   //
64   // The serialized message for each type is:
65   // crash: crash\0|name_|\0
66   // user action: useraction\0|name_|\0
67   // histogram: histogram\0|name_| |sample_| |min_| |max_| |bucket_count_|\0
68   // sparsehistogram: sparsehistogram\0|name_| |sample_|\0
69   // linearhistogram: linearhistogram\0|name_| |sample_| |max_|\0
70   std::string ToString() const;
71 
72   // Builds a crash sample.
73   static std::unique_ptr<MetricSample> CrashSample(
74       const std::string& crash_name);
75 
76   // Builds a histogram sample.
77   static std::unique_ptr<MetricSample> HistogramSample(
78       const std::string& histogram_name,
79       int sample,
80       int min,
81       int max,
82       int bucket_count);
83   // Deserializes a histogram sample.
84   static std::unique_ptr<MetricSample> ParseHistogram(
85       const std::string& serialized);
86 
87   // Builds a sparse histogram sample.
88   static std::unique_ptr<MetricSample> SparseHistogramSample(
89       const std::string& histogram_name,
90       int sample);
91   // Deserializes a sparse histogram sample.
92   static std::unique_ptr<MetricSample> ParseSparseHistogram(
93       const std::string& serialized);
94 
95   // Builds a linear histogram sample.
96   static std::unique_ptr<MetricSample>
97   LinearHistogramSample(const std::string& histogram_name, int sample, int max);
98   // Deserializes a linear histogram sample.
99   static std::unique_ptr<MetricSample> ParseLinearHistogram(
100       const std::string& serialized);
101 
102   // Builds a user action sample.
103   static std::unique_ptr<MetricSample> UserActionSample(
104       const std::string& action_name);
105 
106   // Returns true if sample and this object represent the same sample (type,
107   // name, sample, min, max, bucket_count match).
108   bool IsEqual(const MetricSample& sample);
109 
110  private:
111   const SampleType type_;
112   const std::string name_;
113   const int sample_;
114   const int min_;
115   const int max_;
116   const int bucket_count_;
117 };
118 
119 }  // namespace metrics
120 
121 #endif  // COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_
122