Skip to content

Commit 500dc80

Browse files
author
brizental
committed
Implement a ping uploader
1 parent 97970f8 commit 500dc80

File tree

7 files changed

+504
-17
lines changed

7 files changed

+504
-17
lines changed

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface Configuration {
1111
// The user visible version string fro the application running Glean.js.
1212
readonly appDisplayVersion?: string,
1313
// The server pings are sent to.
14-
readonly serverEndpoint: URL
14+
readonly serverEndpoint?: URL
1515
}
1616

1717
export default Configuration;

src/glean.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DEFAULT_TELEMETRY_ENDPOINT } from "./constants";
66
import Configuration from "config";
77
import MetricsDatabase from "metrics/database";
88
import PingsDatabase from "pings/database";
9+
import PingUploader from "upload";
910
import { isUndefined, sanitizeApplicationId } from "utils";
1011
import { CoreMetrics } from "internal_metrics";
1112
import { Lifetime } from "metrics";
@@ -25,6 +26,8 @@ class Glean {
2526
private _initialized: boolean;
2627
// Instances of Glean's core metrics.
2728
private _coreMetrics: CoreMetrics;
29+
// The ping uploader.
30+
private _pingUploader: PingUploader
2831

2932
// Properties that will only be set on `initialize`.
3033
private _applicationId?: string;
@@ -37,16 +40,21 @@ class Glean {
3740
Use Glean.instance instead to access the Glean singleton.`);
3841
}
3942

43+
this._pingUploader = new PingUploader();
4044
this._coreMetrics = new CoreMetrics();
4145
this._initialized = false;
4246
this._db = {
4347
metrics: new MetricsDatabase(),
44-
pings: new PingsDatabase()
48+
pings: new PingsDatabase(this._pingUploader)
4549
};
4650
// Temporarily setting this to true always, until Bug 1677444 is resolved.
4751
this._uploadEnabled = true;
4852
}
4953

54+
private static get pingUploader(): PingUploader {
55+
return Glean.instance._pingUploader;
56+
}
57+
5058
private static get coreMetrics(): CoreMetrics {
5159
return Glean.instance._coreMetrics;
5260
}
@@ -77,6 +85,10 @@ class Glean {
7785
await Glean.coreMetrics.initialize(config?.appBuild, config?.appDisplayVersion);
7886

7987
Glean.instance._initialized = true;
88+
89+
await Glean.pingUploader.scanPendingPings();
90+
// Even though this returns a promise, there is no need to block on it returning.
91+
Glean.pingUploader.triggerUpload();
8092
}
8193

8294
/**
@@ -158,11 +170,15 @@ class Glean {
158170
Glean.instance._initialized = false;
159171
// Reset upload enabled state, not to inerfere with other tests.
160172
Glean.uploadEnabled = true;
173+
174+
// Stop ongoing jobs and clear pending pings queue.
175+
await Glean.pingUploader.clearPendingPingsQueue();
176+
161177
// Clear the databases.
162178
await Glean.metricsDatabase.clearAll();
163179
await Glean.pingsDatabase.clearAll();
164180

165-
// Initialize Glean.
181+
// Re-Initialize Glean.
166182
await Glean.initialize(applicationId, config);
167183
}
168184
}

src/upload/adapter/browser.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@
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 { GLEAN_VERSION } from "../../constants";
65
import { UploadAdapter } from ".";
7-
import { JSONObject } from "utils";
86

97
const BrowserUploadAdapter: UploadAdapter = {
108
async post(url: URL, options: {
119
headers?: Record<string, string>,
12-
body: JSONObject
10+
body: string
1311
}): Promise<number> {
1412
const stringifiedBody = JSON.stringify(options.body);
1513
const headers = options.headers || {};
1614
const response = await fetch(url.toString(), {
17-
headers: {
18-
"Content-Type": "application/json; charset=utf-8",
19-
"Content-Length": stringifiedBody.length.toString(),
20-
"Date": (new Date()).toISOString(),
21-
"X-Client-Type": "Glean.js",
22-
"X-Client-Version": GLEAN_VERSION,
23-
...headers
24-
},
15+
headers,
2516
body: stringifiedBody,
2617
keepalive: true
2718
});

src/upload/adapter/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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 { JSONObject } from "utils";
6-
75
/**
86
* Uploader interface, actualy uploading logic varies per platform.
97
*/
@@ -18,7 +16,7 @@ export interface UploadAdapter {
1816
*/
1917
post(url: URL, options: {
2018
headers?: Record<string, string>,
21-
body: JSONObject
19+
body: string
2220
}): Promise<number>;
2321
}
2422

0 commit comments

Comments
 (0)