|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
|
| 5 | +import Glean from "glean"; |
| 6 | +import Configuration from "config"; |
| 7 | + |
| 8 | +// Private Glean types to export. |
| 9 | +import PingType from "pings"; |
| 10 | +import BooleanMetricType from "metrics/types/boolean"; |
| 11 | +import CounterMetricType from "metrics/types/counter"; |
| 12 | +import DatetimeMetricType from "metrics/types/datetime"; |
| 13 | +import StringMetricType from "metrics/types/string"; |
| 14 | +import UUIDMetricType from "metrics/types/uuid"; |
| 15 | + |
5 | 16 | export = { |
6 | 17 | /** |
7 | | - * Initializes Glean. |
| 18 | + * Initialize Glean. This method should only be called once, subsequent calls will be no-op. |
| 19 | + * |
| 20 | + * # Note |
| 21 | + * |
| 22 | + * Before this method is called Glean will not be able to upload pings or record metrics, |
| 23 | + * all such operations will be no-op. |
8 | 24 | * |
9 | | - * Before calling this function Glean.js won't submit any pings. |
| 25 | + * This is _not_ the way glean-core deals with this. It will record tasks performed before init |
| 26 | + * and flush them on init. We have a bug to figure out how to do that for Glean.js, Bug 1687491. |
10 | 27 | * |
11 | | - * @param appId Analogous to a Glean SDK application id. |
12 | | - * @param uploadEnabled Whether upload is enabled or not. |
| 28 | + * @param applicationId The application ID (will be sanitized during initialization). |
| 29 | + * @param uploadEnabled Determines whether telemetry is enabled. |
| 30 | + * If disabled, all persisted metrics, events and queued pings (except |
| 31 | + * first_run_date) are cleared. |
| 32 | + * @param config Glean configuration options. |
| 33 | + * |
| 34 | + * @returns A promise that is resolved once initialize is completed. |
13 | 35 | */ |
14 | | - initialize(appId: string, uploadEnabled: boolean): void { |
15 | | - console.info(`Called Glean.initialize("${appId}", ${uploadEnabled})).`); |
| 36 | + async initialize( |
| 37 | + applicationId: string, |
| 38 | + uploadEnabled: boolean, |
| 39 | + config?: Configuration |
| 40 | + ): Promise<void> { |
| 41 | + await Glean.initialize(applicationId, uploadEnabled, config); |
16 | 42 | }, |
17 | 43 |
|
18 | 44 | /** |
19 | | - * Enables / Disables upload. |
| 45 | + * Sets whether upload is enabled or not. |
| 46 | + * |
| 47 | + * When uploading is disabled, metrics aren't recorded at all and no data is uploaded. |
| 48 | + * |
| 49 | + * When disabling, all pending metrics, events and queued pings are cleared. |
20 | 50 | * |
21 | | - * @param flag Whether to disable or enable upload. |
| 51 | + * When enabling, the core Glean metrics are recreated. |
| 52 | + * |
| 53 | + * If the value of this flag is not actually changed, this is a no-op. |
| 54 | + * |
| 55 | + * If Glean has not been initialized yet, this is also a no-op. |
| 56 | + * |
| 57 | + * @param flag When true, enable metric collection. |
| 58 | + * |
| 59 | + * @returns A promise that is resolved once setUploadEnabled is completed. |
22 | 60 | */ |
23 | | - setUploadEnabled(flag: boolean): void { |
24 | | - console.info(`Called Glean.setUploadEnabled(${flag})).`); |
| 61 | + async setUploadEnabled(flag: boolean): Promise<void> { |
| 62 | + if (!Glean.initialized) { |
| 63 | + console.error( |
| 64 | + `Changing upload enabled before Glean is initialized is not supported. |
| 65 | + Pass the correct state into \`Glean.initialize\`. |
| 66 | + See documentation at https://mozilla.github.io/glean/book/user/general-api.html#initializing-the-glean-sdk` |
| 67 | + ); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + await Glean.setUploadEnabled(flag); |
| 72 | + }, |
| 73 | + |
| 74 | + _private: { |
| 75 | + PingType, |
| 76 | + BooleanMetricType, |
| 77 | + CounterMetricType, |
| 78 | + DatetimeMetricType, |
| 79 | + StringMetricType, |
| 80 | + UUIDMetricType |
25 | 81 | } |
26 | 82 | } |
0 commit comments