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