Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[Full changelog](https://github.com/mozilla/glean.js/compare/v2.0.0...main)

* [#1751](https://github.com/mozilla/glean.js/pull/1751): Add a migration flag to initialize. If not explicitly set in the `config` object the migration from IndexedDB to LocalStorage will not occur. **The only projects that should ever set this flag are those that have used Glean.js in production with a version <v2.0.0 and have upgraded.**

# v2.0.0 (2023-08-03)

[Full changelog](https://github.com/mozilla/glean.js/compare/v1.4.0...v2.0.0)
Expand Down
5 changes: 5 additions & 0 deletions glean/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export interface ConfigurationInterface {
readonly osVersion?: string,
// The build date, provided by glean_parser
readonly buildDate?: Date,
// Migrate from legacy storage (IndexedDB) to the new one (LocalStorage).
// This should only be true for older projects that have existing data in IndexedDB.
readonly migrateFromLegacyStorage?: boolean,
}

// Important: the `Configuration` should only be used internally by the Glean singleton.
Expand All @@ -68,6 +71,7 @@ export class Configuration implements ConfigurationInterface {
readonly osVersion?: string;
readonly buildDate?: Date;
readonly maxEvents: number;
readonly migrateFromLegacyStorage?: boolean;

// Debug configuration.
debug: DebugOptions;
Expand All @@ -82,6 +86,7 @@ export class Configuration implements ConfigurationInterface {
this.osVersion = config?.osVersion;
this.buildDate = config?.buildDate;
this.maxEvents = config?.maxEvents || DEFAULT_MAX_EVENTS;
this.migrateFromLegacyStorage = config?.migrateFromLegacyStorage;

this.debug = {};

Expand Down
17 changes: 14 additions & 3 deletions glean/src/core/glean/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { CLIENT_INFO_STORAGE, KNOWN_CLIENT_ID } from "../constants.js";
import type { ConfigurationInterface } from "../config.js";
import type PlatformSync from "../../platform/sync.js";

import { CLIENT_INFO_STORAGE, KNOWN_CLIENT_ID } from "../constants.js";
import { Configuration } from "../config.js";
import PingUploadManager from "../upload/manager/sync.js";
import { isBoolean, isString, sanitizeApplicationId } from "../utils.js";
Expand All @@ -13,7 +15,6 @@ import { DatetimeMetric } from "../metrics/types/datetime.js";
import CorePings from "../internal_pings.js";
import { registerPluginToEvent } from "../events/utils/sync.js";
import ErrorManagerSync from "../error/sync.js";
import type PlatformSync from "../../platform/sync.js";
import { Lifetime } from "../metrics/lifetime.js";
import { Context } from "../context.js";
import log, { LoggingLevel } from "../log.js";
Expand Down Expand Up @@ -42,7 +43,15 @@ namespace Glean {
*/
function onUploadEnabled(): void {
Context.uploadEnabled = true;
(Context.coreMetrics as CoreMetricsSync).initialize();
}

/**
* Initialize core metrics: client_id, first_run_date, os, etc.
*
* @param migrateFromLegacy Whether or not to migrate data from legacy storage.
*/
function initializeCoreMetrics(migrateFromLegacy?: boolean): void {
(Context.coreMetrics as CoreMetricsSync).initialize(migrateFromLegacy);
}

/**
Expand Down Expand Up @@ -246,6 +255,7 @@ namespace Glean {
// If upload is enabled,
// just follow the normal code path to instantiate the core metrics.
onUploadEnabled();
initializeCoreMetrics(config?.migrateFromLegacyStorage);
} else {
// If upload is disabled, and we've never run before, only set the
// client_id to KNOWN_CLIENT_ID, but do not send a deletion request
Expand Down Expand Up @@ -317,6 +327,7 @@ namespace Glean {
if (Context.uploadEnabled !== flag) {
if (flag) {
onUploadEnabled();
initializeCoreMetrics(Context.config.migrateFromLegacyStorage);
} else {
onUploadDisabled(false);
}
Expand Down
10 changes: 4 additions & 6 deletions glean/src/core/internal_metrics/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class CoreMetricsSync {
);
}

initialize(): void {
initialize(migrateFromLegacyStorage?: boolean): void {
// The "sync" version of Glean.js is only meant to be used in the browser.
// If we cannot access the window object, then we are unable to store
// any of the metric data in `localStorage`.
Expand All @@ -140,11 +140,9 @@ export class CoreMetricsSync {
// Currently we are interested in migrating two things
// 1. The client_id - consistent clientId across all sessions.
// 2. The first_run_date - the date when the client was first run.

// The migration is done only once per client. The flag is set in
// LocalStorage to indicate that the migration has been completed.
const migrationFlag = localStorage.getItem("GLEAN_MIGRATION_FLAG");
if (migrationFlag !== "1") {
if (!!migrateFromLegacyStorage && localStorage.getItem("GLEAN_MIGRATION_FLAG") !== "1") {
// The migration is done only once per client. The flag is set in
// LocalStorage to indicate that the migration has been completed.
this.migrateCoreMetricsFromIdb();
localStorage.setItem("GLEAN_MIGRATION_FLAG", "1");
} else {
Expand Down