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 const int num_samples); 38 39 MetricSample(const MetricSample&) = delete; 40 MetricSample& operator=(const MetricSample&) = delete; 41 42 ~MetricSample(); 43 44 // Returns true if the sample is valid (can be serialized without ambiguity). 45 // 46 // This function should be used to filter bad samples before serializing them. 47 bool IsValid() const; 48 49 // Getters for type, name, and num_samples. All types of metrics have these so 50 // we do not need to check the type. type()51 SampleType type() const { return type_; } name()52 const std::string& name() const { return name_; } num_samples()53 int num_samples() const { return num_samples_; } 54 55 // Getters for sample, min, max, bucket_count. 56 // Check the metric type to make sure the request make sense. (ex: a crash 57 // sample does not have a bucket_count so we crash if we call bucket_count() 58 // on it.) 59 int sample() const; 60 int min() const; 61 int max() const; 62 int bucket_count() const; 63 64 // Returns a serialized version of the sample. 65 // 66 // The serialized message for each type is: 67 // crash: crash\0|name_|\0 68 // user action: useraction\0|name_|\0 69 // histogram: histogram\0|name_| |sample_| |min_| |max_| |bucket_count_|\0 70 // sparsehistogram: sparsehistogram\0|name_| |sample_|\0 71 // linearhistogram: linearhistogram\0|name_| |sample_| |max_|\0 72 // 73 // Additionally, if num_samples is not 1, each type may have: 74 // ` |num_samples_|` immediately before the final null terminator. 75 std::string ToString() const; 76 77 // Builds a crash sample. 78 static std::unique_ptr<MetricSample> CrashSample( 79 const std::string& crash_name, 80 int num_samples); 81 // Deserializes a crash sample. 82 static std::unique_ptr<MetricSample> ParseCrash( 83 const std::string& serialized); 84 85 // Builds a histogram sample. 86 static std::unique_ptr<MetricSample> HistogramSample( 87 const std::string& histogram_name, 88 int sample, 89 int min, 90 int max, 91 int bucket_count, 92 int num_samples); 93 // Deserializes a histogram sample. 94 static std::unique_ptr<MetricSample> ParseHistogram( 95 const std::string& serialized); 96 97 // Builds a sparse histogram sample. 98 static std::unique_ptr<MetricSample> SparseHistogramSample( 99 const std::string& histogram_name, 100 int sample, 101 int num_samples); 102 // Deserializes a sparse histogram sample. 103 static std::unique_ptr<MetricSample> ParseSparseHistogram( 104 const std::string& serialized); 105 106 // Builds a linear histogram sample. 107 static std::unique_ptr<MetricSample> LinearHistogramSample( 108 const std::string& histogram_name, 109 int sample, 110 int max, 111 int num_samples); 112 // Deserializes a linear histogram sample. 113 static std::unique_ptr<MetricSample> ParseLinearHistogram( 114 const std::string& serialized); 115 116 // Builds a user action sample. 117 static std::unique_ptr<MetricSample> UserActionSample( 118 const std::string& action_name, 119 int num_samples); 120 // Deserializes a user action sample. 121 static std::unique_ptr<MetricSample> ParseUserAction( 122 const std::string& serialized); 123 124 // Returns true if sample and this object represent the same sample (type, 125 // name, sample, min, max, bucket_count match). 126 bool IsEqual(const MetricSample& sample); 127 128 private: 129 const SampleType type_; 130 const std::string name_; 131 const int sample_; 132 const int min_; 133 const int max_; 134 const int bucket_count_; 135 const int num_samples_; 136 }; 137 138 } // namespace metrics 139 140 #endif // COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_ 141