blob: 4ad9b88ca4187c6b2242ba91ab3232669f4d32e3 [file] [log] [blame] [view]
mpearson2b5f7e02016-10-03 21:27:031# Histogram Guidelines
2
3This document gives the best practices on how to use histograms in code and how
rkaplow6dfcb892016-10-04 14:04:274to document the histograms for the dashboards. There are three general types
vapier52b9aba2016-12-14 06:09:255of histograms: enumerated histograms, count histograms (for arbitrary numbers),
6and sparse histograms (for anything when the precision is important over a wide
7range and/or the range is not possible to specify a priori).
mpearson2b5f7e02016-10-03 21:27:038
9[TOC]
10
Mark Pearsonb1d608d2018-06-05 19:59:4411## Naming Your Histogram
12
13Histogram names should be in the form Group.Name or Group.Subgroup.Name,
14etc., where each group organizes related histograms.
15
Mark Pearson4c4bc972018-05-16 20:01:0616## Coding (Emitting to Histograms)
17
Daniel Cheng01cd75932020-02-06 16:43:4518Prefer the helper functions defined in
Mark Pearsoned73f1f2019-03-22 18:00:1219[histogram_functions.h](https://cs.chromium.org/chromium/src/base/metrics/histogram_functions.h).
Daniel Cheng01cd75932020-02-06 16:43:4520These functions take a lock and perform a map lookup, but the overhead is
21generally insignificant. However, when recording metrics on the critical path
22(e.g. called in a loop or logged multiple times per second), use the macros in
23[histogram_macros.h](https://cs.chromium.org/chromium/src/base/metrics/histogram_macros.h)
24instead. These macros cache a pointer to the histogram object for efficiency,
25though this comes at the cost of increased binary size: 130 bytes/macro usage
26sounds small but quickly adds up.
Mark Pearson159c38972018-06-05 19:44:0827
Mark Pearson4c4bc972018-05-16 20:01:0628### Don't Use the Same Histogram Logging Call in Multiple Places
29
30These logging macros and functions have long names and sometimes include extra
31parameters (defining the number of buckets for example). Use a helper function
32if possible. This leads to shorter, more readable code that's also more
33resilient to problems that could be introduced when making changes. (One could,
34for example, erroneously change the bucketing of the histogram in one call but
35not the other.)
36
37### Use Fixed Strings When Using Histogram Macros
38
39When using histogram macros (calls such as `UMA_HISTOGRAM_ENUMERATION`), you're
Victor-Gabriel Savub2afb6f42019-10-23 07:28:2340not allowed to construct your string dynamically so that it can vary at a
Mark Pearson74c53212019-03-08 00:34:0841callsite. At a given callsite (preferably you have only one), the string
42should be the same every time the macro is called. If you need to use dynamic
43names, use the functions in histogram_functions.h instead of the macros.
Mark Pearson4c4bc972018-05-16 20:01:0644
45### Don't Use Same String in Multiple Places
46
47If you must use the histogram name in multiple places, use a compile-time
48constant of appropriate scope that can be referenced everywhere. Using inline
49strings in multiple places can lead to errors if you ever need to revise the
50name and you update one one location and forget another.
51
52### Efficiency
53
Mark Pearsoned73f1f2019-03-22 18:00:1254Generally, don't be concerned about the processing cost of emitting to a
55histogram (unless you're using [sparse
56histograms](#When-To-Use-Sparse-Histograms)). The normal histogram code is
57highly optimized. If you are recording to a histogram in particularly
58performance-sensitive or "hot" code, make sure you're using the histogram
59macros; see [reasons above](#Coding-Emitting-to-Histograms).
Mark Pearson4c4bc972018-05-16 20:01:0660
61## Picking Your Histogram Type
mpearson2b5f7e02016-10-03 21:27:0362
63### Directly Measure What You Want
64
65Measure exactly what you want, whether that's time used for a function call,
66number of bytes transmitted to fetch a page, number of items in a list, etc.
67Do not assume you can calculate what you want from other histograms. Most of
68the ways to do this are incorrect. For example, if you want to know the time
69taken by a function that all it does is call two other functions, both of which
70are have histogram logging, you might think you can simply add up those
71the histograms for those functions to get the total time. This is wrong.
72If we knew which emissions came from which calls, we could pair them up and
73derive the total time for the function. However, histograms entries do not
74come with timestamps--we pair them up appropriately. If you simply add up the
75two histograms to get the total histogram, you're implicitly assuming those
76values are independent, which may not be the case. Directly measure what you
77care about; don't try to derive it from other data.
78
mpearson2b5f7e02016-10-03 21:27:0379### Enum Histograms
80
81Enumerated histogram are most appropriate when you have a list of connected /
82related states that should be analyzed jointly. For example, the set of
83actions that can be done on the New Tab Page (use the omnibox, click a most
84visited tile, click a bookmark, etc.) would make a good enumerated histogram.
85If the total count of your histogram (i.e. the sum across all buckets) is
86something meaningful--as it is in this example--that is generally a good sign.
87However, the total count does not have to be meaningful for an enum histogram
88to still be the right choice.
89
Mark Pearsona768d0222019-03-20 02:16:0090Enumerated histograms are also appropriate for counting events. Use a simple
91boolean histogram. It's okay if you only log to one bucket (say, `true`).
92It's usually best (though not necessary), if you have a comparison point in
93the same histogram. For example, if you want to count pages opened from the
94history page, it might be a useful comparison to have the same histogram
95record the number of times the history page was opened.
96
Daniel Cheng914170d22019-05-08 09:46:3297If only a few buckets will be emitted to, consider using a [sparse
Mark Pearson4d0b4632017-10-04 21:58:4898histogram](#When-To-Use-Sparse-Histograms).
99
Daniel Cheng914170d22019-05-08 09:46:32100#### Requirements
101
102Enums logged in histograms must:
103
104- be prefixed with the comment:
105 ```c++
106 // These values are persisted to logs. Entries should not be renumbered and
107 // numeric values should never be reused.
108 ```
109- be numbered starting from `0`. Note this bullet point does *not* apply for
110 enums logged with sparse histograms.
111- have enumerators with explicit values (`= 0`, `= 1`, `= 2`), to make it clear
112 that the actual values are important. This also makes it easy to match the
113 values between the C++/Java definition and [histograms.xml](./histograms.xml).
114- not renumber or reuse enumerator values. When adding a new enumerator, append
115 the new enumerator to the end. When removing an unused enumerator, comment it
116 out, making it clear the value was previously used.
117
118If your enum histogram has a catch-all / miscellaneous bucket, put that bucket
119first (`= 0`). This will make the bucket easy to find on the dashboard if
120additional buckets are added later.
121
122#### Usage
123
124Define an `enum class` with a `kMaxValue` enumerator:
125
Steven Holteecf841d2018-08-10 00:53:34126```c++
Daniel Chengcda1df5b2018-03-30 21:30:16127enum class NewTabPageAction {
128 kUseOmnibox = 0,
129 kClickTitle = 1,
Daniel Cheng914170d22019-05-08 09:46:32130 // kUseSearchbox = 2, // no longer used, combined into omnibox
131 kOpenBookmark = 3,
Daniel Chengcda1df5b2018-03-30 21:30:16132 kMaxValue = kOpenBookmark,
133};
134```
Daniel Chengcda1df5b2018-03-30 21:30:16135
Daniel Cheng914170d22019-05-08 09:46:32136`kMaxValue` is a special enumerator that must share the highest enumerator
137value, typically done by aliasing it with the enumerator with the highest
138value: clang automatically checks that `kMaxValue` is correctly set for `enum
139class`.
140
141The histogram helpers use the `kMaxValue` convention, and the enum may be
142logged with:
143
144```c++
Daniel Chengcda1df5b2018-03-30 21:30:16145UMA_HISTOGRAM_ENUMERATION("NewTabPageAction", action);
146```
Daniel Chengcda1df5b2018-03-30 21:30:16147
Daniel Cheng914170d22019-05-08 09:46:32148or:
149
Steven Holteecf841d2018-08-10 00:53:34150```c++
Daniel Cheng914170d22019-05-08 09:46:32151UmaHistogramEnumeration("NewTabPageAction", action);
Daniel Chengcda1df5b2018-03-30 21:30:16152```
Steven Holteecf841d2018-08-10 00:53:34153
Daniel Cheng914170d22019-05-08 09:46:32154#### Legacy Enums
155
156**Note: this method of defining histogram enums is deprecated. Do not use this
157for new enums.**
158
159Many legacy enums define a `kCount` sentinel, reying on the compiler to
160automatically update it when new entries are added:
161
Steven Holteecf841d2018-08-10 00:53:34162```c++
Daniel Chengcda1df5b2018-03-30 21:30:16163enum class NewTabPageAction {
164 kUseOmnibox = 0,
165 kClickTitle = 1,
Daniel Cheng914170d22019-05-08 09:46:32166 // kUseSearchbox = 2, // no longer used, combined into omnibox
167 kOpenBookmark = 3,
Daniel Chengcda1df5b2018-03-30 21:30:16168 kCount,
169};
Daniel Cheng914170d22019-05-08 09:46:32170```
Steven Holteecf841d2018-08-10 00:53:34171
Daniel Cheng914170d22019-05-08 09:46:32172These enums must be recorded using the legacy helpers:
173
174```c++
Daniel Chengcda1df5b2018-03-30 21:30:16175UMA_HISTOGRAM_ENUMERATION("NewTabPageAction", action, NewTabPageAction::kCount);
176```
177
Daniel Cheng914170d22019-05-08 09:46:32178or:
179
180```c++
181UmaHistogramEnumeration("NewTabPageAction", action, NewTabPageAction::kCount);
182```
mpearsonb36013be2017-02-10 20:10:54183
Matt Giucaf3e0e2532017-10-03 23:07:52184### Flag Histograms
185
186When adding a new flag in
187[about_flags.cc](../../../chrome/browser/about_flags.cc), you need to add a
188corresponding entry to [enums.xml](./enums.xml). This will be automatically
189verified by the `AboutFlagsHistogramTest` unit test.
190
191To add a new entry:
192
1931. Edit [enums.xml](./enums.xml), adding the feature to the `LoginCustomFlags`
Brett Wilsonf4d58772017-10-30 21:37:57194 enum section, with any unique value (just make one up, although whatever it
Dave Schuyler988e1a472018-01-04 02:21:11195 is needs to appear in sorted order; `pretty_print.py` will do this for you).
Matt Giucaf3e0e2532017-10-03 23:07:521962. Build `unit_tests`, then run `unit_tests
197 --gtest_filter='AboutFlagsHistogramTest.*'` to compute the correct value.
1983. Update the entry in [enums.xml](./enums.xml) with the correct value, and move
Brett Wilsonf4d58772017-10-30 21:37:57199 it so the list is sorted by value (`pretty_print.py` will do this for you).
Matt Giucaf3e0e2532017-10-03 23:07:522004. Re-run the test to ensure the value and ordering are correct.
201
202You can also use `tools/metrics/histograms/validate_format.py` to check the
203ordering (but not that the value is correct).
204
205Don't remove entries when removing a flag; they are still used to decode data
206from previous Chrome versions.
207
mpearson2b5f7e02016-10-03 21:27:03208### Count Histograms
209
210[histogram_macros.h](https://cs.chromium.org/chromium/src/base/metrics/histogram_macros.h)
211provides macros for some common count types such as memory or elapsed time, in
212addition to general count macros. These have reasonable default values; you
213will not often need to choose number of buckets or histogram min. You still
214will need to choose the histogram max (use the advice below).
215
216If none of the default macros work well for you, please thoughtfully choose
217a min, max, and bucket count for your histogram using the advice below.
218
rkaplow6dfcb892016-10-04 14:04:27219#### Count Histograms: Choosing Min and Max
mpearson2b5f7e02016-10-03 21:27:03220
221For histogram max, choose a value so that very few emission to the histogram
222will exceed the max. If many emissions hit the max, it can be difficult to
223compute statistics such as average. One rule of thumb is at most 1% of samples
224should be in the overflow bucket. This allows analysis of the 99th percentile.
vapier52b9aba2016-12-14 06:09:25225Err on the side of too large a range versus too short a range. (Remember that
226if you choose poorly, you'll have to wait for another release cycle to fix it.)
mpearson2b5f7e02016-10-03 21:27:03227
228For histogram min, if you care about all possible values (zero and above),
229choose a min of 1. (All histograms have an underflow bucket; emitted zeros
230will go there. That's why a min of 1 is appropriate.) Otherwise, choose the
231min appropriate for your particular situation.
232
rkaplow6dfcb892016-10-04 14:04:27233#### Count Histograms: Choosing Number of Buckets
mpearson2b5f7e02016-10-03 21:27:03234
235Choose the smallest number of buckets that will get you the granularity you
236need. By default count histograms bucket sizes scale exponentially so you can
rkaplow6dfcb892016-10-04 14:04:27237get fine granularity when the numbers are small yet still reasonable resolution
rkaplow8a62ef62016-10-06 14:42:34238for larger numbers. The macros default to 50 buckets (or 100 buckets for
239histograms with wide ranges) which is appropriate for most purposes. Because
240histograms pre-allocate all the buckets, the number of buckets selected
241directly dictate how much memory is used. Do not exceed 100 buckets without
Mark Pearsonf0312e92019-09-26 18:56:22242good reason (and consider whether [sparse
243histograms](#When-To-Use-Sparse-Histograms) might work better for you in that
244case--they do not pre-allocate their buckets).
rkaplow8a62ef62016-10-06 14:42:34245
Mark Pearson6be2f35c2018-08-14 07:06:02246### Timing Histograms
247
248You can easily emit a time duration (time delta) using UMA_HISTOGRAM_TIMES,
249UMA_HISTOGRAM_MEDIUM_TIMES, and UMA_HISTOGRAM_LONG_TIMES macros, and their
250friends, as well as helpers such as SCOPED_UMA_HISTOGRAM_TIMER. Many timing
251histograms are used for performance monitoring; if this is the case for you,
252please read [this document about how to structure timing histograms to make
253them more useful and
Paul Jensen5107d9c2018-10-22 22:24:06254actionable](https://chromium.googlesource.com/chromium/src/+/lkgr/docs/speed/diagnostic_metrics.md).
Mark Pearson6be2f35c2018-08-14 07:06:02255
Mark Pearson49928ec2018-06-05 20:15:49256### Percentage or Ratio Histograms
257
258You can easily emit a percentage histogram using the
259UMA_HISTOGRAM_PERCENTAGE macro provided in
260[histogram_macros.h](https://cs.chromium.org/chromium/src/base/metrics/histogram_macros.h).
261You can also easily emit any ratio as a linear histogram (for equally
262sized buckets).
263
264For such histograms, you should think carefully about _when_ the values are
265emitted. Normally, you should emit values periodically at a set time interval,
266such as every 5 minutes. Conversely, we strongly discourage emitting values
267based on event triggers. For example, we do not recommend recording a ratio
268at the end of a video playback.
269
270Why? You typically cannot make decisions based on histograms whose values are
271recorded in response to an event, because such metrics can conflate heavy usage
272with light usage. It's easier to reason about metrics that route around this
273source of bias.
274
275Many developers have been bitten by this. For example, it was previously common
276to emit an actions-per-minute ratio whenever Chrome was backgrounded.
277Precisely, these metrics computed the number of uses of a particular action
278during a Chrome session, divided by length of time Chrome had been open.
279Sometimes, the recorded rate was based on a short interaction with Chrome – a
280few seconds or a minute. Other times, the recorded rate was based on a long
281interaction, tens of minutes or hours. These two situations are
282indistinguishable in the UMA logs – the recorded values can be identical.
283
284This inability to distinguish these two qualitatively different settings make
285such histograms effectively uninterpretable and not actionable. Emitting at a
286regular interval avoids the issue. Each value will represent the same amount of
287time (e.g., one minute of video playback).
288
rkaplow8a62ef62016-10-06 14:42:34289### Local Histograms
290
Gayane Petrosyana6ee443c2018-05-17 21:39:54291Histograms can be added via [Local macros](https://codesearch.chromium.org/chromium/src/base/metrics/histogram_macros_local.h).
292These will still record locally, but will not be uploaded to UMA and will
293therefore not be available for analysis. This can be useful for metrics only
294needed for local debugging. We don't recommend using local histograms outside
295of that scenario.
rkaplow8a62ef62016-10-06 14:42:34296
297### Multidimensional Histograms
298
299It is common to be interested in logging multidimensional data - where multiple
300pieces of information need to be logged together. For example, a developer may
301be interested in the counts of features X and Y based on whether a user is in
302state A or B. In this case, they want to know the count of X under state A,
303as well as the other three permutations.
304
305There is no general purpose solution for this type of analysis. We suggest
306using the workaround of using an enum of length MxN, where you log each unique
307pair {state, feature} as a separate entry in the same enum. If this causes a
Gayane Petrosyana6ee443c2018-05-17 21:39:54308large explosion in data (i.e. >100 enum entries), a [sparse histogram](#When-To-Use-Sparse-Histograms)
309may be appropriate. If you are unsure of the best way to proceed, please
310contact someone from the OWNERS file.
311
312## Histogram Expiry
313
314Histogram expiry is specified by **'expires_after'** attribute in histogram
315descriptions in histograms.xml. The attribute can be specified as date in
Brian Whitefa0a3fa2019-05-13 16:58:11316**YYYY-MM-DD** format or as Chrome milestone in **M**\*(e.g. M68) format. In the
317latter case, the actual expiry date is about 12 weeks after that branch is cut,
318or basically when it is replaced on the "stable" channel by the following
319release.
320
321After a histogram expires, it will cease to be displayed on the dashboard.
322However, the client may continue to send data for that histogram for some time
323after the official expiry date so simply bumping the 'expires_after' date in
324HEAD may be sufficient to resurrect it without any discontinuity. If too much
325time has passed and the client is no longer sending data, it can be re-enabled
326via Finch: see [Expired Histogram Whitelist](#Expired-histogram-whitelist).
327
328Once a histogram has expired, the code to record it becomes dead code and should
329be removed from the codebase along with marking the histogram definition as
330obsolete.
Gayane Petrosyana6ee443c2018-05-17 21:39:54331
Brian White8614f812019-02-07 21:07:01332In **rare** cases, the expiry can be set to "never". This is used to denote
333metrics of critical importance that are, typically, used for other reports.
334For example, all metrics of the "[heartbeat](https://uma.googleplex.com/p/chrome/variations)"
335are set to never expire. All metrics that never expire must have an XML
336comment describing why so that it can be audited in the future.
337
338```
339<!-- expires-never: "heartbeat" metric (internal: go/uma-heartbeats) -->
340```
341
Gayane Petrosyana6ee443c2018-05-17 21:39:54342For all the new histograms the use of expiry attribute will be strongly
343encouraged and enforced by Chrome metrics team through reviews.
344
345#### How to choose expiry for histograms
346
Ilya Sherman67418ea2019-11-27 01:28:23347If you are adding a histogram that will be used to evaluate a feature launch,
348set an expiry date consistent with the expected feature launch date. Otherwise,
349we recommend choosing 3-6 months.
Gayane Petrosyana6ee443c2018-05-17 21:39:54350
Ilya Sherman67418ea2019-11-27 01:28:23351Here are some guidelines for common scenarios:
Gayane Petrosyana6ee443c2018-05-17 21:39:54352
Ilya Sherman67418ea2019-11-27 01:28:23353* If the listed owner moved to different project, find a new owner.
354* If neither the owner nor the team uses the histogram, remove it.
355* If the histogram is not in use now, but might be useful in the far future,
356 remove it.
357* If the histogram is not in use now, but might be useful in the near
358 future, pick ~3 months or ~2 milestones ahead.
359* If the histogram is actively in use now and useful for a short term, pick
360 3-6 month or 2-4 milestones ahead.
361* If the histogram is actively in use and seems useful for an indefinite time,
362 pick 1 year.
363
364We also have a tool that automatically extends expiry dates. The 80% more
365frequently accessed histograms are pushed out every Tuesday, to 6 months from
366the date of the run. Googlers can view the [design
367doc](https://docs.google.com/document/d/1IEAeBF9UnYQMDfyh2gdvE7WlUKsfIXIZUw7qNoU89A4).
Gayane Petrosyana6ee443c2018-05-17 21:39:54368
369### Expired histogram notifier
370
371Expired histogram notifier will notify owners in advance by creating crbugs so
372that the owners can extend the lifetime of the histogram if needed or deprecate
373it. It will regularly check all the histograms in histograms.xml and will
374determine expired histograms or histograms expiring soon. Based on that it will
375create or update crbugs that will be assigned to histogram owners.
376
377### Expired histogram whitelist
378
379If a histogram expires but turns out to be useful, you can add histogram name
380to the whitelist until the updated expiration date reaches to the stable
381channel. For adding histogram to the whitelist, see internal documentation
382[Histogram Expiry](https://goto.google.com/histogram-expiry-gdoc)
mpearson2b5f7e02016-10-03 21:27:03383
mpearson72a5c91392017-05-09 22:49:44384## Testing
mpearson2b5f7e02016-10-03 21:27:03385
vapier52b9aba2016-12-14 06:09:25386Test your histograms using `chrome://histograms`. Make sure they're being
rkaplow6dfcb892016-10-04 14:04:27387emitted to when you expect and not emitted to at other times. Also check that
388the values emitted to are correct. Finally, for count histograms, make sure
389that buckets capture enough precision for your needs over the range.
mpearson2b5f7e02016-10-03 21:27:03390
Ivan Sandrk8ffc5832018-07-09 12:34:58391Pro tip: You can filter the set of histograms shown on `chrome://histograms` by
392specifying a prefix. For example, `chrome://histograms/Extensions.Load` will
393show only histograms whose names match the pattern "Extensions.Load*".
394
mpearson72a5c91392017-05-09 22:49:44395In addition to testing interactively, you can have unit tests examine the
Devlin Cronin15291e9c2018-06-07 21:37:48396values emitted to histograms. See [histogram_tester.h](https://cs.chromium.org/chromium/src/base/test/metrics/histogram_tester.h)
mpearson72a5c91392017-05-09 22:49:44397for details.
mpearson2b5f7e02016-10-03 21:27:03398
Mark Pearson4c4bc972018-05-16 20:01:06399## Interpreting the Resulting Data
400
401The top of [go/uma-guide](http://go/uma-guide) has good advice on how to go
402about analyzing and interpreting the results of UMA data uploaded by users. If
403you're reading this page, you've probably just finished adding a histogram to
404the Chromium source code and you're waiting for users to update their version of
405Chrome to a version that includes your code. In this case, the best advice is
406to remind you that users who update frequently / quickly are biased. Best take
407the initial statistics with a grain of salt; they're probably *mostly* right but
408not entirely so.
409
mpearson72a5c91392017-05-09 22:49:44410## Revising Histograms
411
412When changing the semantics of a histogram (when it's emitted, what buckets
413mean, etc.), make it into a new histogram with a new name. Otherwise the
414"Everything" view on the dashboard will be mixing two different
rkaplow8a62ef62016-10-06 14:42:34415interpretations of the data and make no sense.
mpearson2b5f7e02016-10-03 21:27:03416
mpearson72a5c91392017-05-09 22:49:44417## Deleting Histograms
mpearson2b5f7e02016-10-03 21:27:03418
419Please delete the code that emits to histograms that are no longer needed.
Gayane Petrosyana6ee443c2018-05-17 21:39:54420Histograms take up memory. Cleaning up histograms that you no longer care
Mark Pearson2a311c52019-03-19 21:47:01421about is good! But see the note below on
422[Cleaning Up Histogram Entries](#Cleaning-Up-Histogram-Entries).
mpearson2b5f7e02016-10-03 21:27:03423
424## Documenting Histograms
425
Mark Pearson159c38972018-06-05 19:44:08426Document histograms in [histograms.xml](./histograms.xml). There is also a
427[google-internal version of the file](http://go/chrome-histograms-internal) for
428the rare case when the histogram is confidential (added only to Chrome code,
429not Chromium code; or, an accurate description about how to interpret the
430histogram would reveal information about Google's plans).
431
mpearson2b5f7e02016-10-03 21:27:03432### Add Histogram and Documentation in the Same Changelist
433
vapier52b9aba2016-12-14 06:09:25434If possible, please add the [histograms.xml](./histograms.xml) description in
435the same changelist in which you add the histogram-emitting code. This has
436several benefits. One, it sometimes happens that the
437[histograms.xml](./histograms.xml) reviewer has questions or concerns about the
438histogram description that reveal problems with interpretation of the data and
439call for a different recording strategy. Two, it allows the histogram reviewer
440to easily review the emission code to see if it comports with these best
441practices, and to look for other errors.
mpearson2b5f7e02016-10-03 21:27:03442
443### Understandable to Everyone
444
445Histogram descriptions should be roughly understandable to someone not familiar
446with your feature. Please add a sentence or two of background if necessary.
447
448It is good practice to note caveats associated with your histogram in this
449section, such as which platforms are supported (if the set of supported
Gayane Petrosyana6ee443c2018-05-17 21:39:54450platforms is surprising). E.g., a desktop feature that happens not to be
451logged on Mac.
mpearson2b5f7e02016-10-03 21:27:03452
453### State When It Is Recorded
454
455Histogram descriptions should clearly state when the histogram is emitted
456(profile open? network request received? etc.).
457
jsbellda3a66c2017-02-09 21:40:32458### Owners
rkaplow8a62ef62016-10-06 14:42:34459
Caitlin Fischer254a12f72019-07-31 20:57:03460Histograms need owners, who are the experts on the metric and the points of
461contact for any questions or maintenance tasks, such as extending a histogram's
462expiry or deprecating the metric.
rkaplow8a62ef62016-10-06 14:42:34463
Caitlin Fischer254a12f72019-07-31 20:57:03464Histograms must have a primary owner and may have secondary owners. A primary
465owner is an individual, e.g. <owner>[email protected]</owner>, who is
466ultimately responsible for maintaining the metric. Secondary owners may be
467other individuals, team mailing lists, e.g. <owner>[email protected]</owner>,
468or paths to OWNERS files, e.g. <owner>src/directory/OWNERS</owner>.
Mark Pearson74c53212019-03-08 00:34:08469
Caitlin Fischer254a12f72019-07-31 20:57:03470It's a best practice to list multiple owners, so that there's no single point
471of failure for histogram-related questions and maintenance tasks. If you are
472using a metric heavily and understand it intimately, feel free to add yourself
473as an owner. For individuals, @chromium.org email addresses are preferred.
Mark Pearson74c53212019-03-08 00:34:08474
Caitlin Fischer254a12f72019-07-31 20:57:03475Notably, owners are asked to determine whether histograms have outlived their
476usefulness. When a histogram is nearing expiry, a robot will file a reminder
477bug in Monorail. It's important that somebody familiar with the histogram
478notices and triages such bugs!
rkaplow8a62ef62016-10-06 14:42:34479
Mark Pearson2a311c52019-03-19 21:47:01480### Cleaning Up Histogram Entries
mpearson2b5f7e02016-10-03 21:27:03481
Mark Pearson2a311c52019-03-19 21:47:01482Do not delete histograms from histograms.xml. Instead, mark unused
483histograms as obsolete and annotate them with the date or milestone in
484the `<obsolete>` tag entry.
485
486If the histogram used [histogram suffixes](#Histogram-Suffixes), mark
487the suffix entry for the histogram as obsolete as well.
488
489If the histogram is being replaced by a new version:
490
491* Note in the `<obsolete>` message the name of the replacement histogram.
492
493* Make sure the descriptions of the original and replacement histogram
494 are different. It's never appropriate for them to be identical. Either
495 the old description was wrong, and it should be revised to explain what
496 it actually measured, or the old histogram was measuring something not
497 as useful as the replacement, in which case the new histogram is
498 measuring something different and needs to have a new description.
mpearson2b5f7e02016-10-03 21:27:03499
Mark Pearsona0109122018-05-30 18:23:05500A changelist that marks a histogram as obsolete should be reviewed by all
501current owners.
502
mpearson2b5f7e02016-10-03 21:27:03503Deleting histogram entries would be bad if someone to accidentally reused your
504old histogram name and thereby corrupts new data with whatever old data is still
505coming in. It's also useful to keep obsolete histogram descriptions in
vapier52b9aba2016-12-14 06:09:25506[histograms.xml](./histograms.xml) -- that way, if someone is searching for a
507histogram to answer a particular question, they can learn if there was a
508histogram at some point that did so even if it isn't active now.
mpearson2b5f7e02016-10-03 21:27:03509
Ilya Shermanf54104b2017-07-12 23:45:47510### Histogram Suffixes
511
512It is sometimes useful to record several closely related metrics, which measure
513the same type of data, with some minor variations. It is often useful to use one
514or more <histogram_suffixes> elements to save on redundant verbosity
515in [histograms.xml](./histograms.xml). If a root `<histogram>` or a `<suffix>`
516element is used only to construct a partial name, to be completed by further
517suffixes, annotate the element with the attribute `base="true"`. This instructs
518tools not to treat the partial base name as a distinct histogram. Note that
519suffixes can be applied recursively.
520
Mark Pearsona0109122018-05-30 18:23:05521You can also declare ownership of `<histogram_suffixes>`. If there's no owner
522specified, the generated histograms will inherit owners from the parents.
523
Mark Pearson2a311c52019-03-19 21:47:01524As [with histogram entries](#Cleaning-Up-Histogram-Entries), never delete
525histogram suffixes. If the suffix expansion is no longer used, mark it as
526obsolete. You can also mark individual histograms within the suffix as
527obsolete, indicating the expansion for that histogram is obsolete yet the
528expansion for other histograms with the same suffix are not.
529
Ilya Sherman1eee82c4c2017-12-08 01:22:19530### Enum labels
531
532_All_ histograms, including boolean and sparse histograms, may have enum labels
533provided via [enums.xml](./enums.xml). Using labels is encouraged whenever
534labels would be clearer than raw numeric values.
535
mpearson2b5f7e02016-10-03 21:27:03536## When To Use Sparse Histograms
537
538Sparse histograms are well suited for recording counts of exact sample values
Mark Pearson4d0b4632017-10-04 21:58:48539that are sparsely distributed over a large range. They can be used with enums
Ilya Sherman1eee82c4c2017-12-08 01:22:19540as well as regular integer values. It is often valuable to provide labels in
541[enums.xml](./enums.xml).
mpearson2b5f7e02016-10-03 21:27:03542
543The implementation uses a lock and a map, whereas other histogram types use a
544vector and no lock. It is thus more costly to add values to, and each value
545stored has more overhead, compared to the other histogram types. However it
546may be more efficient in memory if the total number of sample values is small
547compared to the range of their values.
548
Mark Pearsoned73f1f2019-03-22 18:00:12549Please talk with the metrics team if there are more than a thousand possible
550different values that you could emit.
551
rkaplow6dfcb892016-10-04 14:04:27552For more information, see [sparse_histograms.h](https://cs.chromium.org/chromium/src/base/metrics/sparse_histogram.h).
Caitlin Fischerb466a042019-07-31 21:41:46553
554# Team Documentation
555
556This section contains useful information for folks on Chrome Metrics.
557
558## Processing histograms.xml
559
560When working with histograms.xml, verify whether you require fully expanded
561OWNERS files. Many scripts in this directory process histograms.xml, and
562sometimes OWNERS file paths are expanded and other times they are not. OWNERS
563paths are expanded when scripts make use of merge_xml's function MergeFiles;
564otherwise, they are not.