1 // Copyright 2021 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_STRUCTURED_EVENT_H_ 6 #define COMPONENTS_METRICS_STRUCTURED_EVENT_H_ 7 8 #include <map> 9 #include <memory> 10 #include <string> 11 12 #include "base/time/time.h" 13 #include "base/values.h" 14 #include "components/metrics/structured/enums.h" 15 #include "third_party/abseil-cpp/absl/types/optional.h" 16 17 // Builder classes for sending events are generated in 18 // //components/metrics/structured/structured_events.h based on XML 19 // configuration. 20 21 namespace metrics::structured { 22 23 // Event to be built and sent by StructuredMetrics clients. Builder 24 // classes will be codegen'd from metrics definitions in structured.xml, but 25 // clients may choose to use this class directly to build Events. 26 class Event { 27 public: 28 // There should be a 1-1 mapping between MetricType and the mojom enums. 29 // 30 // TODO(jongahn): Move this into common enum file. 31 enum class MetricType { 32 kHmac = 0, 33 kLong = 1, 34 kInt = 2, 35 kDouble = 3, 36 kRawString = 4, 37 kBoolean = 5, 38 }; 39 40 // Holds the value and the type of the metric encoded. 41 struct MetricValue { 42 MetricValue() = default; 43 MetricValue(MetricType type, base::Value value); 44 45 MetricValue(MetricValue&& other); 46 MetricValue& operator=(MetricValue&& other); 47 48 bool operator==(const MetricValue& rhs) const; 49 ~MetricValue(); 50 51 MetricType type; 52 base::Value value; 53 }; 54 55 // Special metadata if event is a sequence project. 56 struct EventSequenceMetadata { 57 explicit EventSequenceMetadata(int reset_counter); 58 ~EventSequenceMetadata(); 59 60 EventSequenceMetadata(const EventSequenceMetadata& other); 61 EventSequenceMetadata& operator=(const EventSequenceMetadata& other); 62 63 // Reset counter used for sequencing events across resets. 64 int reset_counter; 65 66 // UUIDv4 generated for every event. This does not contain any timestamp 67 // information. 68 std::string event_unique_id; 69 }; 70 71 Event(); 72 Event(const std::string& project_name, const std::string& event_name); 73 Event(const std::string& project_name, 74 const std::string& event_name, 75 bool is_event_sequence); 76 77 Event(Event&& other); 78 Event& operator=(Event&& other); 79 80 virtual ~Event(); 81 82 // Whether |this| event part of a sequence. 83 bool IsEventSequenceType() const; 84 85 Event Clone() const; 86 87 // Records |this|. Once this method is called, |this| will be unsafe to 88 // access. 89 void Record(); 90 91 // Returns true if the value was added successfully. |type| and type of 92 // |value| must be consistent and will be enforced. If the data in |value| and 93 // |type| do match, then |value| will be moved into |this| when called. 94 bool AddMetric(const std::string& metric_name, 95 MetricType type, 96 base::Value&& value); 97 98 // Sets the metadata into |this|. If |IsEventSequenceType()| is false, then 99 // this will no-op. 100 void SetEventSequenceMetadata( 101 const EventSequenceMetadata& event_sequence_metadata); 102 103 // Explicitly set the system uptime. 104 void SetRecordedTimeSinceBoot(base::TimeDelta recorded_time_since_boot); 105 106 const std::string& project_name() const; 107 const std::string& event_name() const; 108 bool is_event_sequence() const; 109 const std::map<std::string, MetricValue>& metric_values() const; 110 111 const base::TimeDelta recorded_time_since_boot() const; 112 const EventSequenceMetadata& event_sequence_metadata() const; 113 114 private: 115 std::string project_name_; 116 std::string event_name_; 117 std::map<std::string, MetricValue> metric_values_; 118 119 // System uptime for which the event was recorded. 120 absl::optional<base::TimeDelta> recorded_time_since_boot_; 121 122 absl::optional<EventSequenceMetadata> event_sequence_metadata_; 123 124 // Returns true if part of a sequence. 125 bool is_event_sequence_ = false; 126 }; 127 128 } // namespace metrics::structured 129 130 #endif // COMPONENTS_METRICS_STRUCTURED_EVENT_H_ 131