Skip to content

Commit 652afe5

Browse files
author
Beatriz Rizental
authored
Bug 1691068 - Implement log pings (#41)
1 parent 96ca320 commit 652afe5

File tree

11 files changed

+151
-16
lines changed

11 files changed

+151
-16
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-disable no-undef */
2+
/* eslint-disable no-unused-vars */
3+
/* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6+
7+
"use strict";
8+
9+
const appStarted = new Glean.Glean._private.DatetimeMetricType({
10+
category: "sample",
11+
name: "app_started",
12+
sendInPings: ["sample"],
13+
lifetime: "ping",
14+
disabled: false
15+
}, "millisecond");
16+
17+
const buttonClicked = new Glean.Glean._private.CounterMetricType({
18+
category: "sample",
19+
name: "button_clicked",
20+
sendInPings: ["sample"],
21+
lifetime: "ping",
22+
disabled: false
23+
}, "millisecond");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable no-undef */
2+
/* eslint-disable no-unused-vars */
3+
/* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6+
7+
"use strict";
8+
9+
const samplePing = new Glean.Glean._private.PingType("sample", true, false);

samples/qt-qml-app/main.qml

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import QtQuick.Controls 2.0
77
import QtGraphicalEffects 1.15
88

99
import "../../dist/glean.js" as Glean;
10+
// These must be imported after Glean because they rely on Glean being in the environment.
11+
import "./generatedPings.js" as Pings;
12+
import "./generatedMetrics.js" as Metrics;
1013

1114
Rectangle {
1215
id: screen
@@ -16,38 +19,54 @@ Rectangle {
1619
property int displayText: 0
1720

1821
Button {
19-
id: glean
20-
text: "PUSH 👏 THIS 👏 BUTTON"
22+
id: record
23+
text: "Record"
24+
anchors.horizontalCenter: ping.horizontalCenter
25+
anchors.bottom: ping.bottom
26+
anchors.bottomMargin: 80
27+
palette.buttonText: "black"
28+
palette.button: "#f1f1f1"
29+
font.bold: true
30+
onClicked: () => {
31+
console.log("Adding to the `button_clicked` metric.")
32+
Metrics.buttonClicked.add();
33+
}
34+
}
35+
36+
Button {
37+
id: ping
38+
text: "Submit ping"
2139
anchors.centerIn: parent
2240
palette.buttonText: "white"
2341
palette.button: "#ff5000"
2442
font.bold: true
2543
onClicked: () => {
2644
screen.displayText = 1;
27-
console.info("Nothing will happen. Glean.js is not yet implemented.");
45+
Pings.samplePing.submit();
2846
}
2947
}
3048

3149
DropShadow {
32-
anchors.fill: glean
50+
anchors.fill: ping
3351
horizontalOffset: 7
3452
verticalOffset: -7
3553
radius: 0
3654
color: "#0059ab"
37-
source: glean
55+
source: ping
3856
}
3957

4058
Text {
4159
id: consoleWarn
42-
text: "Now, take a look at the logs on your terminal."
60+
text: "A ping should have been submitted, please check you terminal for logs."
4361
visible: displayText
44-
anchors.top: glean.bottom
62+
anchors.top: ping.bottom
4563
anchors.topMargin: 30
46-
anchors.horizontalCenter: glean.horizontalCenter
64+
anchors.horizontalCenter: ping.horizontalCenter
4765
}
4866

4967
Component.onCompleted: {
50-
// Initialize Glean when the application starts.
51-
Glean.Glean.initialize("qt-qml-app", true);
68+
// Initialize Glean.
69+
Glean.Glean.initialize("qt-qml-app", true, { debug: { logPings: true }});
70+
Metrics.appStarted.set();
5271
}
5372
}

samples/web-extension/generatedMetrics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Glean from "glean";
88

99
export const webExtStarted = new Glean._private.DatetimeMetricType({
1010
category: "sample",
11-
name: "webext_installed",
11+
name: "webext_started",
1212
sendInPings: ["sample"],
1313
lifetime: "ping",
1414
disabled: false

samples/web-extension/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import Glean from "glean";
88
import { samplePing } from "./generatedPings";
99
import { webExtStarted, popupOpened } from "./generatedMetrics";
1010

11-
12-
Glean.initialize("web-extension", true);
11+
Glean.initialize("web-extension", true, { debug: { logPings: true }});
1312
webExtStarted.set();
1413

1514
// Listen for messages from the popup.

src/config.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
import { DEFAULT_TELEMETRY_ENDPOINT } from "./constants";
66
import { validateURL } from "utils";
77

8+
/**
9+
* Lists Glean's debug options.
10+
*/
11+
interface DebugOptions {
12+
// Whether or not lot log pings when they are collected.
13+
logPings?: boolean,
14+
}
15+
816
/**
917
* Describes how to configure Glean.
1018
*/
@@ -15,6 +23,8 @@ export interface ConfigurationInterface {
1523
readonly appDisplayVersion?: string,
1624
// The server pings are sent to.
1725
readonly serverEndpoint?: string,
26+
// Debug configuration.
27+
debug?: DebugOptions,
1828
}
1929

2030
export class Configuration implements ConfigurationInterface {
@@ -24,11 +34,13 @@ export class Configuration implements ConfigurationInterface {
2434
readonly appDisplayVersion?: string;
2535
// The server pings are sent to.
2636
readonly serverEndpoint: string;
37+
// Debug configuration.
38+
debug?: DebugOptions;
2739

2840
constructor(config?: ConfigurationInterface) {
2941
this.appBuild = config?.appBuild;
3042
this.appDisplayVersion = config?.appDisplayVersion;
31-
this.appBuild = config?.appBuild;
43+
this.debug = config?.debug;
3244

3345
if (config?.serverEndpoint && !validateURL(config.serverEndpoint)) {
3446
throw new Error(

src/glean.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,15 @@ class Glean {
318318
return Glean.instance._config?.serverEndpoint;
319319
}
320320

321+
/**
322+
* Whether or not to log pings upon collection.
323+
*
324+
* @returns Whether or not to log pings upon collection.
325+
*/
326+
static get logPings(): boolean {
327+
return Glean.instance._config?.debug?.logPings || false;
328+
}
329+
321330
/**
322331
* Gets this Gleans's instance dispatcher.
323332
*
@@ -373,6 +382,32 @@ class Glean {
373382
});
374383
}
375384

385+
/**
386+
* Sets the `logPings` flag.
387+
*
388+
* When this flag is `true` pings will be logged to the console right before they are collected.
389+
*
390+
* @param flag Whether or not to log pings.
391+
*/
392+
static setLogPings(flag: boolean): void {
393+
Glean.dispatcher.launch(() => {
394+
// It is guaranteed that _config will have a value here.
395+
//
396+
// All dispatched tasks are guaranteed to be run after initialize.
397+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
398+
if (!Glean.instance._config!.debug) {
399+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
400+
Glean.instance._config!.debug = {};
401+
}
402+
403+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
404+
Glean.instance._config!.debug.logPings = flag;
405+
406+
// The dispatcher requires that dispatched functions return promises.
407+
return Promise.resolve();
408+
});
409+
}
410+
376411
/**
377412
* **Test-only API**
378413
*

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ export = {
5959
Glean.setUploadEnabled(flag);
6060
},
6161

62+
/**
63+
* Sets the `logPings` flag.
64+
*
65+
* When this flag is `true` pings will be logged
66+
* to the console right before they are collected.
67+
*
68+
* @param flag Whether or not to log pings.
69+
*/
70+
setLogPings(flag: boolean): void {
71+
Glean.setLogPings(flag);
72+
},
73+
6274
_private: {
6375
PingType,
6476
BooleanMetricType,

src/pings/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
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 { v4 as UUIDv4 } from "uuid";
65

76
import collectAndStorePing from "pings/maker";
7+
import { generateUUIDv4 } from "utils";
88
import Glean from "glean";
99

1010
/**
@@ -60,7 +60,7 @@ class PingType {
6060
correctedReason = undefined;
6161
}
6262

63-
const identifier = UUIDv4();
63+
const identifier = generateUUIDv4();
6464
await collectAndStorePing(identifier, this, correctedReason);
6565
return;
6666
});

src/pings/maker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ export async function collectAndStorePing(identifier: string, ping: PingType, re
217217
if (!payload) {
218218
return;
219219
}
220+
221+
if (Glean.logPings) {
222+
console.info(JSON.stringify(payload, null, 2));
223+
}
224+
220225
const headers = getPingHeaders();
221226
return Glean.pingsDatabase.recordPing(
222227
makePath(identifier, ping),

0 commit comments

Comments
 (0)