Skip to content

Commit 0f19137

Browse files
author
Beatriz Rizental
authored
Bug 1683057 - Implement the UUID metric type (#17)
1 parent dffd3f2 commit 0f19137

File tree

11 files changed

+232
-7
lines changed

11 files changed

+232
-7
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@types/assert": "^1.5.2",
3333
"@types/mocha": "^8.0.3",
3434
"@types/selenium-webdriver": "^4.0.10",
35+
"@types/uuid": "^8.3.0",
3536
"@typescript-eslint/eslint-plugin": "^4.6.1",
3637
"@typescript-eslint/parser": "^4.6.1",
3738
"eslint": "^7.12.1",
@@ -49,5 +50,8 @@
4950
"web-ext-types": "^3.2.1",
5051
"webpack": "^5.4.0",
5152
"webpack-cli": "^4.2.0"
53+
},
54+
"dependencies": {
55+
"uuid": "^8.3.2"
5256
}
5357
}

src/glean.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Glean {
5555
*
5656
* TODO: Only allow this function to be called on test mode (depends on Bug 1682771).
5757
*/
58-
static async resetGlean(): Promise<void> {
58+
static async testRestGlean(): Promise<void> {
5959
// Reset upload enabled state, not to inerfere with other tests.
6060
Glean.uploadEnabled = true;
6161
// Clear the database.

src/metrics/boolean.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BooleanMetric extends Metric {
3535
*
3636
* @param value the value to set.
3737
*/
38-
async set(value: BooleanMetricPayload): Promise<void> {
38+
async set(value: boolean): Promise<void> {
3939
if (!this.shouldRecord()) {
4040
return;
4141
}

src/metrics/counter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class CounterMetric extends Metric {
6969

7070
if (amount <= 0) {
7171
// TODO: record error once Bug 1682574 is resolved.
72-
console.warn(`Attempted to add an invalid amount ${amount}. `);
72+
console.warn(`Attempted to add an invalid amount ${amount}. Ignoring.`);
7373
return;
7474
}
7575

src/metrics/payload.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { BooleanMetricPayload, isBooleanMetricPayload } from "metrics/boolean";
66
import { CounterMetricPayload, isCounterMetricPayload } from "metrics/counter";
77
import { StringMetricPayload, isStringMetricPayload } from "metrics/string";
8+
import { UUIDMetricPayload, isUUIDMetricPayload } from "metrics/uuid";
89

910
/**
1011
* Validates that a given value is the correct type of payload for a metric of a given type.
@@ -22,6 +23,8 @@ export function isMetricPayload<T>(type: string, v: unknown): v is T {
2223
return isCounterMetricPayload(v);
2324
case "string":
2425
return isStringMetricPayload(v);
26+
case "uuid":
27+
return isUUIDMetricPayload(v);
2528
default:
2629
return false;
2730
}
@@ -31,5 +34,6 @@ export function isMetricPayload<T>(type: string, v: unknown): v is T {
3134
export type MetricPayload =
3235
BooleanMetricPayload |
3336
CounterMetricPayload |
34-
StringMetricPayload;
37+
StringMetricPayload |
38+
UUIDMetricPayload;
3539

src/metrics/uuid.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import { v4 as UUIDv4, validate as UUIDvalidate } from "uuid";
6+
7+
import Metric, { CommonMetricData } from "metrics";
8+
import Glean from "glean";
9+
import { isString } from "utils";
10+
11+
export type UUIDMetricPayload = string;
12+
13+
/**
14+
* Checks whether or not `v` is a valid UUID metric payload.
15+
*
16+
* @param v The value to verify.
17+
*
18+
* @returns A special Typescript value (which compiles down to a boolean)
19+
* stating whether `v` is a valid boolean metric payload.
20+
*/
21+
export function isUUIDMetricPayload(v: unknown): v is UUIDMetricPayload {
22+
if (!isString(v)) {
23+
return false;
24+
}
25+
26+
return UUIDvalidate(v);
27+
}
28+
29+
/**
30+
* An UUID metric.
31+
*
32+
* Stores UUID v4 (randomly generated) values.
33+
*/
34+
class UUIDMetric extends Metric {
35+
constructor(meta: CommonMetricData) {
36+
super("uuid", meta);
37+
}
38+
39+
/**
40+
* Sets to the specified value.
41+
*
42+
* @param value the value to set.
43+
*
44+
* @throws In case `value` is not a valid UUID.
45+
*/
46+
async set(value: string): Promise<void> {
47+
if (!this.shouldRecord()) {
48+
return;
49+
}
50+
51+
if (!value) {
52+
value = UUIDv4();
53+
}
54+
55+
if (!isUUIDMetricPayload(value)) {
56+
// TODO: record error once Bug 1682574 is resolved.
57+
console.warn(`"${value}" is not a valid UUID. Ignoring`);
58+
return;
59+
}
60+
61+
await Glean.db.record(this, value);
62+
}
63+
64+
/**
65+
* Generates a new random uuid and sets the metric to it.
66+
*
67+
* @returns The generated value or `undefined` in case this metric shouldn't be recorded.
68+
*/
69+
async generateAndSet(): Promise<UUIDMetricPayload | undefined> {
70+
if (!this.shouldRecord()) {
71+
return;
72+
}
73+
74+
const value = UUIDv4();
75+
await this.set(value);
76+
77+
return value;
78+
}
79+
80+
/**
81+
* **Test-only API**
82+
*
83+
* Gets the currently stored value as a string.
84+
*
85+
* This doesn't clear the stored value.
86+
*
87+
* TODO: Only allow this function to be called on test mode (depends on Bug 1682771).
88+
*
89+
* @param ping the ping from which we want to retrieve this metrics value from.
90+
*
91+
* @returns The value found in storage or `undefined` if nothing was found.
92+
*/
93+
async testGetValue(ping: string): Promise<UUIDMetricPayload | undefined> {
94+
return Glean.db.getMetric(ping, this);
95+
}
96+
}
97+
98+
export default UUIDMetric;

tests/metrics/boolean.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Lifetime } from "metrics";
1010

1111
describe("BooleanMetric", function() {
1212
beforeEach(async function() {
13-
await Glean.resetGlean();
13+
await Glean.testRestGlean();
1414
});
1515

1616
it("attemping to get the value of a metric that hasn't been recorded doesn't error", async function() {

tests/metrics/counter.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Lifetime } from "metrics";
1010

1111
describe("CounterMetric", function() {
1212
beforeEach(async function() {
13-
await Glean.resetGlean();
13+
await Glean.testRestGlean();
1414
});
1515

1616
it("attemping to get the value of a metric that hasn't been recorded doesn't error", async function() {

tests/metrics/string.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Lifetime } from "metrics";
1010

1111
describe("StringMetric", function() {
1212
beforeEach(async function() {
13-
await Glean.resetGlean();
13+
await Glean.testRestGlean();
1414
});
1515

1616
it("attemping to get the value of a metric that hasn't been recorded doesn't error", async function() {

0 commit comments

Comments
 (0)