-
Notifications
You must be signed in to change notification settings - Fork 34
Bug 1709312: Introduce QuantityMetric #281
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* 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 type { CommonMetricData } from "../index.js"; | ||
| import { MetricType } from "../index.js"; | ||
| import { isNumber } from "../../utils.js"; | ||
| import { Context } from "../../context.js"; | ||
| import { Metric } from "../metric.js"; | ||
|
|
||
| export class QuantityMetric extends Metric<number, number> { | ||
| constructor(v: unknown) { | ||
| super(v); | ||
| } | ||
|
|
||
| validate(v: unknown): v is number { | ||
| if (!isNumber(v)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (v < 0) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| payload(): number { | ||
| return this._inner; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A quantity metric. | ||
| * | ||
| * Used to store quantity. | ||
| * The value can only be non-negative. | ||
| */ | ||
| class QuantityMetricType extends MetricType { | ||
| constructor(meta: CommonMetricData) { | ||
| super("quantity", meta); | ||
| } | ||
|
|
||
| /** | ||
| * An internal implemention of `set` that does not dispatch the recording task. | ||
| * | ||
| * # Important | ||
| * | ||
| * This is absolutely not meant to be used outside of Glean itself. | ||
| * It may cause multiple issues because it cannot guarantee | ||
| * that the recording of the metric will happen in order with other Glean API calls. | ||
| * | ||
| * @param instance The metric instance to record to. | ||
| * @param value The string we want to set to. | ||
| */ | ||
| static async _private_setUndispatched(instance: QuantityMetricType, value: number): Promise<void> { | ||
| if (!instance.shouldRecord(Context.uploadEnabled)) { | ||
| return; | ||
| } | ||
|
|
||
| if (value < 0) { | ||
| console.warn(`Attempted to set an invalid value ${value}. Ignoring.`); | ||
| return; | ||
| } | ||
|
|
||
| if (value > Number.MAX_SAFE_INTEGER) { | ||
| console.warn(`Attempted to set a big value ${value}. Capped at ${Number.MAX_SAFE_INTEGER}.`); | ||
| value = Number.MAX_SAFE_INTEGER; | ||
| } | ||
|
|
||
| const metric = new QuantityMetric(value); | ||
| await Context.metricsDatabase.record(instance, metric); | ||
| } | ||
|
|
||
| /** | ||
| * Sets to the specified quantity value. | ||
| * Logs an warning if the value is negative. | ||
| * | ||
| * @param value the value to set. Must be non-negative | ||
| */ | ||
| set(value: number): void { | ||
| Context.dispatcher.launch(() => QuantityMetricType._private_setUndispatched(this, value)); | ||
| } | ||
|
|
||
| /** | ||
| * **Test-only API.** | ||
| * | ||
| * Gets the currently stored value as a number. | ||
| * | ||
| * 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. | ||
| * Defaults to the first value in `sendInPings`. | ||
| * | ||
| * @returns The value found in storage or `undefined` if nothing was found. | ||
| */ | ||
| async testGetValue(ping: string = this.sendInPings[0]): Promise<number | undefined> { | ||
| let metric: number | undefined; | ||
| await Context.dispatcher.testLaunch(async () => { | ||
| metric = await Context.metricsDatabase.getMetric<number>(ping, this); | ||
| }); | ||
| return metric; | ||
| } | ||
| } | ||
|
|
||
| export default QuantityMetricType; | ||
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,106 @@ | ||
| /* 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 assert from "assert"; | ||
| import { Context } from "../../../src/core/context"; | ||
|
|
||
| import Glean from "../../../src/core/glean"; | ||
| import { Lifetime } from "../../../src/core/metrics/lifetime"; | ||
| import QuantityMetricType from "../../../src/core/metrics/types/quantity"; | ||
|
|
||
| describe("QuantityMetric", function() { | ||
| const testAppId = `gleanjs.test.${this.title}`; | ||
|
|
||
| beforeEach(async function() { | ||
| await Glean.testResetGlean(testAppId); | ||
| }); | ||
|
|
||
| it("attempting to get the value of a metric that hasn't been recorded doesn't error", async function() { | ||
| const metric = new QuantityMetricType({ | ||
| category: "aCategory", | ||
| name: "aQuantityMetric", | ||
| sendInPings: ["aPing", "twoPing", "threePing"], | ||
| lifetime: Lifetime.Ping, | ||
| disabled: false | ||
| }); | ||
|
|
||
| assert.strictEqual(await metric.testGetValue("aPing"), undefined); | ||
| }); | ||
|
|
||
| it("attempting to set when glean upload is disabled is a no-op", async function() { | ||
| Glean.setUploadEnabled(false); | ||
|
|
||
| const metric = new QuantityMetricType({ | ||
| category: "aCategory", | ||
| name: "aQuantityMetric", | ||
| sendInPings: ["aPing", "twoPing", "threePing"], | ||
| lifetime: Lifetime.Ping, | ||
| disabled: false | ||
| }); | ||
|
|
||
| metric.set(10); | ||
| assert.strictEqual(await metric.testGetValue("aPing"), undefined); | ||
| }); | ||
|
|
||
| it("ping payload is correct", async function() { | ||
| const metric = new QuantityMetricType({ | ||
| category: "aCategory", | ||
| name: "aQuantityMetric", | ||
| sendInPings: ["aPing"], | ||
| lifetime: Lifetime.Ping, | ||
| disabled: false | ||
| }); | ||
|
|
||
| metric.set(10); | ||
| assert.strictEqual(await metric.testGetValue("aPing"), 10); | ||
|
|
||
| const snapshot = await Context.metricsDatabase.getPingMetrics("aPing", true); | ||
| assert.deepStrictEqual(snapshot, { | ||
| "quantity": { | ||
| "aCategory.aQuantityMetric": 10 | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it("set properly sets the value in all pings", async function() { | ||
| const metric = new QuantityMetricType({ | ||
| category: "aCategory", | ||
| name: "aQuantityMetric", | ||
| sendInPings: ["aPing", "twoPing", "threePing"], | ||
| lifetime: Lifetime.Ping, | ||
| disabled: false | ||
| }); | ||
|
|
||
| metric.set(0); | ||
| assert.strictEqual(await metric.testGetValue("aPing"), 0); | ||
| assert.strictEqual(await metric.testGetValue("twoPing"), 0); | ||
| assert.strictEqual(await metric.testGetValue("threePing"), 0); | ||
| }); | ||
|
|
||
| it("must not set when passed negative", async function() { | ||
| const metric = new QuantityMetricType({ | ||
| category: "aCategory", | ||
| name: "aQuantityMetric", | ||
| sendInPings: ["aPing"], | ||
| lifetime: Lifetime.Ping, | ||
| disabled: false | ||
| }); | ||
|
|
||
| metric.set(-1); | ||
| assert.strictEqual(await metric.testGetValue("aPing"), undefined); | ||
| }); | ||
|
|
||
| it("saturates at boundary", async function() { | ||
| const metric = new QuantityMetricType({ | ||
| category: "aCategory", | ||
| name: "aQuantityMetric", | ||
| sendInPings: ["aPing"], | ||
| lifetime: Lifetime.Ping, | ||
| disabled: false | ||
| }); | ||
|
|
||
| metric.set(Number.MAX_SAFE_INTEGER+1); | ||
| assert.strictEqual(await metric.testGetValue("aPing"), Number.MAX_SAFE_INTEGER); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you kindly add some more details here? For reference, here's the Rust trait for this same type.