blob: 82b42d32f837d41ffc2f79f935c88653086ee5d9 [file] [log] [blame]
Luc Nguyen8901ecf2022-09-23 23:47:271// Copyright 2022 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_METRICS_LOGS_EVENT_MANAGER_H_
6#define COMPONENTS_METRICS_METRICS_LOGS_EVENT_MANAGER_H_
7
Arthur Sonzognic571efb2024-01-26 20:26:188#include <optional>
Helmut Januschkaff1c3ec2024-04-25 19:38:329#include <string_view>
Arthur Sonzognic571efb2024-01-26 20:26:1810
Keishi Hattoric1b00232022-11-22 09:04:2611#include "base/memory/raw_ptr.h"
Luc Nguyen8901ecf2022-09-23 23:47:2712#include "base/observer_list.h"
Luc Nguyena47bc692022-11-01 22:57:3613#include "components/metrics/metrics_log.h"
Luc Nguyen8901ecf2022-09-23 23:47:2714
15namespace metrics {
Luc Nguyen3e8432e2022-10-06 21:23:4416
Alison Gale45c4058f2024-04-26 19:30:0217// TODO(crbug.com/40238818): Add unit tests for the various calls to the notify
Luc Nguyen3e8432e2022-10-06 21:23:4418// functions in ReportingService and UnsentLogStore.
Luc Nguyen8901ecf2022-09-23 23:47:2719class MetricsLogsEventManager {
20 public:
Luc Nguyen3e8432e2022-10-06 21:23:4421 enum class LogEvent {
Luc Nguyend53ac1d2023-01-30 17:47:4322 // The log was staged (queued to be uploaded).
Luc Nguyen8901ecf2022-09-23 23:47:2723 kLogStaged,
24 // The log was discarded.
25 kLogDiscarded,
26 // The log was trimmed.
27 kLogTrimmed,
Luc Nguyend53ac1d2023-01-30 17:47:4328 // The log has been sent out and is currently being uploaded.
Luc Nguyen8901ecf2022-09-23 23:47:2729 kLogUploading,
30 // The log was successfully uploaded.
31 kLogUploaded,
Luc Nguyend53ac1d2023-01-30 17:47:4332 // The log was created.
33 kLogCreated,
34 };
35
Hira Mahmooda5a694b82025-02-18 16:59:5936 // These values are persisted to logs. Entries should not be renumbered and
37 // numeric values should never be reused.
38 // LINT.IfChange(CreateReason)
Luc Nguyend53ac1d2023-01-30 17:47:4339 enum class CreateReason {
Hira Mahmooda5a694b82025-02-18 16:59:5940 kUnknown = 0,
Luc Nguyend53ac1d2023-01-30 17:47:4341 // The log is a periodic log, which are created at regular intervals.
Hira Mahmooda5a694b82025-02-18 16:59:5942 kPeriodic = 1,
Luc Nguyend53ac1d2023-01-30 17:47:4343 // The log was created due to the UMA/UKM service shutting down.
Hira Mahmooda5a694b82025-02-18 16:59:5944 kServiceShutdown = 2,
Luc Nguyend53ac1d2023-01-30 17:47:4345 // The log was loaded from a previous session.
Hira Mahmooda5a694b82025-02-18 16:59:5946 kLoadFromPreviousSession = 3,
Luc Nguyend53ac1d2023-01-30 17:47:4347 // The log was created due to the browser being backgrounded.
Hira Mahmooda5a694b82025-02-18 16:59:5948 kBackgrounded = 4,
Luc Nguyend53ac1d2023-01-30 17:47:4349 // The log was created due to the browser being foregrounded.
Hira Mahmooda5a694b82025-02-18 16:59:5950 kForegrounded = 5,
Luc Nguyend53ac1d2023-01-30 17:47:4351 // The log was created due to a new alternate ongoing log store being set.
Hira Mahmooda5a694b82025-02-18 16:59:5952 kAlternateOngoingLogStoreSet = 6,
Luc Nguyend53ac1d2023-01-30 17:47:4353 // The log was created due to the alternate ongoing log store being unset.
Hira Mahmooda5a694b82025-02-18 16:59:5954 kAlternateOngoingLogStoreUnset = 7,
Luc Nguyend53ac1d2023-01-30 17:47:4355 // The log was created due to the previous session having stability metrics
56 // to report.
Hira Mahmooda5a694b82025-02-18 16:59:5957 kStability = 8,
Luc Nguyend53ac1d2023-01-30 17:47:4358 // The log was fully created and provided by a metrics provider.
Hira Mahmooda5a694b82025-02-18 16:59:5959 kIndependent = 9,
60 // The log was created due to a manual upload from the client.
61 kOutOfBand = 10,
62 kMaxValue = kOutOfBand,
Luc Nguyen8901ecf2022-09-23 23:47:2763 };
Hira Mahmooda5a694b82025-02-18 16:59:5964 // LINT.ThenChange(/tools/metrics/histograms/metadata/uma/enums.xml:MetricsLogCreateReason)
Luc Nguyen8901ecf2022-09-23 23:47:2765
66 class Observer : public base::CheckedObserver {
67 public:
Helmut Januschkaff1c3ec2024-04-25 19:38:3268 virtual void OnLogCreated(std::string_view log_hash,
69 std::string_view log_data,
70 std::string_view log_timestamp,
Luc Nguyend53ac1d2023-01-30 17:47:4371 CreateReason reason) = 0;
Luc Nguyen3e8432e2022-10-06 21:23:4472 virtual void OnLogEvent(MetricsLogsEventManager::LogEvent event,
Helmut Januschkaff1c3ec2024-04-25 19:38:3273 std::string_view log_hash,
74 std::string_view message) = 0;
Arthur Sonzognic571efb2024-01-26 20:26:1875 virtual void OnLogType(std::optional<MetricsLog::LogType> log_type) {}
Luc Nguyen8901ecf2022-09-23 23:47:2776
77 protected:
78 Observer() = default;
79 ~Observer() override = default;
80 };
81
Luc Nguyena47bc692022-11-01 22:57:3682 // Helper class used to indicate that UMA logs created while an instance of
83 // this class is in scope are of a certain type. Only one instance of this
84 // class should exist at a time.
85 class ScopedNotifyLogType {
86 public:
87 ScopedNotifyLogType(MetricsLogsEventManager* logs_event_manager,
88 MetricsLog::LogType log_type);
89
90 ScopedNotifyLogType(const ScopedNotifyLogType& other) = delete;
91 ScopedNotifyLogType& operator=(const ScopedNotifyLogType& other) = delete;
92
93 ~ScopedNotifyLogType();
94
95 private:
Keishi Hattoric1b00232022-11-22 09:04:2696 const raw_ptr<MetricsLogsEventManager> logs_event_manager_;
Luc Nguyena47bc692022-11-01 22:57:3697
98 // Used to ensure that only one instance of this class exists at a time.
99 static bool instance_exists_;
100 };
101
Luc Nguyen8901ecf2022-09-23 23:47:27102 MetricsLogsEventManager();
103
104 MetricsLogsEventManager(const MetricsLogsEventManager&) = delete;
105 MetricsLogsEventManager& operator=(const MetricsLogsEventManager&) = delete;
106
107 ~MetricsLogsEventManager();
108
109 void AddObserver(Observer* observer);
110 void RemoveObserver(Observer* observer);
111
112 // Notifies observers that a log was newly created and is now known by the
113 // metrics service. This may occur when closing a log, or when loading a log
114 // from persistent storage. |log_hash| is the SHA1 hash of the log data, used
115 // to uniquely identify the log. This hash may be re-used to notify that an
116 // event occurred on the log (e.g., the log was trimmed, uploaded, etc.). See
Luc Nguyen3e8432e2022-10-06 21:23:44117 // NotifyLogEvent(). |log_data| is the compressed serialized log protobuf
Luc Nguyen8901ecf2022-09-23 23:47:27118 // (see UnsentLogStore::LogInfo for more details on the compression).
119 // |log_timestamp| is the time at which the log was closed.
Helmut Januschkaff1c3ec2024-04-25 19:38:32120 void NotifyLogCreated(std::string_view log_hash,
121 std::string_view log_data,
122 std::string_view log_timestamp,
Luc Nguyend53ac1d2023-01-30 17:47:43123 CreateReason reason);
Luc Nguyen8901ecf2022-09-23 23:47:27124
Luc Nguyen3e8432e2022-10-06 21:23:44125 // Notifies observers that an event |event| occurred on the log associated
126 // with |log_hash|. Optionally, a |message| can be associated with the event.
127 // In particular, for |kLogDiscarded|, |message| is the reason the log was
128 // discarded (e.g., log is ill-formed). For |kLogTrimmed|, |message| is the
129 // reason why the log was trimmed (e.g., log is too large).
130 void NotifyLogEvent(LogEvent event,
Helmut Januschkaff1c3ec2024-04-25 19:38:32131 std::string_view log_hash,
132 std::string_view message = "");
Luc Nguyen8901ecf2022-09-23 23:47:27133
Luc Nguyena47bc692022-11-01 22:57:36134 // Notifies observers that logs that are created after this function is called
135 // are of the type |log_type|. This should only be used in UMA. This info is
136 // not passed through NotifyLogCreated() because the concept of a log type
137 // only exists in UMA, and this class is intended to be re-used across
138 // different metrics collection services (e.g., UKM).
139 // Note: Typically, this should not be called directly. Consider using
140 // ScopedNotifyLogType.
Arthur Sonzognic571efb2024-01-26 20:26:18141 void NotifyLogType(std::optional<MetricsLog::LogType> log_type);
Luc Nguyena47bc692022-11-01 22:57:36142
Luc Nguyen8901ecf2022-09-23 23:47:27143 private:
144 base::ObserverList<Observer> observers_;
145};
146
147} // namespace metrics
148
shaochenguang61f8f1d82023-11-29 23:57:17149#endif // COMPONENTS_METRICS_METRICS_LOGS_EVENT_MANAGER_H_