blob: d7f0285bf7491d45b48408d94e36ea6e9cbb5ba8 [file] [log] [blame]
[email protected]29cf16772010-04-21 15:13:471// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
[email protected]5ccaa412009-11-13 22:00:163// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
6#define CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
7
[email protected]f4124992013-06-20 03:34:178#include <string>
9
[email protected]5ccaa412009-11-13 22:00:1610#include "base/compiler_specific.h"
[email protected]3c1ec412010-06-02 15:22:5611#include "base/gtest_prod_util.h"
avi8a07d53892015-12-24 22:13:5312#include "base/macros.h"
[email protected]c6944272012-01-06 22:12:2813#include "base/memory/ref_counted.h"
Daniel Erata3d4e9c32018-09-05 20:36:1314#include "base/time/time.h"
[email protected]5ccaa412009-11-13 22:00:1615
[email protected]d5985de2014-05-28 15:24:4816namespace metrics {
17class MetricSample;
18} // namespace metrics
19
[email protected]5ccaa412009-11-13 22:00:1620namespace chromeos {
21
22// ExternalMetrics is a service that Chrome offers to Chrome OS to upload
23// metrics to the UMA server on its behalf. Chrome periodically reads the
24// content of a well-know file, and parses it into name-value pairs, each
25// representing a Chrome OS metrics event. The events are logged using the
26// normal UMA mechanism. The file is then truncated to zero size. Chrome uses
27// flock() to synchronize accesses to the file.
28class ExternalMetrics : public base::RefCountedThreadSafe<ExternalMetrics> {
[email protected]5ccaa412009-11-13 22:00:1629 public:
[email protected]196ff472011-01-21 19:25:2730 ExternalMetrics();
[email protected]5ccaa412009-11-13 22:00:1631
[email protected]29cf16772010-04-21 15:13:4732 // Begins the external data collection. This service is started and stopped
33 // by the chrome metrics service. Calls to RecordAction originate in the
hashimotod562a722015-04-10 13:44:2734 // blocking pool but are executed in the UI thread.
[email protected]29cf16772010-04-21 15:13:4735 void Start();
[email protected]5ccaa412009-11-13 22:00:1636
[email protected]d5985de2014-05-28 15:24:4837 // Creates an ExternalMetrics instance reading from |filename| for testing
38 // purpose.
39 static scoped_refptr<ExternalMetrics> CreateForTesting(
40 const std::string& filename);
41
[email protected]5ccaa412009-11-13 22:00:1642 private:
[email protected]9faa02dd2013-07-03 04:34:1343 friend class base::RefCountedThreadSafe<ExternalMetrics>;
[email protected]d5985de2014-05-28 15:24:4844 friend class ExternalMetricsTest;
[email protected]9faa02dd2013-07-03 04:34:1345
Daniel Erata3d4e9c32018-09-05 20:36:1346 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, CustomInterval);
[email protected]d5985de2014-05-28 15:24:4847 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, HandleMissingFile);
Daniel Erata3d4e9c32018-09-05 20:36:1348 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, CanReceiveHistogram);
[email protected]d5985de2014-05-28 15:24:4849 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest,
50 IncorrectHistogramsAreDiscarded);
[email protected]5ccaa412009-11-13 22:00:1651
52 // The max length of a message (name-value pair, plus header)
[email protected]29cf16772010-04-21 15:13:4753 static const int kMetricsMessageMaxLength = 1024; // be generous
[email protected]5ccaa412009-11-13 22:00:1654
[email protected]41baad02011-05-15 20:37:4655 ~ExternalMetrics();
[email protected]5ccaa412009-11-13 22:00:1656
[email protected]29cf16772010-04-21 15:13:4757 // Passes an action event to the UMA service on the UI thread.
ishermande9efab2015-03-31 00:01:2558 void RecordActionUI(const std::string& action_string);
[email protected]5ccaa412009-11-13 22:00:1659
[email protected]29cf16772010-04-21 15:13:4760 // Passes an action event to the UMA service.
[email protected]d5985de2014-05-28 15:24:4861 void RecordAction(const std::string& action_name);
[email protected]29cf16772010-04-21 15:13:4762
[email protected]c1834a92011-01-21 18:21:0363 // Records an external crash of the given string description to
64 // UMA service on the UI thread.
65 void RecordCrashUI(const std::string& crash_kind);
66
67 // Records an external crash of the given string description.
68 void RecordCrash(const std::string& crash_kind);
69
[email protected]d5985de2014-05-28 15:24:4870 // Records an histogram. |sample| is expected to be an histogram.
71 void RecordHistogram(const metrics::MetricSample& sample);
[email protected]29cf16772010-04-21 15:13:4772
[email protected]d5985de2014-05-28 15:24:4873 // Records a sparse histogram. |sample| is expected to be a sparse histogram.
74 void RecordSparseHistogram(const metrics::MetricSample& sample);
[email protected]5ccaa412009-11-13 22:00:1675
[email protected]d5985de2014-05-28 15:24:4876 // Records a linear histogram. |sample| is expected to be a linear histogram.
77 void RecordLinearHistogram(const metrics::MetricSample& sample);
[email protected]85bbf092013-05-07 08:23:4078
[email protected]5ccaa412009-11-13 22:00:1679 // Collects external events from metrics log file. This is run at periodic
80 // intervals.
[email protected]d5985de2014-05-28 15:24:4881 //
82 // Returns the number of events collected.
83 int CollectEvents();
[email protected]5ccaa412009-11-13 22:00:1684
85 // Calls CollectEvents and reschedules a future collection.
86 void CollectEventsAndReschedule();
87
88 // Schedules a metrics event collection in the future.
89 void ScheduleCollector();
90
[email protected]d5985de2014-05-28 15:24:4891 // File used by libmetrics to send metrics to Chrome.
92 std::string uma_events_file_;
[email protected]9faa02dd2013-07-03 04:34:1393
Daniel Erata3d4e9c32018-09-05 20:36:1394 // Interval between metrics being read from |uma_events_file_|.
95 base::TimeDelta collection_interval_;
96
[email protected]5ccaa412009-11-13 22:00:1697 DISALLOW_COPY_AND_ASSIGN(ExternalMetrics);
98};
99
100} // namespace chromeos
101
102#endif // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_