2023-06-06 17:08:50 +00:00
|
|
|
import {ApiError} from 'app/common/ApiError';
|
2023-07-04 21:21:34 +00:00
|
|
|
import {TelemetryConfig} from 'app/common/gristUrls';
|
|
|
|
import {assertIsDefined} from 'app/common/gutil';
|
2023-06-06 17:08:50 +00:00
|
|
|
import {
|
|
|
|
buildTelemetryEventChecker,
|
2023-07-04 21:21:34 +00:00
|
|
|
Level,
|
|
|
|
TelemetryContracts,
|
2023-06-06 17:08:50 +00:00
|
|
|
TelemetryEvent,
|
|
|
|
TelemetryEventChecker,
|
|
|
|
TelemetryEvents,
|
|
|
|
TelemetryLevel,
|
|
|
|
TelemetryLevels,
|
|
|
|
TelemetryMetadata,
|
|
|
|
TelemetryMetadataByLevel,
|
2023-07-20 14:25:26 +00:00
|
|
|
TelemetryRetentionPeriod,
|
2023-06-06 17:08:50 +00:00
|
|
|
} from 'app/common/Telemetry';
|
2023-07-04 21:21:34 +00:00
|
|
|
import {TelemetryPrefsWithSources} from 'app/common/InstallAPI';
|
|
|
|
import {Activation} from 'app/gen-server/entity/Activation';
|
|
|
|
import {Activations} from 'app/gen-server/lib/Activations';
|
|
|
|
import {HomeDBManager} from 'app/gen-server/lib/HomeDBManager';
|
2023-06-06 17:08:50 +00:00
|
|
|
import {RequestWithLogin} from 'app/server/lib/Authorizer';
|
2023-11-01 13:54:19 +00:00
|
|
|
import {getDocSessionUser, OptDocSession} from 'app/server/lib/DocSession';
|
2023-07-04 21:21:34 +00:00
|
|
|
import {expressWrap} from 'app/server/lib/expressWrap';
|
2023-06-06 17:08:50 +00:00
|
|
|
import {GristServer} from 'app/server/lib/GristServer';
|
2023-07-04 21:21:34 +00:00
|
|
|
import {hashId} from 'app/server/lib/hashingUtils';
|
2023-06-06 17:08:50 +00:00
|
|
|
import {LogMethods} from 'app/server/lib/LogMethods';
|
|
|
|
import {stringParam} from 'app/server/lib/requestUtils';
|
2023-11-01 13:54:19 +00:00
|
|
|
import {getLogMetaFromDocSession} from 'app/server/lib/serverUtils';
|
2023-06-06 17:08:50 +00:00
|
|
|
import * as express from 'express';
|
2023-07-04 21:21:34 +00:00
|
|
|
import fetch from 'node-fetch';
|
2023-06-06 17:08:50 +00:00
|
|
|
import merge = require('lodash/merge');
|
2023-07-04 21:21:34 +00:00
|
|
|
import pickBy = require('lodash/pickBy');
|
2023-06-06 17:08:50 +00:00
|
|
|
|
2023-11-01 13:54:19 +00:00
|
|
|
type RequestOrSession = RequestWithLogin | OptDocSession | null;
|
|
|
|
|
2023-06-06 17:08:50 +00:00
|
|
|
export interface ITelemetry {
|
2023-07-04 21:21:34 +00:00
|
|
|
start(): Promise<void>;
|
2023-11-01 13:54:19 +00:00
|
|
|
logEvent(
|
|
|
|
requestOrSession: RequestOrSession,
|
|
|
|
name: TelemetryEvent,
|
|
|
|
metadata?: TelemetryMetadataByLevel
|
|
|
|
): void;
|
|
|
|
logEventAsync(
|
|
|
|
requestOrSession: RequestOrSession,
|
|
|
|
name: TelemetryEvent,
|
|
|
|
metadata?: TelemetryMetadataByLevel
|
|
|
|
): Promise<void>;
|
2023-11-28 15:41:59 +00:00
|
|
|
shouldLogEvent(name: TelemetryEvent): boolean;
|
2023-06-06 17:08:50 +00:00
|
|
|
addEndpoints(app: express.Express): void;
|
2023-07-04 21:21:34 +00:00
|
|
|
addPages(app: express.Express, middleware: express.RequestHandler[]): void;
|
2023-11-01 13:54:19 +00:00
|
|
|
getTelemetryConfig(requestOrSession?: RequestOrSession): TelemetryConfig | undefined;
|
2023-07-04 21:21:34 +00:00
|
|
|
fetchTelemetryPrefs(): Promise<void>;
|
2023-06-06 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 16:32:40 +00:00
|
|
|
const MAX_PENDING_FORWARD_EVENT_REQUESTS = 25;
|
|
|
|
|
2023-06-06 17:08:50 +00:00
|
|
|
/**
|
|
|
|
* Manages telemetry for Grist.
|
|
|
|
*/
|
|
|
|
export class Telemetry implements ITelemetry {
|
2023-07-04 21:21:34 +00:00
|
|
|
private _activation: Activation | undefined;
|
|
|
|
private _telemetryPrefs: TelemetryPrefsWithSources | undefined;
|
2023-07-20 14:25:26 +00:00
|
|
|
private readonly _deploymentType = this._gristServer.getDeploymentType();
|
2023-07-04 21:21:34 +00:00
|
|
|
private readonly _shouldForwardTelemetryEvents = this._deploymentType !== 'saas';
|
|
|
|
private readonly _forwardTelemetryEventsUrl = process.env.GRIST_TELEMETRY_URL ||
|
|
|
|
'https://telemetry.getgrist.com/api/telemetry';
|
2023-06-09 16:32:40 +00:00
|
|
|
private _numPendingForwardEventRequests = 0;
|
2023-11-01 13:54:19 +00:00
|
|
|
private readonly _logger = new LogMethods('Telemetry ', (requestOrSession: RequestOrSession | undefined) =>
|
|
|
|
this._getLogMeta(requestOrSession));
|
2023-07-20 14:25:26 +00:00
|
|
|
private readonly _telemetryLogger = new LogMethods<string>('Telemetry ', (eventType) => ({
|
|
|
|
eventType,
|
2023-06-06 17:08:50 +00:00
|
|
|
}));
|
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
private _checkTelemetryEvent: TelemetryEventChecker | undefined;
|
2023-06-06 17:08:50 +00:00
|
|
|
|
|
|
|
constructor(private _dbManager: HomeDBManager, private _gristServer: GristServer) {
|
2023-07-04 21:21:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public async start() {
|
|
|
|
await this.fetchTelemetryPrefs();
|
2023-06-06 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a telemetry `event` and its `metadata`.
|
|
|
|
*
|
|
|
|
* Depending on the deployment type, this will either forward the
|
|
|
|
* data to an endpoint (set via GRIST_TELEMETRY_URL) or log it
|
|
|
|
* directly. In hosted Grist, telemetry is logged directly, and
|
|
|
|
* subsequently sent to an OpenSearch instance via CloudWatch. In
|
|
|
|
* other deployment types, telemetry is forwarded to an endpoint
|
|
|
|
* of hosted Grist, which then handles logging to OpenSearch.
|
|
|
|
*
|
|
|
|
* Note that `metadata` is grouped by telemetry level, with only the
|
|
|
|
* groups meeting the current telemetry level being included in
|
|
|
|
* what's logged. If the current telemetry level is `off`, nothing
|
|
|
|
* will be logged. Otherwise, `metadata` will be filtered according
|
|
|
|
* to the current telemetry level, keeping only the groups that are
|
|
|
|
* less than or equal to the current level.
|
|
|
|
*
|
|
|
|
* Additionally, runtime checks are also performed to verify that the
|
|
|
|
* event and metadata being passed in are being logged appropriately
|
|
|
|
* for the configured telemetry level. If any checks fail, an error
|
|
|
|
* is thrown.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* The following will only log the `rowCount` if the telemetry level is set
|
|
|
|
* to `limited`, and will log both the `method` and `userId` if the telemetry
|
|
|
|
* level is set to `full`:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* logEvent('documentUsage', {
|
|
|
|
* limited: {
|
|
|
|
* rowCount: 123,
|
|
|
|
* },
|
|
|
|
* full: {
|
|
|
|
* userId: 1586,
|
|
|
|
* },
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*/
|
2023-11-01 13:54:19 +00:00
|
|
|
public async logEventAsync(
|
|
|
|
requestOrSession: RequestOrSession,
|
2023-06-06 17:08:50 +00:00
|
|
|
event: TelemetryEvent,
|
|
|
|
metadata?: TelemetryMetadataByLevel
|
|
|
|
) {
|
2023-11-01 13:54:19 +00:00
|
|
|
await this._checkAndLogEvent(requestOrSession, event, metadata);
|
|
|
|
}
|
2023-06-06 17:08:50 +00:00
|
|
|
|
2023-11-01 13:54:19 +00:00
|
|
|
/**
|
|
|
|
* Non-async variant of `logEventAsync`.
|
|
|
|
*
|
|
|
|
* Convenient for fire-and-forget usage.
|
|
|
|
*/
|
|
|
|
public logEvent(
|
|
|
|
requestOrSession: RequestOrSession,
|
|
|
|
event: TelemetryEvent,
|
|
|
|
metadata?: TelemetryMetadataByLevel
|
|
|
|
) {
|
|
|
|
this.logEventAsync(requestOrSession, event, metadata).catch((e) => {
|
|
|
|
this._logger.error(requestOrSession, `failed to log telemetry event ${event}`, e);
|
|
|
|
});
|
2023-06-06 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public addEndpoints(app: express.Application) {
|
|
|
|
/**
|
|
|
|
* Logs telemetry events and their metadata.
|
|
|
|
*
|
|
|
|
* Clients of this endpoint may be external Grist instances, so the behavior
|
|
|
|
* varies based on the presence of an `eventSource` key in the event metadata.
|
|
|
|
*
|
|
|
|
* If an `eventSource` key is present, the telemetry event will be logged
|
|
|
|
* directly, as the request originated from an external source; runtime checks
|
|
|
|
* of telemetry data are skipped since they should have already occured at the
|
|
|
|
* source. Otherwise, the event will only be logged after passing various
|
|
|
|
* checks.
|
|
|
|
*/
|
2023-07-04 21:21:34 +00:00
|
|
|
app.post('/api/telemetry', expressWrap(async (req, resp) => {
|
2023-06-06 17:08:50 +00:00
|
|
|
const mreq = req as RequestWithLogin;
|
2023-09-05 18:27:35 +00:00
|
|
|
const event = stringParam(req.body.event, 'event', {allowed: TelemetryEvents.values}) as TelemetryEvent;
|
2023-11-01 13:54:19 +00:00
|
|
|
if ('eventSource' in (req.body.metadata ?? {})) {
|
2023-07-20 14:25:26 +00:00
|
|
|
this._telemetryLogger.rawLog('info', getEventType(event), event, {
|
2023-06-06 17:08:50 +00:00
|
|
|
...(removeNullishKeys(req.body.metadata)),
|
2023-07-20 14:25:26 +00:00
|
|
|
eventName: event,
|
2023-06-06 17:08:50 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
try {
|
2023-07-04 21:21:34 +00:00
|
|
|
this._assertTelemetryIsReady();
|
2023-11-01 13:54:19 +00:00
|
|
|
await this._checkAndLogEvent(mreq, event, merge(
|
2023-06-06 17:08:50 +00:00
|
|
|
{
|
|
|
|
full: {
|
|
|
|
userId: mreq.userId,
|
|
|
|
altSessionId: mreq.altSessionId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
req.body.metadata,
|
|
|
|
));
|
|
|
|
} catch (e) {
|
2023-11-01 13:54:19 +00:00
|
|
|
this._logger.error(mreq, `failed to log telemetry event ${event}`, e);
|
2023-06-06 17:08:50 +00:00
|
|
|
throw new ApiError(`Telemetry failed to log telemetry event ${event}`, 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp.status(200).send();
|
2023-07-04 21:21:34 +00:00
|
|
|
}));
|
2023-06-06 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
public addPages(app: express.Application, middleware: express.RequestHandler[]) {
|
|
|
|
if (this._deploymentType === 'core') {
|
2023-11-29 20:13:29 +00:00
|
|
|
app.get('/support', ...middleware, expressWrap(async (req, resp) => {
|
2023-07-04 21:21:34 +00:00
|
|
|
return this._gristServer.sendAppPage(req, resp,
|
|
|
|
{path: 'app.html', status: 200, config: {}});
|
|
|
|
}));
|
|
|
|
}
|
2023-06-06 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 13:54:19 +00:00
|
|
|
public getTelemetryConfig(requestOrSession?: RequestOrSession): TelemetryConfig | undefined {
|
2023-07-04 21:21:34 +00:00
|
|
|
const prefs = this._telemetryPrefs;
|
|
|
|
if (!prefs) {
|
2023-11-01 13:54:19 +00:00
|
|
|
this._logger.error(requestOrSession, 'getTelemetryConfig called but telemetry preferences are undefined');
|
2023-07-04 21:21:34 +00:00
|
|
|
return undefined;
|
2023-06-06 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
return {
|
|
|
|
telemetryLevel: prefs.telemetryLevel.value,
|
|
|
|
};
|
|
|
|
}
|
2023-06-06 17:08:50 +00:00
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
public async fetchTelemetryPrefs() {
|
|
|
|
this._activation = await this._gristServer.getActivations().current();
|
|
|
|
await this._fetchTelemetryPrefs();
|
2023-06-06 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 15:41:59 +00:00
|
|
|
// Checks if the event should be logged.
|
|
|
|
public shouldLogEvent(event: TelemetryEvent): boolean {
|
|
|
|
return Boolean(this._prepareToLogEvent(event));
|
|
|
|
}
|
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
private async _fetchTelemetryPrefs() {
|
|
|
|
this._telemetryPrefs = await getTelemetryPrefs(this._dbManager, this._activation);
|
|
|
|
this._checkTelemetryEvent = buildTelemetryEventChecker(this._telemetryPrefs.telemetryLevel.value);
|
|
|
|
}
|
2023-06-06 17:08:50 +00:00
|
|
|
|
2023-11-28 15:41:59 +00:00
|
|
|
private _prepareToLogEvent(
|
|
|
|
event: TelemetryEvent
|
|
|
|
): {checkTelemetryEvent: TelemetryEventChecker, telemetryLevel: TelemetryLevel}|undefined {
|
2023-11-01 13:54:19 +00:00
|
|
|
if (!this._checkTelemetryEvent) {
|
2023-11-28 15:41:59 +00:00
|
|
|
this._logger.error(null, 'telemetry event checker is undefined');
|
2023-11-01 13:54:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prefs = this._telemetryPrefs;
|
|
|
|
if (!prefs) {
|
2023-11-28 15:41:59 +00:00
|
|
|
this._logger.error(null, 'telemetry preferences are undefined');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const telemetryLevel = prefs.telemetryLevel.value;
|
|
|
|
if (TelemetryContracts[event] && TelemetryContracts[event].minimumTelemetryLevel > Level[telemetryLevel]) {
|
2023-11-01 13:54:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-28 15:41:59 +00:00
|
|
|
return {checkTelemetryEvent: this._checkTelemetryEvent, telemetryLevel};
|
|
|
|
}
|
2023-11-01 13:54:19 +00:00
|
|
|
|
2023-11-28 15:41:59 +00:00
|
|
|
private async _checkAndLogEvent(
|
|
|
|
requestOrSession: RequestOrSession,
|
|
|
|
event: TelemetryEvent,
|
|
|
|
metadata?: TelemetryMetadataByLevel
|
|
|
|
) {
|
|
|
|
const result = this._prepareToLogEvent(event);
|
|
|
|
if (!result) {
|
2023-11-01 13:54:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-28 15:41:59 +00:00
|
|
|
metadata = filterMetadata(metadata, result.telemetryLevel);
|
|
|
|
result.checkTelemetryEvent(event, metadata);
|
2023-11-01 13:54:19 +00:00
|
|
|
|
|
|
|
if (this._shouldForwardTelemetryEvents) {
|
|
|
|
await this._forwardEvent(requestOrSession, event, metadata);
|
|
|
|
} else {
|
|
|
|
this._logEvent(requestOrSession, event, metadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
private _logEvent(
|
2023-11-01 13:54:19 +00:00
|
|
|
requestOrSession: RequestOrSession,
|
2023-07-04 21:21:34 +00:00
|
|
|
event: TelemetryEvent,
|
|
|
|
metadata?: TelemetryMetadata
|
|
|
|
) {
|
2023-11-01 13:54:19 +00:00
|
|
|
let isInternalUser: boolean | undefined;
|
2023-11-15 20:20:51 +00:00
|
|
|
let isTeamSite: boolean | undefined;
|
2023-11-01 13:54:19 +00:00
|
|
|
if (requestOrSession) {
|
2023-11-15 20:20:51 +00:00
|
|
|
let email: string | undefined;
|
|
|
|
let org: string | undefined;
|
|
|
|
if ('get' in requestOrSession) {
|
|
|
|
email = requestOrSession.user?.loginEmail;
|
|
|
|
org = requestOrSession.org;
|
|
|
|
} else {
|
|
|
|
email = getDocSessionUser(requestOrSession)?.email;
|
|
|
|
org = requestOrSession.client?.getOrg() ?? requestOrSession.req?.org;
|
|
|
|
}
|
2023-11-01 13:54:19 +00:00
|
|
|
if (email) {
|
|
|
|
isInternalUser = email !== 'anon@getgrist.com' && email.endsWith('@getgrist.com');
|
|
|
|
}
|
2023-11-15 20:20:51 +00:00
|
|
|
if (org && !process.env.GRIST_SINGLE_ORG) {
|
|
|
|
isTeamSite = !this._dbManager.isMergedOrg(org);
|
|
|
|
}
|
2023-11-01 13:54:19 +00:00
|
|
|
}
|
|
|
|
const {category: eventCategory} = TelemetryContracts[event];
|
2023-07-20 14:25:26 +00:00
|
|
|
this._telemetryLogger.rawLog('info', getEventType(event), event, {
|
|
|
|
...metadata,
|
2023-07-04 21:21:34 +00:00
|
|
|
eventName: event,
|
2023-11-01 13:54:19 +00:00
|
|
|
...(eventCategory !== undefined ? {eventCategory} : undefined),
|
2023-07-04 21:21:34 +00:00
|
|
|
eventSource: `grist-${this._deploymentType}`,
|
2023-07-20 14:25:26 +00:00
|
|
|
installationId: this._activation!.id,
|
2023-11-01 13:54:19 +00:00
|
|
|
...(isInternalUser !== undefined ? {isInternalUser} : undefined),
|
2023-11-15 20:20:51 +00:00
|
|
|
...(isTeamSite !== undefined ? {isTeamSite} : undefined),
|
2023-07-04 21:21:34 +00:00
|
|
|
});
|
2023-06-06 17:08:50 +00:00
|
|
|
}
|
2023-06-09 16:32:40 +00:00
|
|
|
|
|
|
|
private async _forwardEvent(
|
2023-11-01 13:54:19 +00:00
|
|
|
requestOrSession: RequestOrSession,
|
2023-06-09 16:32:40 +00:00
|
|
|
event: TelemetryEvent,
|
|
|
|
metadata?: TelemetryMetadata
|
|
|
|
) {
|
|
|
|
if (this._numPendingForwardEventRequests === MAX_PENDING_FORWARD_EVENT_REQUESTS) {
|
2023-11-01 13:54:19 +00:00
|
|
|
this._logger.warn(requestOrSession, 'exceeded the maximum number of pending forwardEvent calls '
|
2023-06-09 16:32:40 +00:00
|
|
|
+ `(${MAX_PENDING_FORWARD_EVENT_REQUESTS}). Skipping forwarding of event ${event}.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this._numPendingForwardEventRequests += 1;
|
2023-11-01 13:54:19 +00:00
|
|
|
const {category: eventCategory} = TelemetryContracts[event];
|
2023-07-20 14:25:26 +00:00
|
|
|
await this._doForwardEvent(JSON.stringify({
|
|
|
|
event,
|
|
|
|
metadata: {
|
|
|
|
...metadata,
|
|
|
|
eventName: event,
|
2023-11-01 13:54:19 +00:00
|
|
|
...(eventCategory !== undefined ? {eventCategory} : undefined),
|
2023-07-20 14:25:26 +00:00
|
|
|
eventSource: `grist-${this._deploymentType}`,
|
|
|
|
installationId: this._activation!.id,
|
2023-11-01 13:54:19 +00:00
|
|
|
},
|
2023-07-20 14:25:26 +00:00
|
|
|
}));
|
2023-06-09 16:32:40 +00:00
|
|
|
} catch (e) {
|
2023-11-01 13:54:19 +00:00
|
|
|
this._logger.error(requestOrSession, `failed to forward telemetry event ${event}`, e);
|
2023-06-09 16:32:40 +00:00
|
|
|
} finally {
|
|
|
|
this._numPendingForwardEventRequests -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
private async _doForwardEvent(payload: string) {
|
2023-06-09 16:32:40 +00:00
|
|
|
await fetch(this._forwardTelemetryEventsUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: payload,
|
|
|
|
});
|
|
|
|
}
|
2023-07-04 21:21:34 +00:00
|
|
|
|
|
|
|
private _assertTelemetryIsReady() {
|
|
|
|
try {
|
|
|
|
assertIsDefined('activation', this._activation);
|
|
|
|
} catch (e) {
|
|
|
|
this._logger.error(null, 'activation is undefined', e);
|
|
|
|
throw new ApiError('Telemetry is not ready', 500);
|
|
|
|
}
|
|
|
|
}
|
2023-11-01 13:54:19 +00:00
|
|
|
|
|
|
|
private _getLogMeta(requestOrSession?: RequestOrSession) {
|
|
|
|
if (!requestOrSession) { return {}; }
|
|
|
|
|
|
|
|
if ('get' in requestOrSession) {
|
|
|
|
return {
|
|
|
|
org: requestOrSession.org,
|
|
|
|
email: requestOrSession.user?.loginEmail,
|
|
|
|
userId: requestOrSession.userId,
|
|
|
|
altSessionId: requestOrSession.altSessionId,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return getLogMetaFromDocSession(requestOrSession);
|
|
|
|
}
|
|
|
|
}
|
2023-06-06 17:08:50 +00:00
|
|
|
}
|
2023-06-09 21:30:38 +00:00
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
export async function getTelemetryPrefs(
|
|
|
|
db: HomeDBManager,
|
|
|
|
activation?: Activation
|
|
|
|
): Promise<TelemetryPrefsWithSources> {
|
|
|
|
const GRIST_TELEMETRY_LEVEL = process.env.GRIST_TELEMETRY_LEVEL;
|
|
|
|
if (GRIST_TELEMETRY_LEVEL !== undefined) {
|
|
|
|
const value = TelemetryLevels.check(GRIST_TELEMETRY_LEVEL);
|
|
|
|
return {
|
|
|
|
telemetryLevel: {
|
|
|
|
value,
|
|
|
|
source: 'environment-variable',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const {prefs} = activation ?? await new Activations(db).current();
|
|
|
|
return {
|
|
|
|
telemetryLevel: {
|
|
|
|
value: prefs?.telemetry?.telemetryLevel ?? 'off',
|
|
|
|
source: 'preferences',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new, filtered metadata object, or undefined if `metadata` is undefined.
|
|
|
|
*
|
|
|
|
* Filtering currently:
|
|
|
|
* - removes keys in groups that exceed `telemetryLevel`
|
|
|
|
* - removes keys with values of null or undefined
|
|
|
|
* - hashes the values of keys suffixed with "Digest" (e.g. doc ids, fork ids)
|
|
|
|
* - flattens the entire metadata object (i.e. removes the nesting of keys under
|
|
|
|
* "limited" or "full")
|
|
|
|
*/
|
|
|
|
export function filterMetadata(
|
|
|
|
metadata: TelemetryMetadataByLevel | undefined,
|
|
|
|
telemetryLevel: TelemetryLevel
|
|
|
|
): TelemetryMetadata | undefined {
|
|
|
|
if (!metadata) { return; }
|
|
|
|
|
|
|
|
let filteredMetadata: TelemetryMetadata = {};
|
|
|
|
for (const level of ['limited', 'full'] as const) {
|
|
|
|
if (Level[telemetryLevel] < Level[level]) { break; }
|
|
|
|
|
|
|
|
filteredMetadata = {...filteredMetadata, ...metadata[level]};
|
2023-06-09 21:30:38 +00:00
|
|
|
}
|
2023-07-04 21:21:34 +00:00
|
|
|
|
|
|
|
filteredMetadata = removeNullishKeys(filteredMetadata);
|
|
|
|
filteredMetadata = hashDigestKeys(filteredMetadata);
|
|
|
|
|
|
|
|
return filteredMetadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a copy of `object` with all null and undefined keys removed.
|
|
|
|
*/
|
|
|
|
export function removeNullishKeys(object: Record<string, any>) {
|
|
|
|
return pickBy(object, value => value !== null && value !== undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a copy of `metadata`, replacing the values of all keys suffixed
|
|
|
|
* with "Digest" with the result of hashing the value. The hash is prefixed with
|
|
|
|
* the first 4 characters of the original value, to assist with troubleshooting.
|
|
|
|
*/
|
|
|
|
export function hashDigestKeys(metadata: TelemetryMetadata): TelemetryMetadata {
|
|
|
|
const filteredMetadata: TelemetryMetadata = {};
|
|
|
|
Object.entries(metadata).forEach(([key, value]) => {
|
|
|
|
if (key.endsWith('Digest') && typeof value === 'string') {
|
|
|
|
filteredMetadata[key] = hashId(value);
|
|
|
|
} else {
|
|
|
|
filteredMetadata[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return filteredMetadata;
|
2023-06-09 21:30:38 +00:00
|
|
|
}
|
2023-07-20 14:25:26 +00:00
|
|
|
|
|
|
|
type TelemetryEventType = 'telemetry' | 'telemetry-short-retention';
|
|
|
|
|
|
|
|
const EventTypeByRetentionPeriod: Record<TelemetryRetentionPeriod, TelemetryEventType> = {
|
|
|
|
indefinitely: 'telemetry',
|
|
|
|
short: 'telemetry-short-retention',
|
|
|
|
};
|
|
|
|
|
|
|
|
function getEventType(event: TelemetryEvent) {
|
|
|
|
const {retentionPeriod} = TelemetryContracts[event];
|
|
|
|
return EventTypeByRetentionPeriod[retentionPeriod];
|
|
|
|
}
|