Skip to content

Commit aaa1ce0

Browse files
committed
Dinamically add metric constructors to metric map
This is nice because it means metrics will work exactly like plugins in the sense that they are only added to the bundle if they are used. I am not entirely sure about this change, because it means that if a given metric is in the database, but was not added to the metric map (think if a metric is removed from the code, but has not been collected in a ping once the new code lands) it will be read as invalid by Glean.
1 parent 06a81b5 commit aaa1ce0

File tree

16 files changed

+76
-48
lines changed

16 files changed

+76
-48
lines changed

glean/src/core/context.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type EventsDatabase from "./metrics/events_database/index.js";
88
import type PingsDatabase from "./pings/database.js";
99
import type ErrorManager from "./error/index.js";
1010
import type Platform from "../platform/index.js";
11+
import type { Metric } from "./metrics/metric.js";
12+
import type { JSONValue } from "./utils.js";
1113
import Dispatcher from "./dispatcher.js";
1214
import log, { LoggingLevel } from "./log.js";
1315

@@ -45,6 +47,13 @@ export class Context {
4547
private _initialized = false;
4648
// Whether or not Glean is in testing mode.
4749
private _testing = false;
50+
// A map of metric types and their constructors.
51+
// This map is dinamically filled everytime a metric type is constructed.
52+
//
53+
// If a metric is not on this map it cannot be serialized from the database.
54+
private _supportedMetrics: {
55+
[type: string]: new (v: unknown) => Metric<JSONValue, JSONValue>
56+
} = {};
4857

4958
// The moment the current Glean.js session started.
5059
private _startTime: Date;
@@ -242,4 +251,27 @@ export class Context {
242251
static isPlatformSet(): boolean {
243252
return !!Context.instance._platform;
244253
}
254+
255+
static getSupportedMetric(type: string): (new (v: unknown) => Metric<JSONValue, JSONValue>) | undefined {
256+
return Context.instance._supportedMetrics[type];
257+
}
258+
259+
/**
260+
* Adds a new constructor to the supported metrics map.
261+
*
262+
* If the metric map already contains this constructor, this is a no-op.
263+
*
264+
* @param type A string identifying the given metric type.
265+
* @param ctn The metric constructor.
266+
*/
267+
static addSupportedMetric(
268+
type: string,
269+
ctn: new (v: unknown) => Metric<JSONValue, JSONValue>
270+
): void {
271+
if (type in Context.instance._supportedMetrics) {
272+
return;
273+
}
274+
275+
Context.instance._supportedMetrics[type] = ctn;
276+
}
245277
}

glean/src/core/glean.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,10 @@ class Glean {
526526
uploadEnabled = true,
527527
config?: ConfigurationInterface
528528
): Promise<void> {
529+
// Core metrics need tp be re-initialized so that
530+
// the supportedMetrics map is re-created.
531+
Glean.instance._coreMetrics = new CoreMetrics();
532+
529533
Context.testing = true;
530534

531535
Glean.setPlatform(TestPlatform);

glean/src/core/metrics/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import type { JSONValue} from "../utils.js";
6+
import { isUndefined, testOnly } from "../utils.js";
67
import type { Lifetime } from "./lifetime.js";
78
import type { ErrorType } from "../error/error_type.js";
8-
import { isUndefined, testOnly } from "../utils.js";
9+
import type { Metric } from "./metric.js";
910
import { getValidDynamicLabel } from "./types/labeled.js";
1011
import { Context } from "../context.js";
1112

@@ -53,7 +54,14 @@ export abstract class MetricType implements CommonMetricData {
5354
readonly disabled: boolean;
5455
dynamicLabel?: string;
5556

56-
constructor(type: string, meta: CommonMetricData) {
57+
constructor(
58+
type: string,
59+
meta: CommonMetricData,
60+
metricCtn?: new (v: unknown) => Metric<JSONValue, JSONValue>
61+
) {
62+
if (metricCtn) {
63+
Context.addSupportedMetric(type, metricCtn);
64+
}
5765
this.type = type;
5866

5967
this.name = meta.name;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class BooleanMetric extends Metric<boolean, boolean> {
3030
*/
3131
class BooleanMetricType extends MetricType {
3232
constructor(meta: CommonMetricData) {
33-
super("boolean", meta);
33+
super("boolean", meta, BooleanMetric);
3434
}
3535

3636
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class CounterMetric extends Metric<number, number> {
4343
*/
4444
class CounterMetricType extends MetricType {
4545
constructor(meta: CommonMetricData) {
46-
super("counter", meta);
46+
super("counter", meta, CounterMetric);
4747
}
4848

4949
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class DatetimeMetricType extends MetricType {
168168
timeUnit: TimeUnit;
169169

170170
constructor(meta: CommonMetricData, timeUnit: string) {
171-
super("datetime", meta);
171+
super("datetime", meta, DatetimeMetric);
172172
this.timeUnit = timeUnit as TimeUnit;
173173
}
174174

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class QuantityMetric extends Metric<number, number> {
4141
*/
4242
class QuantityMetricType extends MetricType {
4343
constructor(meta: CommonMetricData) {
44-
super("quantity", meta);
44+
super("quantity", meta, QuantityMetric);
4545
}
4646

4747
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class RateMetric extends Metric<Rate, Rate> {
6060
*/
6161
class RateMetricType extends MetricType {
6262
constructor(meta: CommonMetricData) {
63-
super("rate", meta);
63+
super("rate", meta, RateMetric);
6464
}
6565

6666
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class StringMetric extends Metric<string, string> {
4141
*/
4242
class StringMetricType extends MetricType {
4343
constructor(meta: CommonMetricData) {
44-
super("string", meta);
44+
super("string", meta, StringMetric);
4545
}
4646

4747
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class StringListMetric extends Metric<string[], string[]> {
5151
*/
5252
class StringListMetricType extends MetricType {
5353
constructor(meta: CommonMetricData) {
54-
super("string_list", meta);
54+
super("string_list", meta, StringListMetric);
5555
}
5656

5757
/**

0 commit comments

Comments
 (0)