Debian Codegraph @

js/src/doc/use_counter.md

raw
1 Adding Use Counter Telemetry to the JS Engine
2 ==============================================
3
4 [Use Counters](../dom/use-counters.rst) are used to collect data about the execution
5 of Firefox. In SpiderMonkey we can use use counters to highlight when certain VM
6 features are used or to measure how often certain scenarios are encountered.
7
8 Because use-counters are intended to find the frequency of a feature's use, a page
9 reports only _if_ a feature was used, not how many times. E.g. if you used a feature
10 a million times on a page, and loaded that page once, but you were the only person
11 who ever used that feature then telemetry would report only one use, not a million.
12
13 To add a SpiderMonkey Use Counter we have a couple extra steps compared to the
14 rest of Firefox.
15
16 ## Adding Telemetry
17
18 1. Add an entry to `FOR_EACH_JS_USE_COUNTER` in `js/public/friend/UsageStatistics.h`.
19 This will add an entry to the `JSUseCounter` enum used in the next step.
20
21 2. Call `JSRuntime::setUseCounter(JSObject*, JSUseCounter)` where you would like to
22 report some use counter telemetry. The object passed is used to determine the
23 global to which this usage will be attributed. A good default choice would be to
24 pass `cx->global()` here.
25
26 The UseCounter machinery is relatively efficient, and so avoiding double
27 submission of counters is not necessary. Nevertheless, it is not free, so be
28 cautious about telemetry on very hot paths.
29
30 3. Add an entry to `dom/base/UseCounters.conf`. Use a custom entry in the
31 JavaScript feature usage section. _Note: the first character after `JS_` should
32 be lowercase. See Bug 1934649._
33
34 5. With a browser-building mozconfig active, run `mach gen-use-counter-metrics`. This
35 will update `dom/base/use_counter_metrics.yaml`. Note the emails will be incorrect
36 until Bug 1899418 is fixed.
37
38 6. Update the switch in `js/xpconnect/src/XPCJSRuntime.cpp` in function
39 `SetUseCounterCallback`. Essentially this function redirects from the JS types to
40 the DOM constants defined via use_counter_metrics.yaml.
41
42 At this point you should be able to build the browser and the shell.
43
44 ## Testing Telemetry
45
46 You should write tests for your telemetry. You can do that in the shell using the
47 `getUseCounterResults()` test helper function.
48
49 You can then further test your browser telemetry works interactively using
50 `about:glean`. Visit a page which will trigger the event you're interested in,
51 then visit `about:glean`, and open the DevTools console.
52
53 In the console you can access these use counters from the `Glean` object:
54
55 ```
56 Glean.useCounterDoc.jsOptimizeGetIteratorFuse.testGetValue()
57 ```
58
59 Will for example print the current ping value for the JS Optimize Get Iterator Fuse
60 use counter.
61
62
63 ## Landing Telemetry
64
65 When reviewing telemetry, follow the instructions automatically added by the data
66 classification robot. Typically our use counter telemetry is deeply non-identifiable
67 and thus easily "Category 1 Technical Data", but it is the responsibility of the
68 developer and reviewer to be sure.