1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
ash / metrics / deferred_metrics_reporter.h [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_METRICS_DEFERRED_METRICS_REPORTER_H_
#define ASH_METRICS_DEFERRED_METRICS_REPORTER_H_
#include <memory>
#include <string>
#include <vector>
namespace ash {
// DeferredMetricsReporter stores metrics and delays reporting them until
// `MarkReadyToReport` is called. After it becomes ready, `ReportOrSchedule`
// reports the metric immediately.
class DeferredMetricsReporter {
public:
// Represents a single metric.
class Metric {
public:
virtual ~Metric() = default;
// Implementers should report the metric with the given `prefix`.
virtual void Report(const std::string& prefix) = 0;
};
DeferredMetricsReporter();
DeferredMetricsReporter(const DeferredMetricsReporter& other) = delete;
DeferredMetricsReporter& operator=(const DeferredMetricsReporter& other) =
delete;
~DeferredMetricsReporter();
// Reports the metrics immediately if this reporter is already ready.
// Otherwise, buffers the metrics and it will be reported once it gets ready.
void ReportOrSchedule(std::unique_ptr<Metric> metric);
// Updates the prefix, which is used to report metrics.
void SetPrefix(std::string new_prefix);
// Marks this reporter as ready, and reports all metrics previously scheduled
// if any. Prior to this, `SetPrefix` must be called with non-empty string.
void MarkReadyToReport();
private:
std::string prefix_;
std::vector<std::unique_ptr<Metric>> scheduled_metrics_;
bool ready_ = false;
};
} // namespace ash
#endif // ASH_METRICS_DEFERRED_METRICS_REPORTER_H_