xref: /aosp_15_r20/external/cronet/components/metrics/structured/event.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <optional>
11 #include <string>
12 
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "components/metrics/structured/enums.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   // kInt is used to represent enums.
31   //
32   // TODO(jongahn): Move this into common enum file.
33   enum class MetricType {
34     kHmac = 0,
35     kLong = 1,
36     kInt = 2,
37     kDouble = 3,
38     kRawString = 4,
39     kBoolean = 5,
40   };
41 
42   // Holds the value and the type of the metric encoded.
43   struct MetricValue {
44     MetricValue() = default;
45     MetricValue(MetricType type, base::Value value);
46 
47     MetricValue(MetricValue&& other);
48     MetricValue& operator=(MetricValue&& other);
49 
50     bool operator==(const MetricValue& rhs) const;
51     ~MetricValue();
52 
53     MetricType type;
54     base::Value value;
55   };
56 
57   // Special metadata if event is a sequence project.
58   struct EventSequenceMetadata {
59     explicit EventSequenceMetadata(int reset_counter);
60     ~EventSequenceMetadata();
61 
62     EventSequenceMetadata(const EventSequenceMetadata& other);
63     EventSequenceMetadata& operator=(const EventSequenceMetadata& other);
64 
65     // Reset counter used for sequencing events across resets.
66     int reset_counter;
67 
68     // UUIDv4 generated for every event. This does not contain any timestamp
69     // information.
70     std::string event_unique_id;
71   };
72 
73   Event();
74   Event(const std::string& project_name, const std::string& event_name);
75   Event(const std::string& project_name,
76         const std::string& event_name,
77         bool is_event_sequence);
78 
79   Event(Event&& other);
80   Event& operator=(Event&& other);
81 
82   virtual ~Event();
83 
84   // Whether |this| event part of a sequence.
85   bool IsEventSequenceType() const;
86 
87   Event Clone() const;
88 
89   // Returns true if the value was added successfully. |type| and type of
90   // |value| must be consistent and will be enforced. If the data in |value| and
91   // |type| do match, then |value| will be moved into |this| when called.
92   bool AddMetric(const std::string& metric_name,
93                  MetricType type,
94                  base::Value&& value);
95 
96   // Sets the metadata into |this|. If |IsEventSequenceType()| is false, then
97   // this will no-op.
98   void SetEventSequenceMetadata(
99       const EventSequenceMetadata& event_sequence_metadata);
100 
101   // Explicitly set the system uptime.
102   void SetRecordedTimeSinceBoot(base::TimeDelta recorded_time_since_boot);
103 
project_name()104   const std::string& project_name() const { return project_name_; }
event_name()105   const std::string& event_name() const { return event_name_; }
is_event_sequence()106   bool is_event_sequence() const { return is_event_sequence_; }
metric_values()107   const std::map<std::string, MetricValue>& metric_values() const {
108     return metric_values_;
109   }
has_system_uptime()110   bool has_system_uptime() const {
111     return recorded_time_since_boot_.has_value();
112   }
113   const base::TimeDelta recorded_time_since_boot() const;
114   const EventSequenceMetadata& event_sequence_metadata() const;
115 
116  private:
117   std::string project_name_;
118   std::string event_name_;
119   std::map<std::string, MetricValue> metric_values_;
120 
121   // System uptime for which the event was recorded.
122   std::optional<base::TimeDelta> recorded_time_since_boot_;
123 
124   std::optional<EventSequenceMetadata> event_sequence_metadata_;
125 
126   // Returns true if part of a sequence.
127   bool is_event_sequence_ = false;
128 };
129 
130 }  // namespace metrics::structured
131 
132 #endif  // COMPONENTS_METRICS_STRUCTURED_EVENT_H_
133