File: external_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 (97 lines) | stat: -rw-r--r-- 3,517 bytes parent folder | download | duplicates (9)
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
// Copyright 2021 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_METRICS_STRUCTURED_EXTERNAL_METRICS_H_
#define COMPONENTS_METRICS_STRUCTURED_EXTERNAL_METRICS_H_

#include "base/containers/flat_set.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"

namespace metrics::structured {

class EventsProto;
class ExternalMetricsTest;

// ExternalMetrics reads structured metrics saved by ChromeOS and uploads them
// to the UMA server on its behalf. This is structured metrics' equivalent of
// `ash::ExternalMetrics`.
//
// When a call to CollectEvents() is done, events in that directory will be
// added to StructuredMetricsRecorder. However, if the user has disabled upload,
// then all events will be dropped and deleted.
//
// Chrome periodically reads a directory of protos and adds their content into
// the StructuredMetricProvider's regular metrics upload. After reading each
// file, it is deleted. Chrome and ChromeOS use flock to prevent concurrent
// read/writes.
class ExternalMetrics {
 public:
  using MetricsCollectedCallback =
      base::RepeatingCallback<void(const EventsProto&)>;

  ExternalMetrics(const base::FilePath& events_directory,
                  const base::TimeDelta& collection_interval,
                  MetricsCollectedCallback callback);
  ~ExternalMetrics();
  ExternalMetrics(const ExternalMetrics&) = delete;
  ExternalMetrics& operator=(const ExternalMetrics&) = delete;

  // Adds a project to the disallowed list for testing.
  void AddDisallowedProjectForTest(uint64_t project_name_hash);

  void EnableRecording();
  void DisableRecording();

 private:
  friend class ExternalMetricsTest;

  void ScheduleCollector();
  void CollectEventsAndReschedule();

  // Events are collected from |events_directory|. Each file is processed by:
  //
  //    1. Stat on a file to see if it contains any events or if
  //       something in ChromeOS has already deleted the file.
  //    2. Obtains a flock.
  //    3. File is processed and read into memory.
  //    4. File is deleted.
  //    5. Flock is released.
  //
  // The above is done to avoid reading files mid-write from ChromeOS and having
  // ChromeOS write files after they were marked for deletion by Chrome.
  //
  // Files will be ignored if there is failure to obtain the flock or get the
  // file descriptor.
  //
  // Files will be dropped when an event file exceeds a fixed threshold provided
  // by GetFileSizeByteLimit(). Files will also be dropped if there are too many
  // event files existing on the current iteration provided by
  // GetFileLimitPerScan().
  void CollectEvents();

  // Builds a cache of disallow projects from the Finch controlled variable.
  void CacheDisallowedProjectsSet();

  bool recording_enabled_ = false;

  const base::FilePath events_directory_;
  const base::TimeDelta collection_interval_;
  MetricsCollectedCallback callback_;

  // A set of projects that are not allowed to be recorded. This is a cache of
  // GetDisabledProjects().
  base::flat_set<uint64_t> disallowed_projects_;

  scoped_refptr<base::SequencedTaskRunner> task_runner_;
  base::WeakPtrFactory<ExternalMetrics> weak_factory_{this};
};

}  // namespace metrics::structured

#endif  // COMPONENTS_METRICS_STRUCTURED_EXTERNAL_METRICS_H_