Adding a new Telemetry probe

If a user has opted into submitting performance data to Mozilla, the Telemetry system will collect various measures of Firefox performance, hardware, usage and customizations and submit it to Mozilla. The Telemetry data collected by a single client can be examined from the integrated about:telemetry browser page, while the aggregated reports across entire user populations are publicly available at http://telemetry.mozilla.org.

Note: Every new data collection in Firefox now needs a privacy review from a data collection peer. Just NEEDINFO :bsmedberg, :vladan, or :ally. For the vast majority of histogram probes, we'll reply within 24 hours with an r+.

The following sections explain how to add a new measurement to Telemetry.

Telemetry Histograms

Telemetry histograms are the preferred way to track numeric measurements such as timings. Telemetry also tracks more complex data types such as slow SQL statement strings, browser hang stacks and system configurations. Most of these non-histogram measumerements are maintained by the Telemetry team, so they are not covered in this document. If you need to add a non-histogram measurement, contact that team first.

The histogram below is taken from Firefox's about:telemetry page. It shows a histogram used for tracking plugin shutdown times and the data collected over a single Firefox session. The timing data is grouped into buckets where the height of the blue bars represents the number of items in each bucket. The tallest bar, for example, indicates that there were 63 plugin shutdowns lasting between 129ms and 204ms.

Sample Telemetry histogram "PLUGIN_SHUTDOWN_MS" taken from Firefox's about:telemetry page

Choosing a Histogram Type

The first step to adding a new histogram is to choose the histogram type that best represents the data being measured. The sample histogram used above is an "exponential" histogram.

