File: proto_fetcher_metrics.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 3,997 bytes parent folder | download | duplicates (6)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 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 COMPONENTS_SUPERVISED_USER_CORE_BROWSER_PROTO_FETCHER_METRICS_H_
#define COMPONENTS_SUPERVISED_USER_CORE_BROWSER_PROTO_FETCHER_METRICS_H_

#include <optional>
#include <string>
#include <string_view>

#include "base/timer/elapsed_timer.h"
#include "google_apis/gaia/google_service_auth_error.h"

namespace supervised_user {
struct FetcherConfig;
class ProtoFetcherStatus;

// Encapsulates metric functionalities for proto fetchers.
class ProtoFetcherMetrics {
 public:
  ProtoFetcherMetrics() = delete;
  static std::optional<ProtoFetcherMetrics> FromConfig(
      const FetcherConfig& config);

  // Records metrics for the proto fetcher based on its configuration.
  void RecordMetrics(const ProtoFetcherStatus& status) const;

 protected:
  // The type of metrics tracking expected from the configuration.
  enum class MetricsTrackingMode : int {
    kSingle = 0,
    kOverall = 1,
  };

  // The metric type that is being recorded.
  enum class Type : int {
    kStatus = 1,
    kLatency = 2,
    kHttpStatusOrNetError = 3,
    kRetryCount = 4,
    kAuthError = 5,
  };

  ProtoFetcherMetrics(std::string_view basename,
                      MetricsTrackingMode metrics_tracking_mode);

  // Returns fully-qualified name of histogram for specified metric_type.
  std::string GetFullHistogramName(Type metric_type) const;

  // Returns label associated to the tracking mode.
  std::string GetMetricsTrackingModeLabel() const;

  MetricsTrackingMode metrics_tracking_mode_;

 private:
  // Records the ProtoFetcherStatus count.
  void RecordStatus(const ProtoFetcherStatus& status) const;

  // Records latency from the instantiation of the class (up to 10s).
  void RecordLatency() const;

  // Records latency per ProtoFetcherStatus type.
  void RecordStatusLatency(const ProtoFetcherStatus& status) const;

  // Records errors from ProtoFetcherStatus.
  void RecordAuthError(const GoogleServiceAuthError& auth_error) const;
  void RecordHttpStatusOrNetError(const ProtoFetcherStatus& status) const;

  // Translates top-level metric type into a string. ::ToMetricEnumLabel
  // translates statuses for per-status latency tracking.
  std::string GetMetricKey(Type metric_type) const;

  // Returns fully-qualified name of histogram for specified metric_type with
  // per-status values.
  std::string GetFullHistogramName(Type metric_type,
                                   ProtoFetcherStatus status) const;

  // The returned value must match one of the labels in
  // chromium/src/tools/metrics/histograms/enums.xml://enum[@name='ProtoFetcherStatus'],
  // and should be reflected in tokens in histogram defined for this fetcher.
  // See example at
  // tools/metrics/histograms/metadata/signin/histograms.xml://histogram[@name='Signin.ListFamilyMembersRequest.{Status}.*']
  static std::string ToMetricEnumLabel(const ProtoFetcherStatus& status);

  // Note for this non-owning reference that the corresponding fetcher config
  // that owns this basename has static linkage.
  std::string_view basename_;
  base::ElapsedTimer elapsed_timer_;
};

// Encapsulates metrics functionality for cumulative latency and status tracking
// during the proto fetching lifecycle. This is only available for proto
// fetchers that support retrying policy.
class CumulativeProtoFetcherMetrics : public ProtoFetcherMetrics {
 public:
  CumulativeProtoFetcherMetrics() = delete;
  static std::optional<CumulativeProtoFetcherMetrics> FromConfig(
      const FetcherConfig& config);

  // Records the number of retries from transient errors (up to 100).
  void RecordRetryCount(int count) const;

 private:
  CumulativeProtoFetcherMetrics(std::string_view basename,
                                MetricsTrackingMode metrics_tracking_mode);
};

}  // namespace supervised_user

#endif  // COMPONENTS_SUPERVISED_USER_CORE_BROWSER_PROTO_FETCHER_METRICS_H_