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