OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef METRICS_LIBRARY_H_ | 5 #ifndef METRICS_LIBRARY_H_ |
6 #define METRICS_LIBRARY_H_ | 6 #define METRICS_LIBRARY_H_ |
7 | 7 |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include <gtest/gtest_prod.h> // for FRIEND_TEST | 11 #include <gtest/gtest_prod.h> // for FRIEND_TEST |
12 | 12 |
13 class MetricsLibraryInterface { | 13 class MetricsLibraryInterface { |
14 public: | 14 public: |
15 virtual void Init() = 0; | 15 virtual void Init() = 0; |
16 virtual bool SendToUMA(const std::string& name, int sample, | 16 virtual bool SendToUMA(const std::string& name, int sample, |
17 int min, int max, int nbuckets) = 0; | 17 int min, int max, int nbuckets) = 0; |
18 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0; | 18 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0; |
19 virtual ~MetricsLibraryInterface() {} | 19 virtual ~MetricsLibraryInterface() {} |
20 }; | 20 }; |
21 | 21 |
22 // Library used to send metrics to both Autotest and Chrome/UMA. | 22 // Library used to send metrics to both Autotest and Chrome/UMA. |
23 class MetricsLibrary : public MetricsLibraryInterface { | 23 class MetricsLibrary : public MetricsLibraryInterface { |
24 public: | 24 public: |
25 MetricsLibrary(); | 25 MetricsLibrary(); |
26 | 26 |
27 // Initializes the library. | 27 // Initializes the library. |
28 void Init(); | 28 void Init(); |
29 | 29 |
| 30 // Returns whether or not metrics collection is enabled. |
| 31 bool AreMetricsEnabled(); |
| 32 |
30 // Sends histogram data to Chrome for transport to UMA and returns | 33 // Sends histogram data to Chrome for transport to UMA and returns |
31 // true on success. This method results in the equivalent of an | 34 // true on success. This method results in the equivalent of an |
32 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS | 35 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS |
33 // inside Chrome (see base/histogram.h). | 36 // inside Chrome (see base/histogram.h). |
34 // | 37 // |
35 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|). | 38 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|). |
36 // |min| is the minimum value of the histogram samples (|min| > 0). | 39 // |min| is the minimum value of the histogram samples (|min| > 0). |
37 // |max| is the maximum value of the histogram samples. | 40 // |max| is the maximum value of the histogram samples. |
38 // |nbuckets| is the number of histogram buckets. | 41 // |nbuckets| is the number of histogram buckets. |
39 // [0,min) is the implicit underflow bucket. | 42 // [0,min) is the implicit underflow bucket. |
(...skipping 22 matching lines...) Expand all Loading... |
62 // is strongly recommended to keep this number low (e.g., 50 is | 65 // is strongly recommended to keep this number low (e.g., 50 is |
63 // normal, while 100 is high). | 66 // normal, while 100 is high). |
64 bool SendEnumToUMA(const std::string& name, int sample, int max); | 67 bool SendEnumToUMA(const std::string& name, int sample, int max); |
65 | 68 |
66 // Sends to Autotest and returns true on success. | 69 // Sends to Autotest and returns true on success. |
67 static bool SendToAutotest(const std::string& name, int value); | 70 static bool SendToAutotest(const std::string& name, int value); |
68 | 71 |
69 private: | 72 private: |
70 friend class CMetricsLibraryTest; | 73 friend class CMetricsLibraryTest; |
71 friend class MetricsLibraryTest; | 74 friend class MetricsLibraryTest; |
| 75 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled); |
72 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage); | 76 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage); |
73 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong); | 77 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong); |
74 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome); | 78 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome); |
75 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation); | 79 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation); |
76 | 80 |
77 // Sends message of size |length| to Chrome for transport to UMA and | 81 // Sends message of size |length| to Chrome for transport to UMA and |
78 // returns true on success. | 82 // returns true on success. |
79 bool SendMessageToChrome(int32_t length, const char* message); | 83 bool SendMessageToChrome(int32_t length, const char* message); |
80 | 84 |
81 // Formats a name/value message for Chrome in |buffer| and returns the | 85 // Formats a name/value message for Chrome in |buffer| and returns the |
82 // length of the message or a negative value on error. | 86 // length of the message or a negative value on error. |
83 // | 87 // |
84 // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 | | 88 // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 | |
85 // | 89 // |
86 // The arbitrary |format| argument covers the non-LENGTH portion of the | 90 // The arbitrary |format| argument covers the non-LENGTH portion of the |
87 // message. The caller is responsible to store the \0 character | 91 // message. The caller is responsible to store the \0 character |
88 // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value). | 92 // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value). |
89 int32_t FormatChromeMessage(int32_t buffer_size, char* buffer, | 93 int32_t FormatChromeMessage(int32_t buffer_size, char* buffer, |
90 const char* format, ...); | 94 const char* format, ...); |
91 | 95 |
| 96 // Time at which we last checked if metrics were enabled. |
| 97 static time_t cached_enabled_time_; |
| 98 |
| 99 // Cached state of whether or not metrics were enabled. |
| 100 static bool cached_enabled_; |
| 101 |
92 const char* uma_events_file_; | 102 const char* uma_events_file_; |
| 103 const char* consent_file_; |
93 }; | 104 }; |
94 | 105 |
95 #endif // METRICS_LIBRARY_H_ | 106 #endif // METRICS_LIBRARY_H_ |
OLD | NEW |