Skip to content

Commit c8473c2

Browse files
author
Beatriz Rizental
authored
Bug 1677445 - Implement PingType and ping maker (#22)
1 parent 5e5a712 commit c8473c2

24 files changed

+734
-53
lines changed

src/constants.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 { version } from "../package.json";
6+
7+
export const GLEAN_SCHEMA_VERSION = 1;
8+
9+
// The version for the current build of Glean.js
10+
//
11+
// PACKAGE_VERSION is defined as a global by webpack,
12+
// we need a default here for testing when the app is not build with webpack.
13+
export const GLEAN_VERSION = version;
14+
15+
// The name of a "ping" that will include Glean ping_info metrics,
16+
// such as ping sequence numbers.
17+
//
18+
// Note that this is not really a ping,
19+
// but a neat way to gather metrics in the metrics database.
20+
//
21+
// See: https://mozilla.github.io/glean/book/dev/core/internal/reserved-ping-names.html
22+
export const PING_INFO_STORAGE = "glean_ping_info";
23+
24+
// The name of a "ping" that will include the Glean client_info metrics,
25+
// such as ping sequence numbers.
26+
//
27+
// Note that this is not really a ping,
28+
// but a neat way to gather metrics in the metrics database.
29+
//
30+
// See: https://mozilla.github.io/glean/book/dev/core/internal/reserved-ping-names.html
31+
export const CLIENT_INFO_STORAGE = "glean_client_info";

src/glean.ts

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import Database from "database";
6-
import { isUndefined } from "utils";
5+
import MetricsDatabase from "metrics/database";
6+
import PingsDatabase from "pings/database";
7+
import { isUndefined, sanitizeApplicationId } from "utils";
78

89
class Glean {
910
// The Glean singleton.
1011
private static _instance?: Glean;
1112

12-
// The metrics database.
13-
private _db: Database;
13+
// The metrics and pings databases.
14+
private _db: {
15+
metrics: MetricsDatabase,
16+
pings: PingsDatabase
17+
}
1418
// Whether or not to record metrics.
1519
private _uploadEnabled: boolean;
20+
// Whether or not Glean has been initialized.
21+
private _initialized: boolean;
22+
23+
// Properties that will only be set on `initialize`.
24+
private _applicationId?: string;
1625

1726
private constructor() {
1827
if (!isUndefined(Glean._instance)) {
@@ -21,7 +30,11 @@ class Glean {
2130
Use Glean.instance instead to access the Glean singleton.`);
2231
}
2332

24-
this._db = new Database();
33+
this._initialized = false;
34+
this._db = {
35+
metrics: new MetricsDatabase(),
36+
pings: new PingsDatabase()
37+
};
2538
// Temporarily setting this to true always, until Bug 1677444 is resolved.
2639
this._uploadEnabled = true;
2740
}
@@ -34,9 +47,36 @@ class Glean {
3447
return Glean._instance;
3548
}
3649

50+
/**
51+
* Initialize Glean. This method should only be called once, subsequent calls will be no-op.
52+
*
53+
* @param applicationId The application ID (will be sanitized during initialization).
54+
*/
55+
static initialize(applicationId: string): void {
56+
if (Glean.instance._initialized) {
57+
console.warn("Attempted to initialize Glean, but it has already been initialized. Ignoring.");
58+
return;
59+
}
60+
61+
Glean.instance._applicationId = sanitizeApplicationId(applicationId);
62+
}
63+
64+
/**
65+
* Gets this Glean's instance metrics database.
66+
*
67+
* @returns This Glean's instance metrics database.
68+
*/
69+
static get metricsDatabase(): MetricsDatabase {
70+
return Glean.instance._db.metrics;
71+
}
3772

38-
static get db(): Database {
39-
return Glean.instance._db;
73+
/**
74+
* Gets this Glean's instance pings database.
75+
*
76+
* @returns This Glean's instance pings database.
77+
*/
78+
static get pingsDatabase(): PingsDatabase {
79+
return Glean.instance._db.pings;
4080
}
4181

4282
// TODO: Make the following functions `private` once Bug 1682769 is resolved.
@@ -48,6 +88,14 @@ class Glean {
4888
Glean.instance._uploadEnabled = value;
4989
}
5090

91+
static get applicationId(): string | undefined {
92+
if (!Glean.instance._initialized) {
93+
console.error("Attempted to access the Glean.applicationId before Glean was initialized.");
94+
}
95+
96+
return Glean.instance._applicationId;
97+
}
98+
5199
/**
52100
* **Test-only API**
53101
*
@@ -58,8 +106,9 @@ class Glean {
58106
static async testRestGlean(): Promise<void> {
59107
// Reset upload enabled state, not to inerfere with other tests.
60108
Glean.uploadEnabled = true;
61-
// Clear the database.
62-
await Glean.db.clearAll();
109+
// Clear the databases.
110+
await Glean.metricsDatabase.clearAll();
111+
await Glean.pingsDatabase.clearAll();
63112
}
64113
}
65114

src/database.ts renamed to src/metrics/database.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function createMetricsPayload(v: Metrics): Metrics {
7878
* We have one store in this format for each lifetime: user, ping and application.
7979
*
8080
*/
81-
class Database {
81+
class MetricsDatabase {
8282
private userStore: Store;
8383
private pingStore: Store;
8484
private appStore: Store;
@@ -221,7 +221,7 @@ class Database {
221221
* @returns An object containing all the metrics recorded to the given ping,
222222
* `undefined` in case the ping doesn't contain any recorded metrics.
223223
*/
224-
async getPing(ping: string, clearPingLifetimeData: boolean): Promise<Metrics | undefined> {
224+
async getPingMetrics(ping: string, clearPingLifetimeData: boolean): Promise<Metrics | undefined> {
225225
const userData = await this.getAndValidatePingData(ping, Lifetime.User);
226226
const pingData = await this.getAndValidatePingData(ping, Lifetime.Ping);
227227
const appData = await this.getAndValidatePingData(ping, Lifetime.Application);
@@ -267,4 +267,4 @@ class Database {
267267
}
268268
}
269269

270-
export default Database;
270+
export default MetricsDatabase;

src/metrics/boolean.ts renamed to src/metrics/types/boolean.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class BooleanMetricType extends MetricType {
4040
}
4141

4242
const metric = new BooleanMetric(value);
43-
await Glean.db.record(this, metric);
43+
await Glean.metricsDatabase.record(this, metric);
4444
}
4545

4646
/**
@@ -57,7 +57,7 @@ class BooleanMetricType extends MetricType {
5757
* @returns The value found in storage or `undefined` if nothing was found.
5858
*/
5959
async testGetValue(ping: string): Promise<boolean | undefined> {
60-
return Glean.db.getMetric<boolean>(ping, this);
60+
return Glean.metricsDatabase.getMetric<boolean>(ping, this);
6161
}
6262
}
6363

src/metrics/counter.ts renamed to src/metrics/types/counter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class CounterMetricType extends MetricType {
8787
};
8888
})(amount);
8989

90-
await Glean.db.transform(this, transformFn);
90+
await Glean.metricsDatabase.transform(this, transformFn);
9191
}
9292

9393
/**
@@ -104,7 +104,7 @@ class CounterMetricType extends MetricType {
104104
* @returns The value found in storage or `undefined` if nothing was found.
105105
*/
106106
async testGetValue(ping: string): Promise<number | undefined> {
107-
return Glean.db.getMetric<number>(ping, this);
107+
return Glean.metricsDatabase.getMetric<number>(ping, this);
108108
}
109109
}
110110

src/metrics/datetime.ts renamed to src/metrics/types/datetime.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export class DatetimeMetric extends Metric<DatetimeInternalRepresentation, strin
4040
super(v);
4141
}
4242

43+
static fromDate(v: Date, timeUnit: TimeUnit): DatetimeMetric {
44+
return new DatetimeMetric({
45+
timeUnit,
46+
timezone: v.getTimezoneOffset(),
47+
date: v.toISOString()
48+
});
49+
}
50+
4351
/**
4452
* Gets the datetime data as a Date object.
4553
*
@@ -198,12 +206,8 @@ class DatetimeMetricType extends MetricType {
198206
break;
199207
}
200208

201-
const metric = new DatetimeMetric({
202-
timeUnit: this.timeUnit,
203-
timezone: value.getTimezoneOffset(),
204-
date: value.toISOString(),
205-
});
206-
await Glean.db.record(this, metric);
209+
const metric = DatetimeMetric.fromDate(value, this.timeUnit);
210+
await Glean.metricsDatabase.record(this, metric);
207211
}
208212

209213
/**
@@ -220,7 +224,7 @@ class DatetimeMetricType extends MetricType {
220224
* @returns The value found in storage or `undefined` if nothing was found.
221225
*/
222226
private async testGetValueAsDatetimeMetric(ping: string): Promise<DatetimeMetric | undefined> {
223-
const value = await Glean.db.getMetric<DatetimeInternalRepresentation>(ping, this);
227+
const value = await Glean.metricsDatabase.getMetric<DatetimeInternalRepresentation>(ping, this);
224228
if (value) {
225229
return new DatetimeMetric(value);
226230
}

src/metrics/string.ts renamed to src/metrics/types/string.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class StringMetricType extends MetricType {
6262
}
6363

6464
const metric = new StringMetric(value.substr(0, MAX_LENGTH_VALUE));
65-
await Glean.db.record(this, metric);
65+
await Glean.metricsDatabase.record(this, metric);
6666
}
6767

6868
/**
@@ -79,7 +79,7 @@ class StringMetricType extends MetricType {
7979
* @returns The value found in storage or `undefined` if nothing was found.
8080
*/
8181
async testGetValue(ping: string): Promise<string | undefined> {
82-
return Glean.db.getMetric<string>(ping, this);
82+
return Glean.metricsDatabase.getMetric<string>(ping, this);
8383
}
8484
}
8585

src/metrics/uuid.ts renamed to src/metrics/types/uuid.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class UUIDMetricType extends MetricType {
6161
return;
6262
}
6363

64-
await Glean.db.record(this, metric);
64+
await Glean.metricsDatabase.record(this, metric);
6565
}
6666

6767
/**
@@ -94,7 +94,7 @@ class UUIDMetricType extends MetricType {
9494
* @returns The value found in storage or `undefined` if nothing was found.
9595
*/
9696
async testGetValue(ping: string): Promise<string | undefined> {
97-
return Glean.db.getMetric<string>(ping, this);
97+
return Glean.metricsDatabase.getMetric<string>(ping, this);
9898
}
9999
}
100100

src/metrics/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import { Metric } from "metrics";
66
import { JSONValue } from "utils";
77

8-
import { BooleanMetric } from "metrics/boolean";
9-
import { CounterMetric } from "metrics/counter";
10-
import { StringMetric } from "metrics/string";
11-
import { UUIDMetric } from "metrics/uuid";
12-
import { DatetimeMetric } from "./datetime";
8+
import { BooleanMetric } from "metrics/types/boolean";
9+
import { CounterMetric } from "metrics/types/counter";
10+
import { StringMetric } from "metrics/types/string";
11+
import { UUIDMetric } from "metrics/types/uuid";
12+
import { DatetimeMetric } from "./types/datetime";
1313

1414
/**
1515
* A map containing all supported internal metrics and its constructors.

0 commit comments

Comments
 (0)