mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 1 | # Histogram Guidelines |
| 2 | |
| 3 | This document gives the best practices on how to use histograms in code and how |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 4 | to document the histograms for the dashboards. There are three general types |
Darwin Huang | 1ca97ac | 2020-06-17 18:09:20 | [diff] [blame] | 5 | of histograms: [enumerated histograms](#Enum-Histograms), |
| 6 | [count histograms](#Count-Histograms) (for arbitrary numbers), and |
| 7 | [sparse histograms](#When-To-Use-Sparse-Histograms) (for anything when the |
| 8 | precision is important over a wide range and/or the range is not possible to |
| 9 | specify a priori). |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 10 | |
| 11 | [TOC] |
| 12 | |
Ilya Sherman | b964189 | 2020-11-06 00:53:55 | [diff] [blame] | 13 | ## Defining Useful Metrics |
Mark Pearson | b1d608d | 2018-06-05 19:59:44 | [diff] [blame] | 14 | |
Ilya Sherman | b964189 | 2020-11-06 00:53:55 | [diff] [blame] | 15 | ### Directly Measure What You Want |
| 16 | |
| 17 | Measure exactly what you want, whether that's the time used for a function call, |
| 18 | the number of bytes transmitted to fetch a page, the number of items in a list, |
| 19 | etc. Do not assume you can calculate what you want from other histograms, as |
| 20 | most ways of doing this are incorrect. |
| 21 | |
| 22 | For example, suppose you want to measure the runtime of a function that just |
| 23 | calls two subfunctions, each of which is instrumented with histogram logging. |
| 24 | You might assume that you can simply sum the histograms for those two functions |
| 25 | to get the total time, but that results in misleading data. If we knew which |
| 26 | emissions came from which calls, we could pair them up and derive the total time |
| 27 | for the function. However, histograms are pre-aggregated client-side, which |
| 28 | means that there's no way to recover which emissions should be paired up. If you |
| 29 | simply add up the two histograms to get a total duration histogram, you're |
| 30 | implicitly assuming the two histograms' values are independent, which may not be |
| 31 | the case. |
| 32 | |
| 33 | Directly measure what you care about; don't try to derive it from other data. |
| 34 | |
| 35 | ### Provide Context |
| 36 | |
| 37 | When defining a new metric, think ahead about how you will analyze the |
| 38 | data. Often, this will require providing context in order for the data to be |
| 39 | interpretable. |
| 40 | |
| 41 | For enumerated histograms in particular, that often means including a bucket |
| 42 | that can be used as a baseline for understanding the data recorded to other |
| 43 | buckets: see the [enumerated histogram section](#Enum-Histograms). |
| 44 | |
| 45 | ### Naming Your Histogram |
| 46 | |
| 47 | Histograms are taxonomized into categories, using dot (`.`) characters as |
| 48 | separators. Thus, histogram names should be in the form Category.Name or |
| 49 | Category.Subcategory.Name, etc., where each category organizes related |
| 50 | histograms. |
| 51 | |
| 52 | It should be quite rare to introduce new top-level categories into the existing |
| 53 | taxonomy. If you're tempted to do so, please look through the existing |
Robert Kaplow | cbc6fd6 | 2021-03-19 15:11:40 | [diff] [blame] | 54 | categories to see whether any matches the metric(s) that you are adding. To |
| 55 | create a new category, the CL must be reviewed by |
| 56 | [email protected]. |
Mark Pearson | b1d608d | 2018-06-05 19:59:44 | [diff] [blame] | 57 | |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 58 | ## Coding (Emitting to Histograms) |
| 59 | |
Daniel Cheng | 01cd7593 | 2020-02-06 16:43:45 | [diff] [blame] | 60 | Prefer the helper functions defined in |
Mark Pearson | ed73f1f | 2019-03-22 18:00:12 | [diff] [blame] | 61 | [histogram_functions.h](https://cs.chromium.org/chromium/src/base/metrics/histogram_functions.h). |
Daniel Cheng | 01cd7593 | 2020-02-06 16:43:45 | [diff] [blame] | 62 | These functions take a lock and perform a map lookup, but the overhead is |
| 63 | generally insignificant. However, when recording metrics on the critical path |
| 64 | (e.g. called in a loop or logged multiple times per second), use the macros in |
| 65 | [histogram_macros.h](https://cs.chromium.org/chromium/src/base/metrics/histogram_macros.h) |
| 66 | instead. These macros cache a pointer to the histogram object for efficiency, |
| 67 | though this comes at the cost of increased binary size: 130 bytes/macro usage |
| 68 | sounds small but quickly adds up. |
Mark Pearson | 159c3897 | 2018-06-05 19:44:08 | [diff] [blame] | 69 | |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 70 | ### Don't Use the Same Histogram Logging Call in Multiple Places |
| 71 | |
| 72 | These logging macros and functions have long names and sometimes include extra |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 73 | parameters (defining the number of buckets for example). Use a helper function |
| 74 | if possible. This leads to shorter, more readable code that's also more |
| 75 | resilient to problems that could be introduced when making changes. (One could, |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 76 | for example, erroneously change the bucketing of the histogram in one call but |
| 77 | not the other.) |
| 78 | |
| 79 | ### Use Fixed Strings When Using Histogram Macros |
| 80 | |
| 81 | When using histogram macros (calls such as `UMA_HISTOGRAM_ENUMERATION`), you're |
Victor-Gabriel Savu | b2afb6f4 | 2019-10-23 07:28:23 | [diff] [blame] | 82 | not allowed to construct your string dynamically so that it can vary at a |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 83 | callsite. At a given callsite (preferably you have only one), the string |
| 84 | should be the same every time the macro is called. If you need to use dynamic |
Mark Pearson | 74c5321 | 2019-03-08 00:34:08 | [diff] [blame] | 85 | names, use the functions in histogram_functions.h instead of the macros. |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 86 | |
Arthur Milchior | a70d5e6 | 2022-08-02 05:10:56 | [diff] [blame] | 87 | ### Don't Use Same Inline String in Multiple Places |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 88 | |
| 89 | If you must use the histogram name in multiple places, use a compile-time |
| 90 | constant of appropriate scope that can be referenced everywhere. Using inline |
| 91 | strings in multiple places can lead to errors if you ever need to revise the |
Jana Grill | 8110372 | 2023-01-19 16:31:53 | [diff] [blame] | 92 | name and you update one location and forget another. |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 93 | |
| 94 | ### Efficiency |
| 95 | |
Mark Pearson | ed73f1f | 2019-03-22 18:00:12 | [diff] [blame] | 96 | Generally, don't be concerned about the processing cost of emitting to a |
| 97 | histogram (unless you're using [sparse |
| 98 | histograms](#When-To-Use-Sparse-Histograms)). The normal histogram code is |
| 99 | highly optimized. If you are recording to a histogram in particularly |
| 100 | performance-sensitive or "hot" code, make sure you're using the histogram |
| 101 | macros; see [reasons above](#Coding-Emitting-to-Histograms). |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 102 | |
| 103 | ## Picking Your Histogram Type |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 104 | |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 105 | ### Enum Histograms |
| 106 | |
| 107 | Enumerated histogram are most appropriate when you have a list of connected / |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 108 | related states that should be analyzed jointly. For example, the set of actions |
| 109 | that can be done on the New Tab Page (use the omnibox, click a most visited |
| 110 | tile, click a bookmark, etc.) would make a good enumerated histogram. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 111 | If the total count of your histogram (i.e. the sum across all buckets) is |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 112 | something meaningful—as it is in this example—that is generally a good sign. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 113 | However, the total count does not have to be meaningful for an enum histogram |
| 114 | to still be the right choice. |
| 115 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 116 | Enumerated histograms are also appropriate for counting events. Use a simple |
Ilya Sherman | b964189 | 2020-11-06 00:53:55 | [diff] [blame] | 117 | boolean histogram. It's usually best if you have a comparison point in the same |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 118 | histogram. For example, if you want to count pages opened from the history page, |
| 119 | it might be a useful comparison to have the same histogram record the number of |
| 120 | times the history page was opened. |
Mark Pearson | a768d022 | 2019-03-20 02:16:00 | [diff] [blame] | 121 | |
Ilya Sherman | b964189 | 2020-11-06 00:53:55 | [diff] [blame] | 122 | In rarer cases, it's okay if you only log to one bucket (say, `true`). However, |
| 123 | think about whether this will provide enough [context](#Provide-Context). For |
| 124 | example, suppose we want to understand how often users interact with a button. |
Jared Saul | 73a9daaf | 2021-05-04 15:33:02 | [diff] [blame] | 125 | Just knowing that users clicked this particular button 1 million times in a day |
Ilya Sherman | b964189 | 2020-11-06 00:53:55 | [diff] [blame] | 126 | is not very informative on its own: The size of Chrome's user base is constantly |
| 127 | changing, only a subset of users have consented to metrics reporting, different |
| 128 | platforms have different sampling rates for metrics reporting, and so on. The |
| 129 | data would be much easier to make sense of if it included a baseline: how often |
| 130 | is the button shown? |
| 131 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 132 | If only a few buckets are emitted to, consider using a [sparse |
Mark Pearson | 4d0b463 | 2017-10-04 21:58:48 | [diff] [blame] | 133 | histogram](#When-To-Use-Sparse-Histograms). |
| 134 | |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 135 | #### Requirements |
| 136 | |
| 137 | Enums logged in histograms must: |
| 138 | |
| 139 | - be prefixed with the comment: |
| 140 | ```c++ |
| 141 | // These values are persisted to logs. Entries should not be renumbered and |
| 142 | // numeric values should never be reused. |
| 143 | ``` |
| 144 | - be numbered starting from `0`. Note this bullet point does *not* apply for |
| 145 | enums logged with sparse histograms. |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 146 | - have enumerators with explicit values (`= 0`, `= 1`, `= 2`) to make it clear |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 147 | that the actual values are important. This also makes it easy to match the |
| 148 | values between the C++/Java definition and [histograms.xml](./histograms.xml). |
| 149 | - not renumber or reuse enumerator values. When adding a new enumerator, append |
| 150 | the new enumerator to the end. When removing an unused enumerator, comment it |
| 151 | out, making it clear the value was previously used. |
| 152 | |
| 153 | If your enum histogram has a catch-all / miscellaneous bucket, put that bucket |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 154 | first (`= 0`). This makes the bucket easy to find on the dashboard if additional |
| 155 | buckets are added later. |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 156 | |
| 157 | #### Usage |
| 158 | |
Ilya Sherman | b6bd3c7 | 2020-04-15 23:08:15 | [diff] [blame] | 159 | *In C++*, define an `enum class` with a `kMaxValue` enumerator: |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 160 | |
Steven Holte | ecf841d | 2018-08-10 00:53:34 | [diff] [blame] | 161 | ```c++ |
Hong Xu | 4b0bc44f | 2023-08-01 20:30:42 | [diff] [blame] | 162 | // These values are persisted to logs. Entries should not be renumbered and |
| 163 | // numeric values should never be reused. |
James Lee | 53e80dc | 2024-04-19 11:31:06 | [diff] [blame] | 164 | // |
| 165 | // LINT.IfChange(NewTabPageAction) |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 166 | enum class NewTabPageAction { |
| 167 | kUseOmnibox = 0, |
| 168 | kClickTitle = 1, |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 169 | // kUseSearchbox = 2, // no longer used, combined into omnibox |
| 170 | kOpenBookmark = 3, |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 171 | kMaxValue = kOpenBookmark, |
| 172 | }; |
James Lee | 53e80dc | 2024-04-19 11:31:06 | [diff] [blame] | 173 | // LINT.ThenChange(//path/to/enums.xml:NewTabPageActionEnum) |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 174 | ``` |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 175 | |
James Lee | 53e80dc | 2024-04-19 11:31:06 | [diff] [blame] | 176 | The `LINT.IfChange / LINT.ThenChange` comments point between the code and XML |
| 177 | definitions of the enum, to encourage them to be kept in sync. See |
| 178 | [guide](https://www.chromium.org/chromium-os/developer-library/guides/development/keep-files-in-sync/) |
| 179 | and [more details](http://go/gerrit-ifthisthenthat). |
| 180 | |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 181 | `kMaxValue` is a special enumerator that must share the highest enumerator |
| 182 | value, typically done by aliasing it with the enumerator with the highest |
| 183 | value: clang automatically checks that `kMaxValue` is correctly set for `enum |
| 184 | class`. |
| 185 | |
Takashi Toyoshima | 0b52076 | 2024-05-08 23:17:33 | [diff] [blame^] | 186 | *In Mojo*, define an `enum` without a `kMaxValue` enumerator as `kMaxValue` is |
| 187 | autogenerated for Mojo C++ bindings: |
| 188 | |
| 189 | ```c++ |
| 190 | // These values are persisted to logs. Entries should not be renumbered and |
| 191 | // numeric values should never be reused. |
| 192 | // |
| 193 | // LINT.IfChange(PreloadType) |
| 194 | enum PrerenderType { |
| 195 | kPrefetch = 0, |
| 196 | // kPrerender = 1, // deprecated, revamped as kPrerender2 |
| 197 | kNoStatePrefetch = 2, |
| 198 | kPrerender2 = 3, |
| 199 | }; |
| 200 | // LINT.ThenChange(//path/to/enums.xml:PreloadType) |
| 201 | ``` |
| 202 | |
| 203 | *In C++*, the histogram helpers use the `kMaxValue` convention, and the enum may |
| 204 | be logged with: |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 205 | |
| 206 | ```c++ |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 207 | UMA_HISTOGRAM_ENUMERATION("NewTabPageAction", action); |
| 208 | ``` |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 209 | |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 210 | or: |
| 211 | |
Steven Holte | ecf841d | 2018-08-10 00:53:34 | [diff] [blame] | 212 | ```c++ |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 213 | UmaHistogramEnumeration("NewTabPageAction", action); |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 214 | ``` |
Steven Holte | ecf841d | 2018-08-10 00:53:34 | [diff] [blame] | 215 | |
Hong Xu | 365a4f7 | 2022-02-25 04:26:02 | [diff] [blame] | 216 | where `action` is an enumerator of the enumeration type `NewTabPageAction`. |
| 217 | |
Nate Fischer | 1f6efe5 | 2020-06-17 19:18:21 | [diff] [blame] | 218 | Logging histograms from Java should look similar: |
| 219 | |
| 220 | ```java |
| 221 | // These values are persisted to logs. Entries should not be renumbered and |
| 222 | // numeric values should never be reused. |
| 223 | @IntDef({NewTabPageAction.USE_OMNIBOX, NewTabPageAction.CLICK_TITLE, |
| 224 | NewTabPageAction.OPEN_BOOKMARK}) |
| 225 | private @interface NewTabPageAction { |
| 226 | int USE_OMNIBOX = 0; |
| 227 | int CLICK_TITLE = 1; |
| 228 | // int USE_SEARCHBOX = 2; // no longer used, combined into omnibox |
| 229 | int OPEN_BOOKMARK = 3; |
| 230 | int COUNT = 4; |
| 231 | } |
| 232 | |
| 233 | // Using a helper function is optional, but avoids some boilerplate. |
| 234 | private static void logNewTabPageAction(@NewTabPageAction int action) { |
| 235 | RecordHistogram.recordEnumeratedHistogram( |
| 236 | "NewTabPageAction", action, NewTabPageAction.COUNT); |
| 237 | } |
| 238 | ``` |
| 239 | |
Hong Xu | 7729284 | 2022-05-18 06:43:59 | [diff] [blame] | 240 | Finally, regardless of the programming language you are using, add the |
James Lee | 53e80dc | 2024-04-19 11:31:06 | [diff] [blame] | 241 | definition of the enumerator to [enums.xml](./enums.xml), and add linter checks |
| 242 | to keep the C++/Java and XML values in sync: |
| 243 | |
| 244 | ```xml |
| 245 | <!-- LINT.IfChange(NewTabPageActionEnum) --> |
| 246 | <enum name="NewTabPageActionEnum"> |
| 247 | ... |
| 248 | </enum> |
| 249 | <!-- LINT.ThenChange(//path/to/cpp_definition.h:NewTabPageAction) --> |
| 250 | ``` |
Hong Xu | 7729284 | 2022-05-18 06:43:59 | [diff] [blame] | 251 | |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 252 | #### Legacy Enums |
| 253 | |
| 254 | **Note: this method of defining histogram enums is deprecated. Do not use this |
Ilya Sherman | b6bd3c7 | 2020-04-15 23:08:15 | [diff] [blame] | 255 | for new enums *in C++*.** |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 256 | |
Chris Blume | bdca7ca | 2020-06-08 15:48:35 | [diff] [blame] | 257 | Many legacy enums define a `kCount` sentinel, relying on the compiler to |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 258 | automatically update it when new entries are added: |
| 259 | |
Steven Holte | ecf841d | 2018-08-10 00:53:34 | [diff] [blame] | 260 | ```c++ |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 261 | enum class NewTabPageAction { |
| 262 | kUseOmnibox = 0, |
| 263 | kClickTitle = 1, |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 264 | // kUseSearchbox = 2, // no longer used, combined into omnibox |
| 265 | kOpenBookmark = 3, |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 266 | kCount, |
| 267 | }; |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 268 | ``` |
Steven Holte | ecf841d | 2018-08-10 00:53:34 | [diff] [blame] | 269 | |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 270 | These enums must be recorded using the legacy helpers: |
| 271 | |
| 272 | ```c++ |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 273 | UMA_HISTOGRAM_ENUMERATION("NewTabPageAction", action, NewTabPageAction::kCount); |
| 274 | ``` |
| 275 | |
Daniel Cheng | 914170d2 | 2019-05-08 09:46:32 | [diff] [blame] | 276 | or: |
| 277 | |
| 278 | ```c++ |
| 279 | UmaHistogramEnumeration("NewTabPageAction", action, NewTabPageAction::kCount); |
| 280 | ``` |
mpearson | b36013be | 2017-02-10 20:10:54 | [diff] [blame] | 281 | |
Matt Giuca | f3e0e253 | 2017-10-03 23:07:52 | [diff] [blame] | 282 | ### Flag Histograms |
| 283 | |
| 284 | When adding a new flag in |
| 285 | [about_flags.cc](../../../chrome/browser/about_flags.cc), you need to add a |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 286 | corresponding entry to [enums.xml](./enums.xml). This is automatically verified |
| 287 | by the `AboutFlagsHistogramTest` unit test. |
Matt Giuca | f3e0e253 | 2017-10-03 23:07:52 | [diff] [blame] | 288 | |
| 289 | To add a new entry: |
| 290 | |
manukh | 26fe985 | 2022-10-04 23:38:14 | [diff] [blame] | 291 | 1. After adding flags |
| 292 | to [about_flags.cc](../../../chrome/browser/about_flags.cc), |
| 293 | run `generate_flag_enums.py --feature <your awesome feature>` or |
| 294 | simply `generate_flag_enums.py` (slower). |
| 295 | |
| 296 | You can alternatively follow these steps: |
| 297 | |
Matt Giuca | f3e0e253 | 2017-10-03 23:07:52 | [diff] [blame] | 298 | 1. Edit [enums.xml](./enums.xml), adding the feature to the `LoginCustomFlags` |
Brett Wilson | f4d5877 | 2017-10-30 21:37:57 | [diff] [blame] | 299 | enum section, with any unique value (just make one up, although whatever it |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 300 | is needs to appear in sorted order; `pretty_print.py` can do this for you). |
Matt Giuca | f3e0e253 | 2017-10-03 23:07:52 | [diff] [blame] | 301 | 2. Build `unit_tests`, then run `unit_tests |
Eric Lawrence | d4d7d5c | 2023-05-09 20:48:25 | [diff] [blame] | 302 | --gtest_filter=AboutFlagsHistogramTest.*` to compute the correct value. |
Matt Giuca | f3e0e253 | 2017-10-03 23:07:52 | [diff] [blame] | 303 | 3. Update the entry in [enums.xml](./enums.xml) with the correct value, and move |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 304 | it so the list is sorted by value (`pretty_print.py` can do this for you). |
Matt Giuca | f3e0e253 | 2017-10-03 23:07:52 | [diff] [blame] | 305 | 4. Re-run the test to ensure the value and ordering are correct. |
| 306 | |
| 307 | You can also use `tools/metrics/histograms/validate_format.py` to check the |
| 308 | ordering (but not that the value is correct). |
| 309 | |
| 310 | Don't remove entries when removing a flag; they are still used to decode data |
| 311 | from previous Chrome versions. |
| 312 | |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 313 | ### Count Histograms |
| 314 | |
| 315 | [histogram_macros.h](https://cs.chromium.org/chromium/src/base/metrics/histogram_macros.h) |
Caitlin Fischer | fc138c8 | 2021-11-04 21:31:19 | [diff] [blame] | 316 | provides macros for some common count types, such as memory or elapsed time, in |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 317 | addition to general count macros. These have reasonable default values; you |
| 318 | seldom need to choose the number of buckets or histogram min. However, you still |
| 319 | need to choose the histogram max (use the advice below). |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 320 | |
Caitlin Fischer | fc138c8 | 2021-11-04 21:31:19 | [diff] [blame] | 321 | If none of the default macros work well for you, please thoughtfully choose a |
| 322 | min, max, and bucket count for your histogram using the advice below. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 323 | |
rkaplow | 6dfcb89 | 2016-10-04 14:04:27 | [diff] [blame] | 324 | #### Count Histograms: Choosing Min and Max |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 325 | |
Caitlin Fischer | fc138c8 | 2021-11-04 21:31:19 | [diff] [blame] | 326 | For the max, choose a value such that very few histogram samples exceed the max. |
| 327 | If a sample is greater than or equal to the max value, it is put in an |
| 328 | "overflow" bucket. If this bucket is too large, it can be difficult to compute |
| 329 | statistics. One rule of thumb is that at most 1% of samples should be in the |
Robert Kaplow | cbc6fd6 | 2021-03-19 15:11:40 | [diff] [blame] | 330 | overflow bucket (and ideally, less). This allows analysis of the 99th |
| 331 | percentile. Err on the side of too large a range versus too short a range. |
Caitlin Fischer | fc138c8 | 2021-11-04 21:31:19 | [diff] [blame] | 332 | Remember that if you choose poorly, you'll have to wait for another release |
| 333 | cycle to fix it. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 334 | |
Caitlin Fischer | fc138c8 | 2021-11-04 21:31:19 | [diff] [blame] | 335 | For the min, use 1 if you care about all possible values (zero and above). All |
| 336 | histograms have an underflow bucket for emitted zeros, so a min of 1 is |
| 337 | appropriate. Otherwise, choose the min appropriate for your particular |
| 338 | situation. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 339 | |
rkaplow | 6dfcb89 | 2016-10-04 14:04:27 | [diff] [blame] | 340 | #### Count Histograms: Choosing Number of Buckets |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 341 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 342 | Choose the smallest number of buckets that give you the granularity you need. By |
Hong Xu | 3a229d83 | 2022-05-12 04:37:30 | [diff] [blame] | 343 | default, count histogram bucket sizes increase exponentially with respect to the |
| 344 | value (i.e., exponential binning), so you can get fine granularity when the |
| 345 | values are small yet still reasonable resolution when the values are larger. The |
| 346 | macros default to 50 buckets (or 100 buckets for histograms with wide ranges), |
| 347 | which is appropriate for most purposes. Because histograms pre-allocate all the |
| 348 | buckets, the number of buckets selected directly dictates how much memory is |
| 349 | used. Do not exceed 100 buckets without good reason (and consider whether |
| 350 | [sparse histograms](#When-To-Use-Sparse-Histograms) might work better for you in |
| 351 | that case—they do not pre-allocate their buckets). |
rkaplow | 8a62ef6 | 2016-10-06 14:42:34 | [diff] [blame] | 352 | |
Mark Pearson | 6be2f35c | 2018-08-14 07:06:02 | [diff] [blame] | 353 | ### Timing Histograms |
| 354 | |
Yoshisato Yanagisawa | 4782e2f | 2024-04-19 00:50:34 | [diff] [blame] | 355 | You can easily emit a time duration (time delta) using base::UmaHistogramTimes, |
| 356 | base::UmaHistogramMediumTimes, base::UmaHistogramLongTimes, and their friends. |
| 357 | For the critical path, UMA_HISTOGRAM_TIMES, UMA_HISTOGRAM_MEDIUM_TIMES, |
| 358 | UMA_HISTOGRAM_LONG_TIMES macros, and their friends, as well as helpers like |
| 359 | SCOPED_UMA_HISTOGRAM_TIMER are also available. Many timing |
Mark Pearson | 6be2f35c | 2018-08-14 07:06:02 | [diff] [blame] | 360 | histograms are used for performance monitoring; if this is the case for you, |
| 361 | please read [this document about how to structure timing histograms to make |
| 362 | them more useful and |
Paul Jensen | 5107d9c | 2018-10-22 22:24:06 | [diff] [blame] | 363 | actionable](https://chromium.googlesource.com/chromium/src/+/lkgr/docs/speed/diagnostic_metrics.md). |
Mark Pearson | 6be2f35c | 2018-08-14 07:06:02 | [diff] [blame] | 364 | |
Mark Pearson | 49928ec | 2018-06-05 20:15:49 | [diff] [blame] | 365 | ### Percentage or Ratio Histograms |
| 366 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 367 | You can easily emit a percentage histogram using the UMA_HISTOGRAM_PERCENTAGE |
| 368 | macro provided in |
Mark Pearson | 49928ec | 2018-06-05 20:15:49 | [diff] [blame] | 369 | [histogram_macros.h](https://cs.chromium.org/chromium/src/base/metrics/histogram_macros.h). |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 370 | You can also easily emit any ratio as a linear histogram (for equally sized |
| 371 | buckets). |
Mark Pearson | 49928ec | 2018-06-05 20:15:49 | [diff] [blame] | 372 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 373 | For such histograms, you want each value recorded to cover approximately the |
| 374 | same span of time. This typically means emitting values periodically at a set |
| 375 | time interval, such as every 5 minutes. We do not recommend recording a ratio at |
| 376 | the end of a video playback, as video lengths vary greatly. |
Mark Pearson | 49928ec | 2018-06-05 20:15:49 | [diff] [blame] | 377 | |
Mark Pearson | 9be8bffa | 2020-03-03 19:08:02 | [diff] [blame] | 378 | It is okay to emit at the end of an animation sequence when what's being |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 379 | animated is fixed / known. In this case, each value represents roughly the same |
| 380 | span of time. |
Mark Pearson | 9be8bffa | 2020-03-03 19:08:02 | [diff] [blame] | 381 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 382 | Why? You typically cannot make decisions based on histograms whose values are |
| 383 | recorded in response to an event that varies in length because such metrics can |
| 384 | conflate heavy usage with light usage. It's easier to reason about metrics that |
| 385 | avoid this source of bias. |
Mark Pearson | 49928ec | 2018-06-05 20:15:49 | [diff] [blame] | 386 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 387 | Many developers have been bitten by this. For example, it was previously common |
| 388 | to emit an actions-per-minute ratio whenever Chrome was backgrounded. Precisely, |
| 389 | these metrics computed the number of uses of a particular action during a Chrome |
| 390 | session, divided by length of time Chrome had been open. Sometimes, the recorded |
| 391 | rate was based on a short interaction with Chrome–a few seconds or a minute. |
| 392 | Other times, the recorded rate was based on a long interaction, tens of minutes |
| 393 | or hours. These two situations are indistinguishable in the UMA logs–the |
| 394 | recorded values can be identical. |
Mark Pearson | 49928ec | 2018-06-05 20:15:49 | [diff] [blame] | 395 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 396 | The inability to distinguish these two qualitatively different settings make |
| 397 | such histograms effectively uninterpretable and not actionable. Emitting at a |
| 398 | regular interval avoids the issue. Each value represents the same amount of time |
| 399 | (e.g., one minute of video playback). |
Mark Pearson | 49928ec | 2018-06-05 20:15:49 | [diff] [blame] | 400 | |
rkaplow | 8a62ef6 | 2016-10-06 14:42:34 | [diff] [blame] | 401 | ### Local Histograms |
| 402 | |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 403 | Histograms can be added via [Local macros](https://codesearch.chromium.org/chromium/src/base/metrics/histogram_macros_local.h). |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 404 | These still record locally, but are not uploaded to UMA and are therefore not |
| 405 | available for analysis. This can be useful for metrics only needed for local |
| 406 | debugging. We don't recommend using local histograms outside of that scenario. |
rkaplow | 8a62ef6 | 2016-10-06 14:42:34 | [diff] [blame] | 407 | |
| 408 | ### Multidimensional Histograms |
| 409 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 410 | It is common to be interested in logging multidimensional data–where multiple |
rkaplow | 8a62ef6 | 2016-10-06 14:42:34 | [diff] [blame] | 411 | pieces of information need to be logged together. For example, a developer may |
| 412 | be interested in the counts of features X and Y based on whether a user is in |
| 413 | state A or B. In this case, they want to know the count of X under state A, |
| 414 | as well as the other three permutations. |
| 415 | |
| 416 | There is no general purpose solution for this type of analysis. We suggest |
| 417 | using the workaround of using an enum of length MxN, where you log each unique |
| 418 | pair {state, feature} as a separate entry in the same enum. If this causes a |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 419 | large explosion in data (i.e. >100 enum entries), a [sparse histogram](#When-To-Use-Sparse-Histograms) |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 420 | may be appropriate. If you are unsure of the best way to proceed, please contact |
| 421 | someone from the OWNERS file. |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 422 | |
| 423 | ## Histogram Expiry |
| 424 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 425 | Histogram expiry is specified by the `expires_after` attribute in histogram |
Mark Pearson | 37c3c9a | 2023-06-29 17:17:30 | [diff] [blame] | 426 | descriptions in histograms.xml. It is a required attribute. The attribute can |
| 427 | be specified as date in **YYYY-MM-DD** format or as Chrome milestone in |
| 428 | **M**\*(e.g. M105) format. In the latter case, the actual expiry date is about |
| 429 | 12 weeks after that branch is cut, or basically when it is replaced on the |
| 430 | "stable" channel by the following release. |
Brian White | fa0a3fa | 2019-05-13 16:58:11 | [diff] [blame] | 431 | |
Mark Pearson | ce4371c | 2021-03-15 23:57:42 | [diff] [blame] | 432 | After a histogram expires, it ceases to be displayed on the dashboard. |
| 433 | Follow [these directions](#extending) to extend it. |
Brian White | fa0a3fa | 2019-05-13 16:58:11 | [diff] [blame] | 434 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 435 | Once a histogram has expired, the code that records it becomes dead code and |
Gabriel Gauthier-Shalom | 0c0f1d86 | 2022-02-09 15:47:33 | [diff] [blame] | 436 | should be removed from the codebase. You should also [clean up](#obsolete) the |
Alexei Svitkine | d4cbf40 | 2022-11-14 20:55:25 | [diff] [blame] | 437 | corresponding entry in histograms.xml. In _rare_ cases, a histogram may be |
| 438 | expired intentionally while keeping the code around; such cases must be |
Alexei Svitkine | 6fbe8ac | 2022-11-14 21:17:40 | [diff] [blame] | 439 | [annotated appropriately](#Intentionally-expired-histograms) in histograms.xml. |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 440 | |
Brian White | 8614f81 | 2019-02-07 21:07:01 | [diff] [blame] | 441 | In **rare** cases, the expiry can be set to "never". This is used to denote |
Robert Kaplow | cbc6fd6 | 2021-03-19 15:11:40 | [diff] [blame] | 442 | metrics of critical importance that are, typically, used for other reports. For |
| 443 | example, all metrics of the |
| 444 | "[heartbeat](https://uma.googleplex.com/p/chrome/variations)" are set to never |
| 445 | expire. All metrics that never expire must have an XML comment describing why so |
| 446 | that it can be audited in the future. Setting an expiry to "never" must be |
| 447 | reviewed by [email protected]. |
Brian White | 8614f81 | 2019-02-07 21:07:01 | [diff] [blame] | 448 | |
| 449 | ``` |
| 450 | <!-- expires-never: "heartbeat" metric (internal: go/uma-heartbeats) --> |
| 451 | ``` |
| 452 | |
Mark Pearson | 37c3c9a | 2023-06-29 17:17:30 | [diff] [blame] | 453 | It is never appropriate to set the expiry to "never" on a new histogram. Most |
| 454 | new histograms don't turn out to have the properties the implementer wants, |
| 455 | whether due to bugs in the implementation or simply an evolving understanding |
| 456 | of what should be measured. |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 457 | |
Yoshisato Yanagisawa | 19d35ca | 2024-04-09 03:50:18 | [diff] [blame] | 458 | #### Guidelines on expiry |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 459 | |
Ilya Sherman | 67418ea | 2019-11-27 01:28:23 | [diff] [blame] | 460 | Here are some guidelines for common scenarios: |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 461 | |
Yoshisato Yanagisawa | 19d35ca | 2024-04-09 03:50:18 | [diff] [blame] | 462 | * If the listed owner moved to a different project, find a new owner. |
Ilya Sherman | 67418ea | 2019-11-27 01:28:23 | [diff] [blame] | 463 | * If neither the owner nor the team uses the histogram, remove it. |
| 464 | * If the histogram is not in use now, but might be useful in the far future, |
| 465 | remove it. |
| 466 | * If the histogram is not in use now, but might be useful in the near |
Brian White | db68067b | 2021-10-13 18:27:28 | [diff] [blame] | 467 | future, pick ~3 months (also ~3 milestones) ahead. |
Yoshisato Yanagisawa | 19d35ca | 2024-04-09 03:50:18 | [diff] [blame] | 468 | * Otherwise, pick an expiry that is reasonable for how long the metric should |
| 469 | be used, up to a year. |
Ilya Sherman | 67418ea | 2019-11-27 01:28:23 | [diff] [blame] | 470 | |
Brian White | db68067b | 2021-10-13 18:27:28 | [diff] [blame] | 471 | We also have a tool that automatically extends expiry dates. The most frequently |
| 472 | accessed histograms, currently 99%, have their expirations automatically |
| 473 | extended every Tuesday to 6 months from the date of the run. Googlers can view |
| 474 | the [design |
| 475 | doc](https://docs.google.com/document/d/1IEAeBF9UnYQMDfyh2gdvE7WlUKsfIXIZUw7qNoU89A4) |
| 476 | of the program that does this. The bottom line is: If the histogram is being |
| 477 | checked, it should be extended without developer interaction. |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 478 | |
Yoshisato Yanagisawa | 19d35ca | 2024-04-09 03:50:18 | [diff] [blame] | 479 | #### How to choose expiry for new histograms |
| 480 | |
| 481 | In general, set an expiry that is reasonable for how long the metric should |
| 482 | be used, up to a year. |
| 483 | |
| 484 | Some common cases: |
| 485 | |
| 486 | * When adding a histogram to evaluate a feature launch, set an expiry date |
| 487 | consistent with the expected feature launch date. |
| 488 | * If you expect the histogram to be useful for an indefinite time, set an |
| 489 | expiry date up to 1 year out. This gives a chance to re-evaluate whether |
| 490 | the histogram indeed proved to be useful. |
| 491 | * Otherwise, 3-6 months (3-6 milestones) is typically a good choice. |
| 492 | |
Mark Pearson | ce4371c | 2021-03-15 23:57:42 | [diff] [blame] | 493 | #### How to extend an expired histogram {#extending} |
| 494 | |
| 495 | You can revive an expired histogram by setting the expiration date to a |
| 496 | date in the future. |
| 497 | |
| 498 | There's some leeway here. A client may continue to send data for that |
| 499 | histogram for some time after the official expiry date so simply bumping |
| 500 | the 'expires_after' date at HEAD may be sufficient to resurrect it without |
| 501 | any data discontinuity. |
| 502 | |
| 503 | If a histogram expired more than a month ago (for histograms with an |
| 504 | expiration date) or more than one milestone ago (for histograms with |
| 505 | expiration milestones; this means top-of-tree is two or more milestones away |
| 506 | from expired milestone), then you may be outside the safety window. In this |
| 507 | case, when extending the histogram add to the histogram description a |
| 508 | message: "Warning: this histogram was expired from DATE to DATE; data may be |
| 509 | missing." (For milestones, write something similar.) |
| 510 | |
| 511 | When reviving a histogram outside the safety window, realize the change to |
| 512 | histograms.xml to revive it rolls out with the binary release. It takes |
| 513 | some time to get to the stable channel. |
| 514 | |
| 515 | It you need to revive it faster, the histogram can be re-enabled via adding to |
| 516 | the [expired histogram allowlist](#Expired-histogram-allowlist). |
| 517 | |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 518 | ### Expired histogram notifier |
| 519 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 520 | The expired histogram notifier notifies histogram owners before their histograms |
| 521 | expire by creating crbugs, which are assigned to owners. This allows owners to |
| 522 | extend the lifetime of their histograms, if needed, or deprecate them. The |
| 523 | notifier regularly checks all histograms across the histograms.xml files and |
| 524 | identifies expired or soon-to-be expired histograms. It then creates or updates |
| 525 | crbugs accordingly. |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 526 | |
Caitlin Fischer | 9f484105 | 2020-11-04 21:02:44 | [diff] [blame] | 527 | ### Expired histogram allowlist |
Gayane Petrosyan | a6ee443c | 2018-05-17 21:39:54 | [diff] [blame] | 528 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 529 | If a histogram expires but turns out to be useful, you can add the histogram's |
Alexei Svitkine | d4cbf40 | 2022-11-14 20:55:25 | [diff] [blame] | 530 | name to the allowlist to re-enable logging for it, until the updated expiration |
| 531 | date reaches the Stable channel. When doing so, update the histogram's summary |
| 532 | to document the period during which the histogram's data is incomplete. To add a |
| 533 | histogram to the allowlist, see the internal documentation: |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 534 | [Histogram Expiry](https://goto.google.com/histogram-expiry-gdoc). |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 535 | |
Alexei Svitkine | 6fbe8ac | 2022-11-14 21:17:40 | [diff] [blame] | 536 | ### Intentionally expired histograms |
Alexei Svitkine | d4cbf40 | 2022-11-14 20:55:25 | [diff] [blame] | 537 | |
| 538 | In **rare** cases, a histogram may be expired intentionally while keeping the |
| 539 | code around. For example, this can be useful for diagnostic metrics that are |
| 540 | occasionally needed to investigate specific bugs, but do not need to be reported |
| 541 | otherwise. |
| 542 | |
| 543 | To avoid such histograms to be flagged for code clean up, they must be annotated |
| 544 | in the histograms.xml with the `expired_intentionally` tag as follows: |
| 545 | |
| 546 | ```xml |
| 547 | <histogram name="Tab.Open" enum="TabType" expires_after="M100"> |
| 548 | <expired_intentionally>Kept as a diagnostic metric.</expired_intentionally> |
| 549 | <owner>[email protected]</owner> |
| 550 | <summary>Histogram summary.</summary> |
| 551 | </histogram> |
| 552 | ``` |
| 553 | |
mpearson | 72a5c9139 | 2017-05-09 22:49:44 | [diff] [blame] | 554 | ## Testing |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 555 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 556 | Test your histograms using `chrome://histograms`. Make sure they're being |
rkaplow | 6dfcb89 | 2016-10-04 14:04:27 | [diff] [blame] | 557 | emitted to when you expect and not emitted to at other times. Also check that |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 558 | the values emitted to are correct. Finally, for count histograms, make sure |
rkaplow | 6dfcb89 | 2016-10-04 14:04:27 | [diff] [blame] | 559 | that buckets capture enough precision for your needs over the range. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 560 | |
Ivan Sandrk | 8ffc583 | 2018-07-09 12:34:58 | [diff] [blame] | 561 | Pro tip: You can filter the set of histograms shown on `chrome://histograms` by |
Luc Nguyen | b1324cb | 2022-12-17 16:23:41 | [diff] [blame] | 562 | appending to the URL. For example, `chrome://histograms/UserActions` shows |
| 563 | only histograms whose names contain "UserActions", such as |
| 564 | "UMA.UserActionsCount". |
Ivan Sandrk | 8ffc583 | 2018-07-09 12:34:58 | [diff] [blame] | 565 | |
mpearson | 72a5c9139 | 2017-05-09 22:49:44 | [diff] [blame] | 566 | In addition to testing interactively, you can have unit tests examine the |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 567 | values emitted to histograms. See [histogram_tester.h](https://cs.chromium.org/chromium/src/base/test/metrics/histogram_tester.h) |
mpearson | 72a5c9139 | 2017-05-09 22:49:44 | [diff] [blame] | 568 | for details. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 569 | |
Luc Nguyen | b1324cb | 2022-12-17 16:23:41 | [diff] [blame] | 570 | See also `chrome://metrics-internals` ([docs](https://chromium.googlesource.com/chromium/src/+/master/components/metrics/debug/README.md)) |
| 571 | for more thorough manual testing if needed. |
| 572 | |
Robert Kaplow | 8202763 | 2023-02-13 16:31:52 | [diff] [blame] | 573 | By default, histograms in unit or browser tests will not be actually uploaded. |
| 574 | In general, you can rely on the UMA infrastructure to upload the metrics correctly. |
| 575 | |
Alan Screen | 291bc9a | 2023-06-27 21:16:49 | [diff] [blame] | 576 | ### Don't Use Histograms to Prove Main Logic Correctness |
| 577 | |
| 578 | Do not rely upon using histograms in tests as a way to prove correctness of |
| 579 | your main program logic. If a unit or browser test uses a histogram count as a |
| 580 | way to validate logic then that test coverage would be lost if the histogram is |
| 581 | deleted after it has expired. That situation would prevent cleanup of the |
| 582 | histogram. Construct your tests using other means to validate your general |
| 583 | logic, and only use |
| 584 | [`HistogramTester`](https://cs.chromium.org/chromium/src/base/test/metrics/histogram_tester.h) |
| 585 | to verify that the histogram values are being generated as you would expect. |
| 586 | |
Dana Fried | ac15ff8 | 2024-04-02 21:19:34 | [diff] [blame] | 587 | ### Verify Enum and Variant Values |
| 588 | |
| 589 | If you have <enum> or <variant> entries that need to be updated to match code, |
| 590 | you can use |
| 591 | [HistogramEnumReader](https://cs.chromium.org/chromium/src/base/test/metrics/histogram_enum_reader.h) |
| 592 | or |
| 593 | [HistogramVariantsReader](https://cs.chromium.org/chromium/src/base/test/metrics/histogram_enum_reader.h) |
| 594 | to read and verify the expected values in a unit test. This prevents a mismatch |
| 595 | between code and histogram data from slipping through CQ. |
| 596 | |
| 597 | For an example, see |
| 598 | [BrowserUserEducationServiceTest.CheckFeaturePromoHistograms](https://cs.chromium.org/chromium/src/chrome/browser/ui/views/user_education/browser_user_education_service_unittest.cc). |
| 599 | |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 600 | ## Interpreting the Resulting Data |
| 601 | |
| 602 | The top of [go/uma-guide](http://go/uma-guide) has good advice on how to go |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 603 | about analyzing and interpreting the results of UMA data uploaded by users. If |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 604 | you're reading this page, you've probably just finished adding a histogram to |
| 605 | the Chromium source code and you're waiting for users to update their version of |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 606 | Chrome to a version that includes your code. In this case, the best advice is |
| 607 | to remind you that users who update frequently / quickly are biased. Best take |
Mark Pearson | 4c4bc97 | 2018-05-16 20:01:06 | [diff] [blame] | 608 | the initial statistics with a grain of salt; they're probably *mostly* right but |
| 609 | not entirely so. |
| 610 | |
mpearson | 72a5c9139 | 2017-05-09 22:49:44 | [diff] [blame] | 611 | ## Revising Histograms |
| 612 | |
Robert Kaplow | cbc6fd6 | 2021-03-19 15:11:40 | [diff] [blame] | 613 | When changing the semantics of a histogram (when it's emitted, what the buckets |
| 614 | represent, the bucket range or number of buckets, etc.), create a new histogram |
| 615 | with a new name. Otherwise analysis that mixes the data pre- and post- change |
| 616 | may be misleading. If the histogram name is still the best name choice, the |
| 617 | recommendation is to simply append a '2' to the name. See [Cleaning Up Histogram |
Austin Sullivan | 1040ce2 | 2022-04-13 15:51:29 | [diff] [blame] | 618 | Entries](#obsolete) for details on how to handle the XML changes. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 619 | |
mpearson | 72a5c9139 | 2017-05-09 22:49:44 | [diff] [blame] | 620 | ## Deleting Histograms |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 621 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 622 | Please delete code that emits to histograms that are no longer needed. |
| 623 | Histograms take up memory. Cleaning up histograms that you no longer care |
| 624 | about is good! But see the note below on |
Gabriel Gauthier-Shalom | 0c0f1d86 | 2022-02-09 15:47:33 | [diff] [blame] | 625 | [Cleaning Up Histogram Entries](#obsolete). |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 626 | |
| 627 | ## Documenting Histograms |
| 628 | |
Darren Shen | da91dc75 | 2023-03-01 05:28:30 | [diff] [blame] | 629 | Document histograms in an appropriate [metadata/foo/histograms.xml](https://source.chromium.org/search?q=f:metadata%2F.*%2Fhistograms.xml&ss=chromium%2Fchromium%2Fsrc) |
| 630 | file. |
| 631 | |
| 632 | There is also a [google-internal version of the file](https://goto.google.com/chrome-histograms-internal) |
| 633 | for two cases: |
| 634 | |
| 635 | * The histogram is confidential (an accurate description about how to interpret |
| 636 | the histogram would reveal information about Google's plans). In this case, |
| 637 | you must only document the histogram in the internal version. |
| 638 | * The corresponding code that emits the histogram is internal (added only to |
| 639 | Chrome code, not to Chromium code). In this case, you may document the |
| 640 | histogram in either the internal or external version. |
Mark Pearson | 159c3897 | 2018-06-05 19:44:08 | [diff] [blame] | 641 | |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 642 | ### Add Histogram and Documentation in the Same Changelist |
| 643 | |
vapier | 52b9aba | 2016-12-14 06:09:25 | [diff] [blame] | 644 | If possible, please add the [histograms.xml](./histograms.xml) description in |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 645 | the same changelist in which you add the histogram-emitting code. This has |
| 646 | several benefits. One, it sometimes happens that the |
vapier | 52b9aba | 2016-12-14 06:09:25 | [diff] [blame] | 647 | [histograms.xml](./histograms.xml) reviewer has questions or concerns about the |
| 648 | histogram description that reveal problems with interpretation of the data and |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 649 | call for a different recording strategy. Two, it allows the histogram reviewer |
vapier | 52b9aba | 2016-12-14 06:09:25 | [diff] [blame] | 650 | to easily review the emission code to see if it comports with these best |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 651 | practices and to look for other errors. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 652 | |
| 653 | ### Understandable to Everyone |
| 654 | |
| 655 | Histogram descriptions should be roughly understandable to someone not familiar |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 656 | with your feature. Please add a sentence or two of background if necessary. |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 657 | |
Robert Kaplow | cbc6fd6 | 2021-03-19 15:11:40 | [diff] [blame] | 658 | Note any caveats associated with your histogram in the summary. For example, if |
| 659 | the set of supported platforms is surprising, such as if a desktop feature is |
| 660 | not available on Mac, the summary should explain where it is recorded. It is |
| 661 | also common to have caveats along the lines of "this histogram is only recorded |
| 662 | if X" (e.g., upon a successful connection to a service, a feature is enabled by |
| 663 | the user). |
| 664 | |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 665 | |
| 666 | ### State When It Is Recorded |
| 667 | |
| 668 | Histogram descriptions should clearly state when the histogram is emitted |
| 669 | (profile open? network request received? etc.). |
| 670 | |
Mark Pearson | d8fc9fd2 | 2021-03-12 20:18:58 | [diff] [blame] | 671 | Some histograms record error conditions. These should be clear about whether |
| 672 | all errors are recorded or only the first. If only the first, the histogram |
| 673 | description should have text like: |
| 674 | ``` |
| 675 | In the case of multiple errors, only the first reason encountered is recorded. Refer |
| 676 | to Class::FunctionImplementingLogic() for details. |
| 677 | ``` |
| 678 | |
Ilya Sherman | 470c95a | 2020-09-21 23:05:43 | [diff] [blame] | 679 | ### Provide Clear Units or Enum Labels |
| 680 | |
| 681 | For enumerated histograms, including boolean and sparse histograms, provide an |
| 682 | `enum=` attribute mapping enum values to semantically contentful labels. Define |
| 683 | the `<enum>` in enums.xml if none of the existing enums are a good fit. Use |
| 684 | labels whenever they would be clearer than raw numeric values. |
| 685 | |
| 686 | For non-enumerated histograms, include a `units=` attribute. Be specific: |
| 687 | e.g. distinguish "MB" vs. "MiB", refine generic labels like "counts" to more |
| 688 | precise labels like "pages", etc. |
| 689 | |
jsbell | da3a66c | 2017-02-09 21:40:32 | [diff] [blame] | 690 | ### Owners |
rkaplow | 8a62ef6 | 2016-10-06 14:42:34 | [diff] [blame] | 691 | |
Caitlin Fischer | 254a12f7 | 2019-07-31 20:57:03 | [diff] [blame] | 692 | Histograms need owners, who are the experts on the metric and the points of |
| 693 | contact for any questions or maintenance tasks, such as extending a histogram's |
| 694 | expiry or deprecating the metric. |
rkaplow | 8a62ef6 | 2016-10-06 14:42:34 | [diff] [blame] | 695 | |
Caitlin Fischer | 254a12f7 | 2019-07-31 20:57:03 | [diff] [blame] | 696 | Histograms must have a primary owner and may have secondary owners. A primary |
Mario Bianucci | 9947bbd | 2020-10-28 17:41:47 | [diff] [blame] | 697 | owner is a Googler with an @google.com or @chromium.org email address, e.g. |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 698 | <owner>[email protected]</owner>, who is ultimately responsible for maintaining |
| 699 | the metric. Secondary owners may be other individuals, team mailing lists, e.g. |
| 700 | <owner>[email protected]</owner>, or paths to OWNERS files, e.g. |
| 701 | <owner>src/directory/OWNERS</owner>. |
Mark Pearson | 74c5321 | 2019-03-08 00:34:08 | [diff] [blame] | 702 | |
Caitlin Fischer | 254a12f7 | 2019-07-31 20:57:03 | [diff] [blame] | 703 | It's a best practice to list multiple owners, so that there's no single point |
| 704 | of failure for histogram-related questions and maintenance tasks. If you are |
| 705 | using a metric heavily and understand it intimately, feel free to add yourself |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 706 | as an owner. |
Mark Pearson | 74c5321 | 2019-03-08 00:34:08 | [diff] [blame] | 707 | |
Caitlin Fischer | 254a12f7 | 2019-07-31 20:57:03 | [diff] [blame] | 708 | Notably, owners are asked to determine whether histograms have outlived their |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 709 | usefulness. When a histogram is nearing expiry, a robot files a reminder bug in |
| 710 | Monorail. It's important that somebody familiar with the histogram notices and |
| 711 | triages such bugs! |
rkaplow | 8a62ef6 | 2016-10-06 14:42:34 | [diff] [blame] | 712 | |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 713 | Tip: When removing someone from the owner list for a histogram, it's a nice |
| 714 | courtesy to ask them for approval. |
| 715 | |
Caitlin Fischer | feafb439 | 2020-10-05 21:10:07 | [diff] [blame] | 716 | ### Components |
| 717 | |
Ariel Zhang | 62ee3f4 | 2024-02-26 23:25:29 | [diff] [blame] | 718 | Histograms may be associated with a component, which can help make sure that |
Caitlin Fischer | feafb439 | 2020-10-05 21:10:07 | [diff] [blame] | 719 | histogram expiry bugs don't fall through the cracks. |
| 720 | |
Ariel Zhang | 62ee3f4 | 2024-02-26 23:25:29 | [diff] [blame] | 721 | A histogram is associated with the `buganizer_public` component listed in the |
| 722 | DIR_METADATA file adjacent to the histograms.xml file if present. |
| 723 | |
| 724 | There are two other ways in which components may be associated with a |
| 725 | histogram. The first way is to add a tag containing the component ID to a |
| 726 | histogram or histogram suffix, e.g. <component>1456399</component>. The second |
| 727 | way is to specify an OWNERS file as a secondary owner for a histogram. If the |
| 728 | OWNERS file has an adjacent DIR_METADATA file that contains a |
| 729 | `buganizer_public` component, then that component is associated with the |
| 730 | histogram. If there isn't a parallel DIR_METADATA file with such a component, |
| 731 | but an ancestor directory has one, then the ancestor directory's component is |
| 732 | used. |
| 733 | |
| 734 | If more than one component is associated with a histogram, <component> tag is |
| 735 | favored over adjacent DIR_METADATA file and over OWNERS file. |
| 736 | |
| 737 | **Note:** For non-Chromium Issue Tracker (ChromeOS Public Tracker or internal) |
| 738 | components, make sure [email protected] has access to create and |
| 739 | update issues. |
| 740 | |
Caitlin Fischer | feafb439 | 2020-10-05 21:10:07 | [diff] [blame] | 741 | |
Sun Yueru | 3938571 | 2023-02-09 20:11:08 | [diff] [blame] | 742 | ### Improvement Direction |
| 743 | For some histograms, an increase or a decrease in the reported values can be |
| 744 | associated with either an improvement or a deterioration. For example, if you |
| 745 | are tracking page load speed, then seeing your metrics tracking page load time |
| 746 | in milliseconds getting gradually larger values, perhaps as the result of a |
| 747 | Finch study, may signify worse performance; on the contrary, seeing a reduction |
| 748 | in the page load speed may indicate an improvement. You can provide this |
| 749 | information on the movement direction by adding a tag |
| 750 | `<improvement direction="LOWER_IS_BETTER"/>` within your `<histogram>`. The |
| 751 | opposite is `<improvement direction="HIGHER_IS_BETTER"/>`. |
| 752 | |
| 753 | For other histograms where there may not be a movement direction that's clearly |
| 754 | better, you can set `<improvement direction="NEITHER_IS_BETTER"/>`. |
| 755 | |
| 756 | This `<improvement>` tag is optional. You can also add/delete this tag or make a |
| 757 | correction to its `direction` attribute any time. |
| 758 | |
Gabriel Gauthier-Shalom | 0c0f1d86 | 2022-02-09 15:47:33 | [diff] [blame] | 759 | ### Cleaning Up Histogram Entries {#obsolete} |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 760 | |
Alexei Svitkine | 7bfb670 | 2023-11-28 18:18:24 | [diff] [blame] | 761 | When the code to log a histogram is deleted, its corresponding histograms.xml |
| 762 | entry should also be removed. Past histogram data will still be available for |
| 763 | viewing on Google's internal UMA dashboard. |
Pavol Marko | 17ed24e | 2023-09-11 09:43:15 | [diff] [blame] | 764 | |
Alexei Svitkine | 7bfb670 | 2023-11-28 18:18:24 | [diff] [blame] | 765 | The CL to remove one or more histograms can also specify an obsoletion message |
| 766 | through special syntax in the CL description. This also applies to variants of a |
Gabriel Gauthier-Shalom | 0c0f1d86 | 2022-02-09 15:47:33 | [diff] [blame] | 767 | [patterned histogram](#Patterned-Histograms) and to suffix entries for a |
| 768 | suffixed histogram. |
Mark Pearson | 2a311c5 | 2019-03-19 21:47:01 | [diff] [blame] | 769 | |
Ariel Zhang | ed17ef2 | 2023-05-18 16:42:48 | [diff] [blame] | 770 | The changelist that obsoletes a histogram entry should be reviewed by all |
| 771 | current owners. |
Mark Pearson | 2a311c5 | 2019-03-19 21:47:01 | [diff] [blame] | 772 | |
Ariel Zhang | ed17ef2 | 2023-05-18 16:42:48 | [diff] [blame] | 773 | #### Remove the Entry |
Mark Pearson | 2a311c5 | 2019-03-19 21:47:01 | [diff] [blame] | 774 | |
Ariel Zhang | ed17ef2 | 2023-05-18 16:42:48 | [diff] [blame] | 775 | Delete the entry in the histograms.xml file. |
Gabriel Gauthier-Shalom | 0c0f1d86 | 2022-02-09 15:47:33 | [diff] [blame] | 776 | |
Gabriel Gauthier-Shalom | a7394aa | 2022-02-14 16:33:37 | [diff] [blame] | 777 | * In some cases there may be artifacts that remain, with some examples being: |
Charlie Harrison | 1ad2f85 | 2023-11-06 18:22:23 | [diff] [blame] | 778 | * Empty `<token>` blocks, or individual `<variant>`s. |
Gabriel Gauthier-Shalom | a7394aa | 2022-02-14 16:33:37 | [diff] [blame] | 779 | * `<enum>` blocks from enums.xml that are no longer used. |
Alexei Svitkine | 7bfb670 | 2023-11-28 18:18:24 | [diff] [blame] | 780 | * Suffix entries in `histogram_suffixes_list.xml`. |
Gabriel Gauthier-Shalom | a7394aa | 2022-02-14 16:33:37 | [diff] [blame] | 781 | * Please remove these artifacts if you find them. |
Sun Yueru | f81cee3 | 2023-01-19 01:52:58 | [diff] [blame] | 782 | * **Exception**: please update the label of `<int value=... label=... />` with |
| 783 | the `(Obsolete) ` prefix, e.g. |
| 784 | `<int value="1" label="(Obsolete) Navigation failed. Removed in 2023/01."/>` |
| 785 | rather than deleting them, if the surrounding `<enum>` block is not being |
| 786 | deleted. |
Ariel Zhang | ed17ef2 | 2023-05-18 16:42:48 | [diff] [blame] | 787 | |
| 788 | #### Add an Obsoletion Message |
| 789 | |
Will Harris | 0874e55 | 2023-08-25 16:09:44 | [diff] [blame] | 790 | An obsoletion message is displayed on the dashboard and provides developers |
| 791 | context for why the histogram was removed and, if applicable, which histogram |
| 792 | it was replaced by. |
| 793 | |
Alexei Svitkine | 7bfb670 | 2023-11-28 18:18:24 | [diff] [blame] | 794 | **Note:** You can skip this step if the histogram is expired. This is because |
| 795 | tooling automatically records the date and milestone of a histogram's |
Ariel Zhang | d111b72 | 2023-12-12 15:48:58 | [diff] [blame] | 796 | removal. |
Ariel Zhang | 1cd26820 | 2023-07-14 19:30:56 | [diff] [blame] | 797 | |
Will Harris | 0874e55 | 2023-08-25 16:09:44 | [diff] [blame] | 798 | You can provide a custom obsoletion message for a removed histogram via tags |
| 799 | on the CL description: |
Ariel Zhang | ed17ef2 | 2023-05-18 16:42:48 | [diff] [blame] | 800 | |
| 801 | * Add the obsoletion message in the CL description in the format |
Alexei Svitkine | 7bfb670 | 2023-11-28 18:18:24 | [diff] [blame] | 802 | `OBSOLETE_HISTOGRAM[histogram name]=message`, e.g.: |
| 803 | `OBSOLETE_HISTOGRAM[Tab.Count]=Replaced by Tab.Count2` |
| 804 | * To add the same obsoletion message to all the histograms removed in the CL, |
| 805 | you can use `OBSOLETE_HISTOGRAMS=message`, e.g.: |
| 806 | `OBSOLETE_HISTOGRAMS=Patterned histogram Hist.{Token} is replaced by Hist.{Token}.2` |
Ariel Zhang | 7b8cbf9 | 2023-06-21 22:24:14 | [diff] [blame] | 807 | * **Notes:** |
Ariel Zhang | d111b72 | 2023-12-12 15:48:58 | [diff] [blame] | 808 | * **The full tag should be put on a single line, even if it is longer than the |
| 809 | maximum CL description width.** |
Ariel Zhang | 1cd26820 | 2023-07-14 19:30:56 | [diff] [blame] | 810 | * You can add multiple obsoletion message tags in one CL. |
Alexei Svitkine | 7bfb670 | 2023-11-28 18:18:24 | [diff] [blame] | 811 | * `OBSOLETE_HISTOGRAMS` messages will be overwritten by histogram-specific |
| 812 | ones, if present. |
| 813 | * You could also include information about why the histogram was removed. For |
| 814 | example, you might indicate how the histogram's summary did not accurately |
| 815 | describe the collected data. |
| 816 | * If the histogram is being replaced, include the name of the replacement and |
| 817 | make sure that the new description is different from the original to reflect |
| 818 | the change between versions. |
Ilya Sherman | 8f0034a | 2020-07-22 22:06:34 | [diff] [blame] | 819 | |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 820 | ### Patterned Histograms |
Ilya Sherman | f54104b | 2017-07-12 23:45:47 | [diff] [blame] | 821 | |
| 822 | It is sometimes useful to record several closely related metrics, which measure |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 823 | the same type of data, with some minor variations. You can declare the metadata |
| 824 | for these concisely using patterned histograms. For example: |
Ilya Sherman | f54104b | 2017-07-12 23:45:47 | [diff] [blame] | 825 | |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 826 | ```xml |
Jared Saul | 73a9daaf | 2021-05-04 15:33:02 | [diff] [blame] | 827 | <histogram name="Pokemon.{Character}.EfficacyAgainst{OpponentType}" |
Robert Kaplow | e1430ce | 2021-03-25 19:02:18 | [diff] [blame] | 828 | units="multiplier" expires_after="M95"> |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 829 | <owner>[email protected]</owner> |
| 830 | <owner>[email protected]</owner> |
| 831 | <summary> |
| 832 | The efficacy multiplier for {Character} against an opponent of |
| 833 | {OpponentType} type. |
| 834 | </summary> |
| 835 | <token key="Character"> |
| 836 | <variant name="Bulbasaur"/> |
| 837 | <variant name="Charizard"/> |
| 838 | <variant name="Mewtwo"/> |
| 839 | </token> |
| 840 | <token key="OpponentType"> |
| 841 | <variant name="Dragon" summary="dragon"/> |
| 842 | <variant name="Flying" summary="flappity-flap"/> |
| 843 | <variant name="Psychic" summary="psychic"/> |
| 844 | <variant name="Water" summary="water"/> |
| 845 | </token> |
| 846 | </histogram> |
| 847 | ``` |
| 848 | |
| 849 | This example defines metadata for 12 (= 3 x 4) concrete histograms, such as |
| 850 | |
| 851 | ```xml |
Robert Kaplow | e1430ce | 2021-03-25 19:02:18 | [diff] [blame] | 852 | <histogram name="Pokemon.Charizard.EfficacyAgainstWater" |
| 853 | units="multiplier" expires_after="M95"> |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 854 | <owner>[email protected]</owner> |
| 855 | <owner>[email protected]</owner> |
| 856 | <summary> |
| 857 | The efficacy multiplier for Charizard against an opponent of water type. |
| 858 | </summary> |
| 859 | </histogram> |
| 860 | ``` |
| 861 | |
James Lee | 53e80dc | 2024-04-19 11:31:06 | [diff] [blame] | 862 | Each token `<variant>` defines what text should be substituted for it, |
| 863 | both in the histogram name and in the summary text. The name part gets |
| 864 | substituted into the histogram name; the summary part gets substituted in |
Mark Pearson | 268ea6b | 2021-09-28 00:44:45 | [diff] [blame] | 865 | the summary field (the histogram description). As shorthand, a |
| 866 | `<variant>` that omits the `summary` attribute substitutes the value of |
| 867 | the `name` attribute in the histogram's `<summary>` text as well. |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 868 | |
| 869 | *** promo |
| 870 | Tip: You can declare an optional token by listing an empty name: `<variant |
| 871 | name="" summary="aggregated across all breakdowns"/>`. This can be useful when |
| 872 | recording a "parent" histogram that aggregates across a set of breakdowns. |
| 873 | *** |
| 874 | |
| 875 | You can use the `<variants>` tag to define a set of `<variant>`s out-of-line. |
| 876 | This is useful for token substitutions that are shared among multiple families |
Ariel Zhang | 6adadaf | 2023-06-07 14:55:15 | [diff] [blame] | 877 | of histograms within the same file. See |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 878 | [histograms.xml](https://source.chromium.org/search?q=file:histograms.xml%20%3Cvariants) |
| 879 | for examples. |
| 880 | |
Joe Mason | b468cc4 | 2022-06-21 18:02:16 | [diff] [blame] | 881 | *** promo |
| 882 | Warning: The `name` attribute of the `<variants>` tag is globally scoped, so |
Ariel Zhang | 6adadaf | 2023-06-07 14:55:15 | [diff] [blame] | 883 | use detailed names to avoid collisions. The `<variants>` defined should only |
| 884 | be used within the file. |
Joe Mason | b468cc4 | 2022-06-21 18:02:16 | [diff] [blame] | 885 | *** |
| 886 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 887 | By default, a `<variant>` inherits the owners declared for the patterned |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 888 | histogram. Each variant can optionally override the inherited list with custom |
| 889 | owners: |
| 890 | ```xml |
| 891 | <variant name="SubteamBreakdown" ...> |
| 892 | <owner>[email protected]</owner> |
| 893 | <owner>[email protected]</owner> |
| 894 | </variant> |
| 895 | ``` |
Mark Pearson | a010912 | 2018-05-30 18:23:05 | [diff] [blame] | 896 | |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 897 | *** promo |
Oksana Zhuravlova | 5242ad2 | 2021-02-19 00:14:20 | [diff] [blame] | 898 | Tip: You can run `print_expanded_histograms.py --pattern=` to show all generated |
Weilun Shi | bac61d9d3 | 2020-11-12 02:40:26 | [diff] [blame] | 899 | histograms by patterned histograms or histogram suffixes including their |
| 900 | summaries and owners. For example, this can be run (from the repo root) as: |
| 901 | ``` |
Oksana Zhuravlova | 5242ad2 | 2021-02-19 00:14:20 | [diff] [blame] | 902 | ./tools/metrics/histograms/print_expanded_histograms.py --pattern=^UMA.A.B |
Weilun Shi | bac61d9d3 | 2020-11-12 02:40:26 | [diff] [blame] | 903 | ``` |
| 904 | *** |
| 905 | |
| 906 | *** promo |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 907 | Tip: You can run `print_histogram_names.py --diff` to enumerate all the |
| 908 | histogram names that are generated by a particular CL. For example, this can be |
| 909 | run (from the repo root) as: |
Charlie Harrison | 90407d9 | 2020-05-19 23:57:32 | [diff] [blame] | 910 | ``` |
Egor Pasko | 5ec32b7 | 2021-07-23 14:34:22 | [diff] [blame] | 911 | ./tools/metrics/histograms/print_histogram_names.py --diff origin/main |
Charlie Harrison | 90407d9 | 2020-05-19 23:57:32 | [diff] [blame] | 912 | ``` |
Ilya Sherman | 9e22dea | 2020-10-05 22:32:36 | [diff] [blame] | 913 | *** |
| 914 | |
| 915 | For documentation about the `<histogram_suffixes>` syntax, which is deprecated, |
| 916 | see |
| 917 | https://chromium.googlesource.com/chromium/src/+/refs/tags/87.0.4270.1/tools/metrics/histograms/one-pager.md#histogram-suffixes-deprecated-in-favor-of-pattern-histograms |
Charlie Harrison | 90407d9 | 2020-05-19 23:57:32 | [diff] [blame] | 918 | |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 919 | ## When To Use Sparse Histograms |
| 920 | |
Caitlin Fischer | b5e9435 | 2020-10-27 17:34:50 | [diff] [blame] | 921 | Sparse histograms are well-suited for recording counts of exact sample values |
| 922 | that are sparsely distributed over a large range. They can be used with enums |
Ilya Sherman | 1eee82c4c | 2017-12-08 01:22:19 | [diff] [blame] | 923 | as well as regular integer values. It is often valuable to provide labels in |
| 924 | [enums.xml](./enums.xml). |
mpearson | 2b5f7e0 | 2016-10-03 21:27:03 | [diff] [blame] | 925 | |
| 926 | The implementation uses a lock and a map, whereas other histogram types use a |
| 927 | vector and no lock. It is thus more costly to add values to, and each value |
| 928 | stored has more overhead, compared to the other histogram types. However it |
| 929 | may be more efficient in memory if the total number of sample values is small |
| 930 | compared to the range of their values. |
| 931 | |
Mark Pearson | ed73f1f | 2019-03-22 18:00:12 | [diff] [blame] | 932 | Please talk with the metrics team if there are more than a thousand possible |
| 933 | different values that you could emit. |
| 934 | |
rkaplow | 6dfcb89 | 2016-10-04 14:04:27 | [diff] [blame] | 935 | For more information, see [sparse_histograms.h](https://cs.chromium.org/chromium/src/base/metrics/sparse_histogram.h). |
Caitlin Fischer | b466a04 | 2019-07-31 21:41:46 | [diff] [blame] | 936 | |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 937 | |
Robert Kaplow | 6be6fbf | 2021-04-19 17:30:38 | [diff] [blame] | 938 | # Becoming a Metrics Reviewer |
Caitlin Fischer | b466a04 | 2019-07-31 21:41:46 | [diff] [blame] | 939 | |
Jared Saul | 73a9daaf | 2021-05-04 15:33:02 | [diff] [blame] | 940 | Any Chromium committer who is also a Google employee is eligible to become a |
| 941 | metrics reviewer. Please follow the instructions at [go/reviewing-metrics](https://goto.google.com/reviewing-metrics). |
| 942 | This consists of reviewing our training materials and passing an informational |
| 943 | quiz. Since metrics have a direct impact on internal systems and have privacy |
Robert Kaplow | 6be6fbf | 2021-04-19 17:30:38 | [diff] [blame] | 944 | considerations, we're currently only adding Googlers into this program. |
| 945 | |
| 946 | |
| 947 | # Reviewing Metrics CLs |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 948 | |
Robert Kaplow | cbc6fd6 | 2021-03-19 15:11:40 | [diff] [blame] | 949 | If you are a metric OWNER, you have the serious responsibility of ensuring |
| 950 | Chrome's data collection is following best practices. If there's any concern |
| 951 | about an incoming metrics changelist, please escalate by assigning to |
| 952 | [email protected]. |
| 953 | |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 954 | When reviewing metrics CLs, look at the following, listed in approximate order |
| 955 | of importance: |
| 956 | |
Robert Kaplow | 6be6fbf | 2021-04-19 17:30:38 | [diff] [blame] | 957 | ## Privacy |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 958 | |
| 959 | Does anything tickle your privacy senses? (Googlers, see |
| 960 | [go/uma-privacy](https://goto.google.com/uma-privacy) for guidelines.) |
| 961 | |
| 962 | **Please escalate if there's any doubt!** |
| 963 | |
Robert Kaplow | 6be6fbf | 2021-04-19 17:30:38 | [diff] [blame] | 964 | ## Clarity |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 965 | |
| 966 | Is the metadata clear enough for [all Chromies](#Understandable-to-Everyone) to |
| 967 | understand what the metric is recording? Consider the histogram name, |
| 968 | description, units, enum labels, etc. |
| 969 | |
| 970 | It's really common for developers to forget to list [when the metric is |
| 971 | recorded](#State-When-It-Is-Recorded). This is particularly important context, |
| 972 | so please remind developers to clearly document it. |
| 973 | |
| 974 | Note: Clarity is a bit less important for very niche metrics used only by a |
| 975 | couple of engineers. However, it's hard to assess the metric design and |
| 976 | correctness if the metadata is especially unclear. |
| 977 | |
Robert Kaplow | 6be6fbf | 2021-04-19 17:30:38 | [diff] [blame] | 978 | ## Metric design |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 979 | |
| 980 | * Does the metric definition make sense? |
| 981 | * Will the resulting data be interpretable at analysis time? |
| 982 | |
Robert Kaplow | 6be6fbf | 2021-04-19 17:30:38 | [diff] [blame] | 983 | ## Correctness |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 984 | |
| 985 | Is the histogram being recorded correctly? |
| 986 | |
| 987 | * Does the bucket layout look reasonable? |
| 988 | |
| 989 | * The metrics APIs like base::UmaHistogram* have some sharp edges, |
| 990 | especially for the APIs that require specifying the number of |
| 991 | buckets. Check for off-by-one errors and unused buckets. |
| 992 | |
| 993 | * Is the bucket layout efficient? Typically, push back if there are >50 |
| 994 | buckets -- this can be ok in some cases, but make sure that the CL author |
| 995 | has consciously considered the tradeoffs here and is making a reasonable |
| 996 | choice. |
| 997 | |
| 998 | * For timing metrics, do the min and max bounds make sense for the duration |
| 999 | that is being measured? |
| 1000 | |
| 1001 | * The base::UmaHistogram* functions are |
| 1002 | [generally preferred](#Coding-Emitting-to-Histograms) over the |
| 1003 | UMA_HISTOGRAM_* macros. If using the macros, remember that names must be |
| 1004 | runtime constants! |
| 1005 | |
| 1006 | Also, related to [clarity](#Clarity): Does the client logic correctly implement |
| 1007 | the metric described in the XML metadata? Some common errors to watch out for: |
| 1008 | |
| 1009 | * The metric is only emitted within an if-stmt (e.g., only if some data is |
| 1010 | available) and this restriction isn't mentioned in the metadata description. |
| 1011 | |
| 1012 | * The metric description states that it's recorded when X happens, but it's |
| 1013 | actually recorded when X is scheduled to occur, or only emitted when X |
| 1014 | succeeds (but omitted on failure), etc. |
| 1015 | |
| 1016 | When the metadata and the client logic do not match, the appropriate solution |
| 1017 | might be to update the metadata, or it might be to update the client |
| 1018 | logic. Guide this decision by considering what data will be more easily |
| 1019 | interpretable and what data will have hidden surprises/gotchas. |
| 1020 | |
Robert Kaplow | 6be6fbf | 2021-04-19 17:30:38 | [diff] [blame] | 1021 | ## Sustainability |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 1022 | |
Robert Kaplow | cd6e042 | 2021-04-07 21:58:53 | [diff] [blame] | 1023 | * Is the CL adding a reasonable number of metrics/buckets? |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 1024 | * When reviewing a CL that is trying to add many metrics at once, guide the CL |
| 1025 | author toward an appropriate solution for their needs. For example, |
| 1026 | multidimensional metrics can be recorded via UKM, and we are currently |
Robert Kaplow | cd6e042 | 2021-04-07 21:58:53 | [diff] [blame] | 1027 | building support for structured metrics in UMA. |
| 1028 | * There's no hard rule, but anything above 20 separate histograms should be |
| 1029 | escalated by being assigned to [email protected]. |
| 1030 | * Similarly, any histogram with more than 100 possible buckets should be |
| 1031 | escalated by being assigned to [email protected]. |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 1032 | |
| 1033 | * Are expiry dates being set |
| 1034 | [appropriately](#How-to-choose-expiry-for-histograms)? |
| 1035 | |
Robert Kaplow | 6be6fbf | 2021-04-19 17:30:38 | [diff] [blame] | 1036 | ## Everything Else! |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 1037 | |
| 1038 | This document describes many other nuances that are important for defining and |
| 1039 | recording useful metrics. Check CLs for these other types of issues as well. |
| 1040 | |
| 1041 | And, as you would with a language style guide, periodically re-review the doc to |
| 1042 | stay up to date on the details. |
| 1043 | |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 1044 | |
Robert Kaplow | 6be6fbf | 2021-04-19 17:30:38 | [diff] [blame] | 1045 | # Team Documentation |
Ilya Sherman | f64bca25 | 2020-11-10 23:16:24 | [diff] [blame] | 1046 | |
Caitlin Fischer | b466a04 | 2019-07-31 21:41:46 | [diff] [blame] | 1047 | |
| 1048 | ## Processing histograms.xml |
| 1049 | |
| 1050 | When working with histograms.xml, verify whether you require fully expanded |
| 1051 | OWNERS files. Many scripts in this directory process histograms.xml, and |
| 1052 | sometimes OWNERS file paths are expanded and other times they are not. OWNERS |
| 1053 | paths are expanded when scripts make use of merge_xml's function MergeFiles; |
| 1054 | otherwise, they are not. |