-
Notifications
You must be signed in to change notification settings - Fork 34
Bug 1679379 - Implement the boolean metric type #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b611df6
Remove code duplication from record database function
9e48bd5
Remove unused imports from index
8865810
Fix test scripts walk directory recursively
1b4c4d1
Implement the boolean metric type
7ff4a7b
Fix bug on isBoolean function, attend to review comments
4b73cbc
Rebase and add missing docs
9e4b386
Attend to review comments
c30a293
Add missing awaits
309d58c
Make the Glean singleton an implementation detail
f48ba34
Make the glean instance private and fix log bug
c23a333
Fix shouldRecord comment, and add TODO comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import Database from "database"; | ||
| import { isUndefined } from "utils"; | ||
|
|
||
| class Glean { | ||
brizental marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // The Glean singleton. | ||
| private static _instance?: Glean; | ||
|
|
||
| // The metrics database. | ||
| private _db: Database; | ||
| // Whether or not to record metrics. | ||
| private _uploadEnabled: boolean; | ||
|
|
||
| private constructor() { | ||
| if (!isUndefined(Glean._instance)) { | ||
| throw new Error( | ||
| `Tried to instantiate Glean through \`new\`. | ||
| Use Glean.instance instead to access the Glean singleton.`); | ||
| } | ||
|
|
||
| this._db = new Database(); | ||
| // Temporarily setting this to true always, until Bug 1677444 is resolved. | ||
| this._uploadEnabled = true; | ||
| } | ||
|
|
||
| private static get instance(): Glean { | ||
| if (!Glean._instance) { | ||
| Glean._instance = new Glean(); | ||
| } | ||
|
|
||
| return Glean._instance; | ||
| } | ||
|
|
||
|
|
||
| static get db(): Database { | ||
| return Glean.instance._db; | ||
| } | ||
|
|
||
| // TODO: Make the following functions `private` once Bug 1682769 is resolved. | ||
| static get uploadEnabled(): boolean { | ||
brizental marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Glean.instance._uploadEnabled; | ||
| } | ||
|
|
||
| static set uploadEnabled(value: boolean) { | ||
| Glean.instance._uploadEnabled = value; | ||
| } | ||
| } | ||
|
|
||
| export default Glean; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import Metric, { CommonMetricData } from "metrics"; | ||
| import Glean from "glean"; | ||
| import { isBoolean } from "utils"; | ||
|
|
||
| export type BooleanMetricPayload = boolean; | ||
brizental marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Checks whether or not `v` is a valid boolean metric payload. | ||
| * | ||
| * @param v The value to verify. | ||
| * | ||
| * @returns A special Typescript value (which compiles down to a boolean) | ||
| * stating wether `v` is a valid boolean metric payload. | ||
| */ | ||
| export function isBooleanMetricPayload(v: unknown): v is BooleanMetricPayload { | ||
| return isBoolean(v); | ||
| } | ||
|
|
||
| class BooleanMetric extends Metric { | ||
| constructor(meta: CommonMetricData) { | ||
| super("boolean", meta); | ||
| } | ||
|
|
||
| /** | ||
| * Sets to the specified boolean value. | ||
| * | ||
| * @param value the value to set. | ||
| */ | ||
| async set(value: BooleanMetricPayload): Promise<void> { | ||
| if (!this.shouldRecord()) { | ||
| return; | ||
| } | ||
|
|
||
| await Glean.db.record(this, value); | ||
| } | ||
|
|
||
| /** | ||
| * **Test-only API (exported for FFI purposes).** | ||
| * | ||
| * Gets the currently stored value as a boolean. | ||
| * | ||
| * This doesn't clear the stored value. | ||
| * | ||
| * TODO: Only allow this function to be called on test mode (depends on Bug 1682771). | ||
| * | ||
| * @param ping the ping from which we want to retrieve this metrics value from. | ||
| * | ||
| * @returns The value found in storage or `undefined` if nothing was found. | ||
| */ | ||
| async testGetValue(ping: string): Promise<BooleanMetricPayload | undefined> { | ||
| return Glean.db.getMetric(ping, isBooleanMetricPayload, this); | ||
| } | ||
| } | ||
|
|
||
| export default BooleanMetric; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { BooleanMetricPayload, isBooleanMetricPayload } from "metrics/boolean"; | ||
| import { isString } from "utils"; | ||
|
|
||
| /** | ||
| * Validates that a given value is the correct type of payload for a metric of a given type. | ||
| * | ||
| * @param type The type of the metric to validate | ||
| * @param v The value to verify | ||
| * | ||
| * @returns Whether or not `v` is of the correct type. | ||
| */ | ||
| export function isMetricPayload(type: string, v: unknown): v is MetricPayload { | ||
brizental marked this conversation as resolved.
Show resolved
Hide resolved
brizental marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| switch (type) { | ||
| case "boolean": | ||
| return isBooleanMetricPayload(v); | ||
| default: | ||
| return isString(v); | ||
| } | ||
| } | ||
|
|
||
| // Leaving the `string` as a valid metric payload here so that tests keep working for now. | ||
brizental marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| export type MetricPayload = BooleanMetricPayload | string; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.