Skip to content

Commit

Permalink
implement withIdbErrorHandling for telemetry logging (#9226)
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamlangford authored Oct 3, 2024
1 parent 6791c26 commit cf3b817
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
57 changes: 28 additions & 29 deletions src/background/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import {
} from "@/data/service/apiClient";
import { allowsTrack } from "@/telemetry/dnt";
import { type DBSchema, type IDBPDatabase, openDB } from "idb";
import { deleteDatabase } from "@/utils/idbUtils";
import {
DATABASE_NAME,
deleteDatabase,
IDB_OPERATION,
withIdbErrorHandling,
} from "@/utils/idbUtils";
import { browserVersion, detectBrowser } from "@/vendors/mixpanelBrowser";
import { count as registrySize } from "@/registry/packageRegistry";
import { count as logSize } from "@/telemetry/logging";
Expand All @@ -35,7 +40,6 @@ import { API_PATHS } from "@/data/service/urlPaths";

const EVENT_BUFFER_DEBOUNCE_MS = 2000;
const EVENT_BUFFER_MAX_MS = 10_000;
const TELEMETRY_DB_NAME = "telemetrydb";
const TELEMETRY_DB_VERSION_NUMBER = 1;
const TELEMETRY_EVENT_OBJECT_STORE = "events";

Expand Down Expand Up @@ -162,7 +166,7 @@ async function openTelemetryDB() {
let database: IDBPDatabase<TelemetryDB> | null = null;

database = await openDB<TelemetryDB>(
TELEMETRY_DB_NAME,
DATABASE_NAME.TELEMETRY,
TELEMETRY_DB_VERSION_NUMBER,
{
upgrade(db) {
Expand Down Expand Up @@ -193,61 +197,56 @@ async function openTelemetryDB() {
return database;
}

async function addEvent(event: UserTelemetryEvent): Promise<void> {
const db = await openTelemetryDB();
// eslint-disable-next-line local-rules/persistBackgroundData -- Function
const withTelemetryDB = withIdbErrorHandling(
openTelemetryDB,
DATABASE_NAME.TELEMETRY,
);

try {
async function addEvent(event: UserTelemetryEvent): Promise<void> {
await withTelemetryDB(async (db) => {
await db.add(TELEMETRY_EVENT_OBJECT_STORE, event);
} finally {
db.close();
}
}, IDB_OPERATION[DATABASE_NAME.TELEMETRY].ADD_EVENT);
}

export async function flushEvents(): Promise<UserTelemetryEvent[]> {
const db = await openTelemetryDB();
try {
return withTelemetryDB(async (db) => {
const tx = db.transaction(TELEMETRY_EVENT_OBJECT_STORE, "readwrite");
const allEvents = await tx.store.getAll();
await tx.store.clear();
return allEvents;
} finally {
db.close();
}
}, IDB_OPERATION[DATABASE_NAME.TELEMETRY].FLUSH_EVENTS);
}

/**
* Deletes and recreates the logging database.
*/
export async function recreateDB(): Promise<void> {
await deleteDatabase(TELEMETRY_DB_NAME);
await deleteDatabase(DATABASE_NAME.TELEMETRY);

// Open the database to recreate it
const db = await openTelemetryDB();
db.close();
await withTelemetryDB(
async () => {},
IDB_OPERATION[DATABASE_NAME.TELEMETRY].RECREATE_DB,
);
}

/**
* Returns the number of telemetry entries in the database.
*/
export async function count(): Promise<number> {
const db = await openTelemetryDB();
try {
return await db.count(TELEMETRY_EVENT_OBJECT_STORE);
} finally {
db.close();
}
return withTelemetryDB(
async (db) => db.count(TELEMETRY_EVENT_OBJECT_STORE),
IDB_OPERATION[DATABASE_NAME.TELEMETRY].COUNT,
);
}

/**
* Clears all event entries from the database.
*/
export async function clear(): Promise<void> {
const db = await openTelemetryDB();
try {
await withTelemetryDB(async (db) => {
await db.clear(TELEMETRY_EVENT_OBJECT_STORE);
} finally {
db.close();
}
}, IDB_OPERATION[DATABASE_NAME.TELEMETRY].CLEAR);
}

async function flush(): Promise<void> {
Expand Down
11 changes: 10 additions & 1 deletion src/utils/idbUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const DATABASE_NAME = {
LOG: "LOG",
TRACE: "TRACE",
PACKAGE_REGISTRY: "BRICK_REGISTRY",
TELEMETRY: "telemetrydb",
} as const;

export const IDB_OPERATION = {
Expand Down Expand Up @@ -43,6 +44,13 @@ export const IDB_OPERATION = {
REPLACE_ALL: "replaceAll",
FIND: "find",
},
[DATABASE_NAME.TELEMETRY]: {
ADD_EVENT: "addEvent",
FLUSH_EVENTS: "flushEvents",
RECREATE_DB: "recreateDB",
COUNT: "count",
CLEAR: "clear",
},
} as const satisfies Record<
ValueOf<typeof DATABASE_NAME>,
Record<string, string>
Expand All @@ -51,7 +59,8 @@ export const IDB_OPERATION = {
type OperationNames =
| ValueOf<(typeof IDB_OPERATION)[typeof DATABASE_NAME.LOG]>
| ValueOf<(typeof IDB_OPERATION)[typeof DATABASE_NAME.TRACE]>
| ValueOf<(typeof IDB_OPERATION)[typeof DATABASE_NAME.PACKAGE_REGISTRY]>;
| ValueOf<(typeof IDB_OPERATION)[typeof DATABASE_NAME.PACKAGE_REGISTRY]>
| ValueOf<(typeof IDB_OPERATION)[typeof DATABASE_NAME.TELEMETRY]>;

// IDB Quota Error message strings
const QUOTA_ERRORS = [
Expand Down

0 comments on commit cf3b817

Please sign in to comment.