The following types are available:

  • flag: This histogram type allows you to record a single value. This type is useful if you need to track whether a feature was ever used during a Firefox session. You only need to add a single line of code which sets the flag when the feature is used because the histogram is initialized with a default value of false (flag not set).
  • boolean: These histograms only record boolean values. Multiple boolean entries can be recorded in the same histogram during a single browsing session, e.g. if a histogram is measuring user choices in a dialog box with options "Yes" or "No", a new boolean value is added every time the dialog is displayed.
  • count: This histogram type is used when you want to record a count of something. It only stores a single value and it can only be incremented by one with each add/accumulate call.

    Count histograms and keyed histograms are fully supported only in our V4 pipeline tools, such as the unified telemetry (v4) dashboards. These are not fully supported in Telemetry v2 pipeline tools such as the histogram change detector.

  • enumerated: This histogram type is intended for storing "enum" values. An enumerated histogram consists of a fixed number of "buckets", each of which is associated with a consecutive integer value (the bucket's "label"). Each bucket corresponds to an enum value and counts the number of times its particular enum value was recorded. You might use this type of histogram if, for example, you wanted to track the relative popularity of SSL handshake types. Whenever the browser started an SSL handshake, it would record one of a limited number of enum values which uniquely identifies the handshake type.
    Note: Set "n_buckets" to a slightly larger value than needed to allow for new enum values in the future. The current Telemetry server does not support changing histogram declarations after the histogram has already been released. See Miscellaneous section.
  • linear: Linear histograms are similar to enumerated histograms, except each bucket is associated with a range of values instead of a single enum value. The range of values covered by each bucket increases linearly from the previous bucket, e.g. one bucket might count the number of occurrences of values between 0 to 9, the next bucket would cover values 10-19, the next 20-29, etc. This bucket type is useful if there aren't orders of magnitude differences between the minimum and maximum values stored in the histogram, e.g. if the values you are storing are percentages 0-100%.
    NOTE: If you need a linear histogram with buckets < 0, 1, 2 ... N >, then you should declare an enumerated histogram. This restriction was added to prevent developers from making a common off-by-one mistake when specifying the number of buckets in a linear histogram.
  • exponential: Exponential histograms are similar to linear histograms but the range of values covered by each bucket increases exponentially. As an example of its use, consider the timings of an I/O operation whose duration might normally fall in the range of 0ms-50ms but extreme cases might have durations in seconds or minutes. For such measurements, you would want finer-grained bucketing in the normal range but coarser-grained bucketing for the extremely large values. An exponential histogram fits this requirement since it has "narrow" buckets near the minimum value and significantly "wider" buckets near the maximum value.

Keyed Histograms

Keyed histograms are collections of one of the histogram types above, indexed by a string key. This is for example useful when you want to break down certain counts by a name, like how often searches happen with which search engine.

Count histograms and keyed histograms are fully supported only in our V4 pipeline tools, such as the unified telemetry (v4) dashboards. These are not fully supported in Telemetry v2 pipeline tools such as the histogram change detector.

Declaring a Histogram

Histograms should be declared in the toolkit/components/telemetry/Histograms.json file. These declarations are checked for correctness at compile time and used to generate C++ code. It is also possible to create histograms at runtime dynamically, but this is primarily done by add-ons when they create their own histograms in Telemetry.

The following is a sample histogram declaration from Histograms.json for a histogram named MEMORY_RESIDENT which tracks the amount of resident memory used by a process:

"MEMORY_RESIDENT": {
  "alert_emails": ["team@mozilla.xyz"],
  "expires_in_version": "never",
  "kind": "exponential",
  "low": "32 * 1024",
  "high": "1024 * 1024",
  "n_buckets": 50,
  "description": "Resident memory size (KB)"
},

Note that histogram declarations in Histograms.json are converted to C++ code so the right-hand sides of fields can be the names of C++ constants or simple expressions as in the "low" and "high" fields above.

The possible fields in a histogram declaration are:

  • alert_emails: Optional. This field is a list of e-mail addresses that should be notified when the distribution of the histogram changes significantly from one build-id to the other. This can be useful to detect regressions. Note that all alerts will be sent automatically to mozilla.dev.telemetry-alerts.
  • expires_in_version: Required. The version number in which the histogram expires, e.g. "30"; a version number of type "N" and "N.0" is automatically converted to "N.0a1" in order to expire the histogram also in the development channels. A telemetry probe acting on an expired histogram will be considered a non-op. For histograms that never expire the value "never" can be used as in the example above.
  • extended_statistics_ok: Optional. Boolean to opt into the detailed log-stats for exponential histograms. This carries some extra overhead of calculating log() on each accumulate statement -- only use this if the probe isn't used on a hot path.
    • Note: The extended statistics are a leftover from a different Telemetry backend where they were used to calculate a better mean for bucketed measurements in exponential histograms. The extended statistics are not used in the current Telemetry backend, and since it carries an additional overhead, you should not use this field in your histogram declaration.

  • kind: Required. One of the histogram types described in the previous section. Different histogram types require different fields to be present in the declaration.
  • keyed: Optional, boolean, defaults to false. Determines whether this is a keyed histogram.
  • low: Optional, the default value is 0. This field represents the minimum value expected in the histogram. Note that all histograms automatically get a bucket with label "0" for counting values below the "low" value.
  • high: Required for linear and exponential histograms. The maximum value to be stored in a linear or exponential histogram. Any recorded values greater than this maximum will be counted in the last bucket.
  • n_buckets: Required for linear and exponential histograms. The number of buckets in a linear or exponential histogram.
  • n_values: Required for enumerated histograms. Similar to n_buckets, it represent the number of elements in the enum.
  • description: Required. A description of the data tracked by the histogram.
  • cpp_guard: Optional. This field inserts an #ifdef directive around the histogram's C++ declaration. This is typically used for platform-specific histograms, e.g. "cpp_guard": "ANDROID"
  • releaseChannelCollection: Optional. This is one of:
    • "opt-in": (default value) This histogram is submitted by default on pre-release channels; on the release channel only if the user opted into additional data collection
    • "opt-out": This histogram is submitted by default on release and pre-release channels, unless the user opted out.
      Because they are collected by default, opt-out probes need to meet a higher "user benefit" threshold than opt-in probes.

      Make sure you've NEEDINFO'd a privacy peer for ALL new data collection: https://wiki.mozilla.org/Firefox/Data_Collection

Adding a JavaScript Probe

A Telemetry probe is the code that measures and stores values in a histogram. Probes in privileged JavaScript code can make use of the nsITelemetry interface to get references to histogram objects. A new value is recorded in the histogram by calling add on the histogram object:

let histogram = Services.telemetry.getHistogramById("PLACES_AUTOCOMPLETE_1ST_RESULT_TIME_MS");
histogram.add(measuredDuration);

let keyed = Services.telemetry.getKeyedHistogramById("TAG_SEEN_COUNTS");
keyed.add("blink");

Add-ons can dynamically add new histograms to Telemetry:

const ADDON_NAME = "Test addon";
const ADDON_HISTOGRAM_NAME = "my_histogram";

let Telemetry = Services.telemetry;
Telemetry.registerAddonHistogram(ADDON_NAME, ADDON_HISTOGRAM_NAME, Telemetry.HISTOGRAM_LINEAR, 1, 5, 6);

let histogram = Telemetry.getAddonHistogram(ADDON_NAME, ADDON_HISTOGRAM_NAME);
histogram.add(1);

Adding a C++ Probe

Probes in native code can also use the nsITelemetry interface, but the helper functions declared in Telemetry.h are more convenient:

#include "mozilla/Telemetry.h"

/**
 * Adds sample to a histogram defined in Histograms.json
 *
 * @param id - histogram id
 * @param sample - value to record.
 */
void Accumulate(ID id, uint32_t sample);

/**
 * Adds time delta in milliseconds to a histogram defined in Histograms.json
 *
 * @param id - histogram id
 * @param start - start time
 * @param end - end time
 */
void AccumulateTimeDelta(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now());

The histogram names declared in Histograms.json are translated into constants in the mozilla::Telemetry namespace:

mozilla::Telemetry::Accumulate(mozilla::Telemetry::STARTUP_CRASH_DETECTED, true);

The Telemetry.h header also declares the helper classes AutoTimer and AutoCounter. Objects of these types automatically record a histogram value when they go out of scope:

nsresult
nsPluginHost::StopPluginInstance(nsNPAPIPluginInstance* aInstance)
{
  Telemetry::AutoTimer<Telemetry::PLUGIN_SHUTDOWN_MS> timer;
  ...
  return NS_OK;
}

Miscellaneous

  • Changing histogram declarations after the histogram has been released is tricky. You will need to create a new histogram with the new parameters.
    • For enum histograms, it's prudent to set "n_buckets" to a slightly larger value than needed since new elements may be added to the enum in the future.
  • getHistogramById will throw an NS_ERROR_ILLEGAL_VALUE JavaScript exception if it is called with an invalid histogram ID
  • Flag histograms will ignore any changes after the flag is set, so once the flag is set, it cannot be unset
  • Histograms which track timings in milliseconds or microseconds should suffix their names with "_MS" and "_US" respectively. Flag-type histograms should have the suffix "_FLAG" in their name.
  • If a histogram does not specify a "low" value, it will always have a "0" bucket (for negative or zero values) and a "1" bucket (for values between 1 and the next bucket)
  • The histograms on the about:telemetry page only show the non-empty buckets in a histogram except for the bucket to the left of the first non-empty bucket and the bucket to the right of the last non-empty bucket

Document Tags and Contributors

Tags: 
Contributors to this page: azhang9, Vladan, overholt, gfritzsche, mnoorenberghe, rvitillo, Sheppy
Last updated by: azhang9,