-
Notifications
You must be signed in to change notification settings - Fork 34
Bug 1716956: Initial attempt to implement metric type "StringList" #614
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
14 commits
Select commit
Hold shift + click to select a range
b4d814d
Bump ts-node from 10.2.0 to 10.2.1 in /samples/web-extension/typescript
dependabot[bot] ed47740
Bump geckodriver from 2.0.2 to 2.0.3 in /glean
dependabot[bot] 5d42645
Bump @types/node in /samples/web-extension/typescript
dependabot[bot] a1cd532
Merge branch 'main' of https://github.com/mozilla/glean.js into main
ChinYing-Li 44ba6f3
Bug 1716956: Implement the String List metric type
ChinYing-Li 84f1e0d
Fix linting error
ChinYing-Li 1fbcdc3
Fix documentation strings
ChinYing-Li b137e26
Merge branch 'main' of https://github.com/mozilla/glean.js into main
ChinYing-Li a5038e9
Merge branch 'main' into bug_1716956_new
ChinYing-Li 1c1ed76
Update package-lock.json
ChinYing-Li c3ff3e1
Fix the wording in string_list's docstring
ChinYing-Li 131abe5
Merge branch 'main' of https://github.com/mozilla/glean.js into main
ChinYing-Li 2e93ef1
Merge branch 'main' into bug_1716956_new
ChinYing-Li 3091685
Update package-lock.json
ChinYing-Li 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,166 @@ | ||
| /* 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 { Context } from "../../context.js"; | ||
| import { Metric } from "../metric.js"; | ||
| import { isString, truncateStringAtBoundaryWithError } from "../../utils.js"; | ||
| import type { JSONValue } from "../../utils.js"; | ||
| import { ErrorType } from "../../error/error_type.js"; | ||
|
|
||
| export const MAX_LIST_LENGTH = 20; | ||
| export const MAX_STRING_LENGTH = 50; | ||
|
|
||
| export class StringListMetric extends Metric<string[], string[]> { | ||
| constructor(v: unknown) { | ||
| super(v); | ||
| } | ||
|
|
||
| validate(v: unknown): v is string[] { | ||
| if (!Array.isArray(v)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (v.length > MAX_LIST_LENGTH) { | ||
| return false; | ||
| } | ||
|
|
||
| for (const s of v) { | ||
| if (!isString(s) || s.length > MAX_STRING_LENGTH) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| payload(): string[] { | ||
| return this._inner; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A string list metric. | ||
| * | ||
| * This allows appending a string value with arbitrary content to a list. | ||
| * The list is length-limited to `MAX_LIST_LENGTH`. | ||
| * Strings are length-limited to `MAX_STRING_LENGTH` characters. | ||
| */ | ||
| class StringListMetricType extends MetricType { | ||
| constructor(meta: CommonMetricData) { | ||
| super("string_list", meta); | ||
| } | ||
|
|
||
| /** | ||
| * Sets to the specified string list value. | ||
| * | ||
| * # Note | ||
| * | ||
| * Truncates the list if it is longer than `MAX_LIST_LENGTH` and records an error. | ||
| * | ||
| * Truncates the value if it is longer than `MAX_STRING_LENGTH` characters | ||
| * and records an error. | ||
| * | ||
| * @param value The list of strings to set the metric to. | ||
| */ | ||
| set(value: string[]): void { | ||
| Context.dispatcher.launch(async () => { | ||
| if (!this.shouldRecord(Context.uploadEnabled)) { | ||
| return; | ||
| } | ||
ChinYing-Li marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const truncatedList: string[] = []; | ||
| if (value.length > MAX_LIST_LENGTH) { | ||
| await Context.errorManager.record( | ||
| this, | ||
| ErrorType.InvalidValue, | ||
| `String list length of ${value.length} exceeds maximum of ${MAX_LIST_LENGTH}.` | ||
| ); | ||
| } | ||
|
|
||
| for (let i = 0; i < Math.min(value.length, MAX_LIST_LENGTH); ++i) { | ||
| const truncatedString = await truncateStringAtBoundaryWithError(this, value[i], MAX_STRING_LENGTH); | ||
| truncatedList.push(truncatedString); | ||
| } | ||
| const metric = new StringListMetric(truncatedList); | ||
| await Context.metricsDatabase.record(this, metric); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a new string `value` to the list. | ||
| * | ||
| * # Note | ||
| * | ||
| * - If the list is already of length `MAX_LIST_LENGTH`, record an error. | ||
| * - Truncates the value if it is longer than `MAX_STRING_LENGTH` characters | ||
| * and records an error. | ||
| * | ||
| * @param value The string to add. | ||
| */ | ||
| add(value: string): void { | ||
| Context.dispatcher.launch(async () => { | ||
| if (!this.shouldRecord(Context.uploadEnabled)) { | ||
| return; | ||
| } | ||
|
|
||
| const truncatedValue = await truncateStringAtBoundaryWithError(this, value, MAX_STRING_LENGTH); | ||
| let currentLen = 0; | ||
|
|
||
| const transformFn = ((value) => { | ||
| return (v?: JSONValue): StringListMetric => { | ||
| let metric: StringListMetric; | ||
| let result: string[]; | ||
| try { | ||
| metric = new StringListMetric(v); | ||
| result = metric.get(); | ||
| currentLen = result.length; | ||
| if (result.length < MAX_LIST_LENGTH) { | ||
| result.push(value); | ||
| } | ||
| } catch { | ||
| metric = new StringListMetric([value]); | ||
| result = [value]; | ||
| } | ||
| metric.set(result); | ||
| return metric; | ||
| }; | ||
| })(truncatedValue); | ||
|
|
||
| await Context.metricsDatabase.transform(this, transformFn); | ||
|
|
||
| if (currentLen >= MAX_LIST_LENGTH) { | ||
| await Context.errorManager.record( | ||
| this, | ||
| ErrorType.InvalidValue, | ||
| `String list length of ${currentLen+1} exceeds maximum of ${MAX_LIST_LENGTH}.` | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Test-only API** | ||
| * | ||
| * Gets the currently stored value as a string array. | ||
| * | ||
| * 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<string[] | undefined> { | ||
| let metric: string[] | undefined; | ||
| await Context.dispatcher.testLaunch(async () => { | ||
| metric = await Context.metricsDatabase.getMetric<string[]>(ping, this); | ||
| }); | ||
| return metric; | ||
| } | ||
| } | ||
|
|
||
| export default StringListMetricType; | ||
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
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.