Skip to content

Commit 1c11990

Browse files
committed
Address review comments
1 parent 1af4fa0 commit 1c11990

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* [#101](https://github.com/mozilla/glean.js/pull/101): BUGFIX: Only validate Debug View Tag and Source Tags when they are present.
1111
* [#101](https://github.com/mozilla/glean.js/pull/101): BUGFIX: Only validate Debug View Tag and Source Tags when they are present.
1212
* [#102](https://github.com/mozilla/glean.js/pull/102): BUGFIX: Include a Glean User-Agent header in all pings.
13-
* [#97](https://github.com/mozilla/glean.js/pull/97): Add support for labelled metric types (string, boolean and counter).
13+
* [#97](https://github.com/mozilla/glean.js/pull/97): Add support for labeled metric types (string, boolean and counter).
1414

1515
# v0.4.0 (2021-03-10)
1616

glean/src/core/metrics/database.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class MetricsDatabase {
133133
}
134134

135135
const store = this._chooseStore(metric.lifetime);
136-
const storageKey = await metric.getAsyncIdentifier();
136+
const storageKey = await metric.identifier();
137137
for (const ping of metric.sendInPings) {
138138
const finalTransformFn = (v?: JSONValue): JSONValue => transformFn(v).get();
139139
await store.update([ping, metric.type, storageKey], finalTransformFn);
@@ -205,7 +205,7 @@ class MetricsDatabase {
205205
metric: MetricType
206206
): Promise<T | undefined> {
207207
const store = this._chooseStore(metric.lifetime);
208-
const storageKey = await metric.getAsyncIdentifier();
208+
const storageKey = await metric.identifier();
209209
const value = await store.get([ping, metric.type, storageKey]);
210210
if (!isUndefined(value) && !validateMetricInternalRepresentation<T>(metric.type, value)) {
211211
console.error(`Unexpected value found for metric ${storageKey}: ${JSON.stringify(value)}. Clearing.`);

glean/src/core/metrics/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ export abstract class MetricType implements CommonMetricData {
155155
* @returns The generated identifier. If `category` is empty, it's ommitted. Otherwise,
156156
* it's the combination of the metric's `category`, `name` and `label`.
157157
*/
158-
async getAsyncIdentifier(): Promise<string> {
158+
async identifier(): Promise<string> {
159159
const baseIdentifier = this.baseIdentifier();
160160

161161
// We need to use `isUndefined` and cannot use `(this.dynamicLabel)` because we want
162-
// empty strings to propagate as a dynamic labels, so that erros are potentially recorded.
162+
// empty strings to propagate as dynamic labels, so that erros are potentially recorded.
163163
if (!isUndefined(this.dynamicLabel)) {
164164
return await LabeledMetricType.getValidDynamicLabel(this);
165165
} else {
@@ -178,7 +178,7 @@ export abstract class MetricType implements CommonMetricData {
178178
}
179179

180180
/**
181-
* This is no-op internal metric representation.
181+
* This is a no-op internal metric representation.
182182
*
183183
* This can be used to instruct the validators to simply report
184184
* whatever is stored internally, without performing any specific

glean/src/core/metrics/types/labeled.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ class LabeledMetricType<T extends SupportedLabeledTypes> {
5252
labels?: string[],
5353
) {
5454
return new Proxy(this, {
55-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
56-
get: (_target: LabeledMetricType<T>, label: string): any => {
55+
get: (_target: LabeledMetricType<T>, label: string): T => {
5756
if (labels) {
58-
return LabeledMetricType.createFromStaticLabel<typeof submetric>(meta, submetric, labels, label);
57+
return LabeledMetricType.createFromStaticLabel(meta, submetric, labels, label);
5958
}
6059

61-
return LabeledMetricType.createFromDynamicLabel<typeof submetric>(meta, submetric, label);
60+
return LabeledMetricType.createFromDynamicLabel(meta, submetric, label);
6261
}
6362
});
6463
}
@@ -94,7 +93,7 @@ class LabeledMetricType<T extends SupportedLabeledTypes> {
9493
submetricClass: T,
9594
allowedLabels: string[],
9695
label: string
97-
): T {
96+
): InstanceType<T> {
9897
// If the label was provided in the registry file, then use it. Otherwise,
9998
// store data in the `OTHER_LABEL`.
10099
const adjustedLabel = allowedLabels.includes(label) ? label : OTHER_LABEL;
@@ -119,14 +118,23 @@ class LabeledMetricType<T extends SupportedLabeledTypes> {
119118
meta: CommonMetricData,
120119
submetricClass: T,
121120
label: string
122-
): T {
121+
): InstanceType<T> {
123122
const newMeta: CommonMetricData = {
124123
...meta,
125124
dynamicLabel: label
126125
};
127126
return new submetricClass(newMeta);
128127
}
129128

129+
/**
130+
* Checks if the dynamic label stored in the metric data is
131+
* valid. If not, record an error and store data in the "__other__"
132+
* label.
133+
*
134+
* @param metric the metric metadata.
135+
*
136+
* @returns a valid label that can be used to store data.
137+
*/
130138
static async getValidDynamicLabel(metric: MetricType): Promise<string> {
131139
// Note that we assume `metric.dynamicLabel` to always be available within this function.
132140
// This is a safe assumptions because we should only call `getValidDynamicLabel` if we have

glean/tests/core/metrics/labeled.spec.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import PingType from "../../../src/core/pings";
1717
const sandbox = sinon.createSandbox();
1818

1919
describe("LabeledMetric", function() {
20+
const testAppId = `gleanjs.test.${this.title}`;
21+
2022
beforeEach(async function() {
21-
await Glean.testResetGlean("gleanjs.unit.test");
23+
await Glean.testResetGlean(testAppId);
2224
// Disable ping uploading for it not to interfere with this tests.
2325
sandbox.stub(Glean["pingUploader"], "triggerUpload").callsFake(() => Promise.resolve());
2426
});
@@ -34,7 +36,7 @@ describe("LabeledMetric", function() {
3436
sendIfEmpty: false,
3537
});
3638

37-
const labeledCounterMetric = new LabeledMetricType<CounterMetricType>(
39+
const labeledCounterMetric = new LabeledMetricType(
3840
{
3941
category: "telemetry",
4042
name: "labeled_counter_metric",
@@ -79,7 +81,7 @@ describe("LabeledMetric", function() {
7981
sendIfEmpty: false,
8082
});
8183

82-
const labeledCounterMetric = new LabeledMetricType<CounterMetricType>(
84+
const labeledCounterMetric = new LabeledMetricType(
8385
{
8486
category: "telemetry",
8587
name: "labeled_counter_metric",
@@ -126,7 +128,7 @@ describe("LabeledMetric", function() {
126128
});
127129

128130
it("test __other__ label without predefined labels", async function() {
129-
const labeledCounterMetric = new LabeledMetricType<CounterMetricType>(
131+
const labeledCounterMetric = new LabeledMetricType(
130132
{
131133
category: "telemetry",
132134
name: "labeled_counter_metric",
@@ -151,7 +153,7 @@ describe("LabeledMetric", function() {
151153
});
152154

153155
it("test __other__ label without predefined labels before Glean initialization", async function() {
154-
const labeledCounterMetric = new LabeledMetricType<CounterMetricType>(
156+
const labeledCounterMetric = new LabeledMetricType(
155157
{
156158
category: "telemetry",
157159
name: "labeled_counter_metric",
@@ -181,7 +183,7 @@ describe("LabeledMetric", function() {
181183
});
182184

183185
it("Ensure invalid labels on labeled counter go to __other__", async function() {
184-
const labeledCounterMetric = new LabeledMetricType<CounterMetricType>(
186+
const labeledCounterMetric = new LabeledMetricType(
185187
{
186188
category: "telemetry",
187189
name: "labeled_counter_metric",
@@ -203,7 +205,7 @@ describe("LabeledMetric", function() {
203205
});
204206

205207
it("Ensure invalid labels on labeled boolean go to __other__", async function() {
206-
const labeledBooleanMetric = new LabeledMetricType<BooleanMetricType>(
208+
const labeledBooleanMetric = new LabeledMetricType(
207209
{
208210
category: "telemetry",
209211
name: "labeled_boolean_metric",
@@ -225,7 +227,7 @@ describe("LabeledMetric", function() {
225227
});
226228

227229
it("Ensure invalid labels on labeled string go to __other__", async function() {
228-
const labeledStringMetric = new LabeledMetricType<StringMetricType>(
230+
const labeledStringMetric = new LabeledMetricType(
229231
{
230232
category: "telemetry",
231233
name: "labeled_string_metric",
@@ -247,7 +249,7 @@ describe("LabeledMetric", function() {
247249
});
248250

249251
it("test labeled string metric type", async function() {
250-
const labeledStringMetric = new LabeledMetricType<StringMetricType>(
252+
const labeledStringMetric = new LabeledMetricType(
251253
{
252254
category: "telemetry",
253255
name: "labeled_string_metric",
@@ -269,7 +271,7 @@ describe("LabeledMetric", function() {
269271
});
270272

271273
it("test labeled boolean metric type", async function() {
272-
const metric = new LabeledMetricType<BooleanMetricType>(
274+
const metric = new LabeledMetricType(
273275
{
274276
category: "telemetry",
275277
name: "labeled_bool",
@@ -291,7 +293,7 @@ describe("LabeledMetric", function() {
291293
});
292294

293295
it("dynamic labels regex mismatch", async function() {
294-
const labeledCounterMetric = new LabeledMetricType<CounterMetricType>(
296+
const labeledCounterMetric = new LabeledMetricType(
295297
{
296298
category: "telemetry",
297299
name: "labeled_counter_metric",
@@ -320,7 +322,7 @@ describe("LabeledMetric", function() {
320322
});
321323

322324
it("dynamic labels regex allowed", async function() {
323-
const labeledCounterMetric = new LabeledMetricType<CounterMetricType>(
325+
const labeledCounterMetric = new LabeledMetricType(
324326
{
325327
category: "telemetry",
326328
name: "labeled_counter_metric",
@@ -350,7 +352,7 @@ describe("LabeledMetric", function() {
350352
});
351353

352354
it("seen labels get reloaded across initializations", async function() {
353-
const labeledCounterMetric = new LabeledMetricType<CounterMetricType>(
355+
const labeledCounterMetric = new LabeledMetricType(
354356
{
355357
category: "telemetry",
356358
name: "labeled_metric",

0 commit comments

Comments
 (0)