mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Add Support Grist page and nudge
Summary: Adds a new Support Grist page (accessible only in grist-core), containing options to opt in to telemetry and sponsor Grist Labs on GitHub. A nudge is also shown in the doc menu, which can be collapsed or permanently dismissed. Test Plan: Browser and server tests. Reviewers: paulfitz, dsagal Reviewed By: paulfitz Subscribers: jarek, dsagal Differential Revision: https://phab.getgrist.com/D3926
This commit is contained in:
@@ -6,7 +6,7 @@ import { applyPatch } from 'app/gen-server/lib/TypeORMPatches';
|
||||
import { getMigrations, getOrCreateConnection, getTypeORMSettings,
|
||||
undoLastMigration, updateDb } from 'app/server/lib/dbUtils';
|
||||
import { getDatabaseUrl } from 'app/server/lib/serverUtils';
|
||||
import { getTelemetryLevel } from 'app/server/lib/Telemetry';
|
||||
import { getTelemetryPrefs } from 'app/server/lib/Telemetry';
|
||||
import { Gristifier } from 'app/server/utils/gristify';
|
||||
import { pruneActionHistory } from 'app/server/utils/pruneActionHistory';
|
||||
import * as commander from 'commander';
|
||||
@@ -81,12 +81,14 @@ export function addSettingsCommand(program: commander.Command,
|
||||
.action(showTelemetry);
|
||||
}
|
||||
|
||||
function showTelemetry(options: {
|
||||
async function showTelemetry(options: {
|
||||
json?: boolean,
|
||||
all?: boolean,
|
||||
}) {
|
||||
const contracts = TelemetryContracts;
|
||||
const levelName = getTelemetryLevel();
|
||||
const db = await getHomeDBManager();
|
||||
const prefs = await getTelemetryPrefs(db);
|
||||
const levelName = prefs.telemetryLevel.value;
|
||||
const level = Level[levelName];
|
||||
if (options.json) {
|
||||
console.log(JSON.stringify({
|
||||
|
||||
@@ -68,7 +68,6 @@ import {FormulaProperties, getFormulaProperties} from 'app/common/GranularAccess
|
||||
import {isHiddenCol} from 'app/common/gristTypes';
|
||||
import {commonUrls, parseUrlId} from 'app/common/gristUrls';
|
||||
import {byteString, countIf, retryOnce, safeJsonParse} from 'app/common/gutil';
|
||||
import {hashId} from 'app/common/hashingUtils';
|
||||
import {InactivityTimer} from 'app/common/InactivityTimer';
|
||||
import {Interval} from 'app/common/Interval';
|
||||
import * as roles from 'app/common/roles';
|
||||
@@ -1396,9 +1395,9 @@ export class ActiveDoc extends EventEmitter implements AssistanceDoc {
|
||||
const isTemplate = TEMPLATES_ORG_DOMAIN === doc.workspace.org.domain && doc.type !== 'tutorial';
|
||||
this.logTelemetryEvent(docSession, 'documentForked', {
|
||||
limited: {
|
||||
forkIdDigest: hashId(forkIds.forkId),
|
||||
forkDocIdDigest: hashId(forkIds.docId),
|
||||
trunkIdDigest: doc.trunkId ? hashId(doc.trunkId) : undefined,
|
||||
forkIdDigest: forkIds.forkId,
|
||||
forkDocIdDigest: forkIds.docId,
|
||||
trunkIdDigest: doc.trunkId,
|
||||
isTemplate,
|
||||
lastActivity: doc.updatedAt,
|
||||
},
|
||||
@@ -2540,7 +2539,7 @@ export class ActiveDoc extends EventEmitter implements AssistanceDoc {
|
||||
altSessionId ? {altSessionId} : {},
|
||||
{
|
||||
limited: {
|
||||
docIdDigest: hashId(this._docName),
|
||||
docIdDigest: this._docName,
|
||||
},
|
||||
full: {
|
||||
siteId: this._doc?.workspace.org.id,
|
||||
|
||||
@@ -9,7 +9,6 @@ import fetch, {Response as FetchResponse, RequestInit} from 'node-fetch';
|
||||
import {ApiError} from 'app/common/ApiError';
|
||||
import {getSlugIfNeeded, parseSubdomainStrictly, parseUrlId} from 'app/common/gristUrls';
|
||||
import {removeTrailingSlash} from 'app/common/gutil';
|
||||
import {hashId} from 'app/common/hashingUtils';
|
||||
import {LocalPlugin} from "app/common/plugin";
|
||||
import {TELEMETRY_TEMPLATE_SIGNUP_COOKIE_NAME} from 'app/common/Telemetry';
|
||||
import {Document as APIDocument} from 'app/common/UserAPI';
|
||||
@@ -304,13 +303,13 @@ export function attachAppEndpoint(options: AttachOptions): void {
|
||||
}
|
||||
|
||||
const isPublic = ((doc as unknown) as APIDocument).public ?? false;
|
||||
const isSnapshot = parseUrlId(urlId).snapshotId;
|
||||
const isSnapshot = Boolean(parseUrlId(urlId).snapshotId);
|
||||
// TODO: Need a more precise way to identify a template. (This org now also has tutorials.)
|
||||
const isTemplate = TEMPLATES_ORG_DOMAIN === doc.workspace.org.domain && doc.type !== 'tutorial';
|
||||
if (isPublic || isTemplate) {
|
||||
gristServer.getTelemetry().logEvent('documentOpened', {
|
||||
limited: {
|
||||
docIdDigest: hashId(docId),
|
||||
docIdDigest: docId,
|
||||
access: doc.access,
|
||||
isPublic,
|
||||
isSnapshot,
|
||||
|
||||
@@ -11,7 +11,6 @@ import {
|
||||
import {isRaisedException} from "app/common/gristTypes";
|
||||
import {buildUrlId, parseUrlId} from "app/common/gristUrls";
|
||||
import {isAffirmative} from "app/common/gutil";
|
||||
import {hashId} from "app/common/hashingUtils";
|
||||
import {SchemaTypes} from "app/common/schema";
|
||||
import {SortFunc} from 'app/common/SortFunc';
|
||||
import {Sort} from 'app/common/SortSpec';
|
||||
@@ -918,8 +917,8 @@ export class DocWorkerApi {
|
||||
const {forkId} = parseUrlId(scope.urlId);
|
||||
activeDoc.logTelemetryEvent(docSession, 'tutorialRestarted', {
|
||||
full: {
|
||||
tutorialForkIdDigest: forkId ? hashId(forkId) : undefined,
|
||||
tutorialTrunkIdDigest: hashId(tutorialTrunkId),
|
||||
tutorialForkIdDigest: forkId,
|
||||
tutorialTrunkIdDigest: tutorialTrunkId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import {encodeUrl, getSlugIfNeeded, GristDeploymentType, GristDeploymentTypes,
|
||||
GristLoadConfig, IGristUrlState, isOrgInPathOnly, parseSubdomain,
|
||||
sanitizePathTail} from 'app/common/gristUrls';
|
||||
import {getOrgUrlInfo} from 'app/common/gristUrls';
|
||||
import {InstallProperties} from 'app/common/InstallAPI';
|
||||
import {UserProfile} from 'app/common/LoginSessionAPI';
|
||||
import {tbind} from 'app/common/tbind';
|
||||
import * as version from 'app/common/version';
|
||||
@@ -50,14 +51,15 @@ import {getAppPathTo, getAppRoot, getUnpackedAppRoot} from 'app/server/lib/place
|
||||
import {addPluginEndpoints, limitToPlugins} from 'app/server/lib/PluginEndpoint';
|
||||
import {PluginManager} from 'app/server/lib/PluginManager';
|
||||
import * as ProcessMonitor from 'app/server/lib/ProcessMonitor';
|
||||
import {adaptServerUrl, getOrgUrl, getOriginUrl, getScope, optStringParam,
|
||||
RequestWithGristInfo, stringParam, TEST_HTTPS_OFFSET, trustOrigin} from 'app/server/lib/requestUtils';
|
||||
import {adaptServerUrl, getOrgUrl, getOriginUrl, getScope, isDefaultUser, optStringParam,
|
||||
RequestWithGristInfo, sendOkReply, stringParam, TEST_HTTPS_OFFSET,
|
||||
trustOrigin} from 'app/server/lib/requestUtils';
|
||||
import {ISendAppPageOptions, makeGristConfig, makeMessagePage, makeSendAppPage} from 'app/server/lib/sendAppPage';
|
||||
import {getDatabaseUrl, listenPromise} from 'app/server/lib/serverUtils';
|
||||
import {Sessions} from 'app/server/lib/Sessions';
|
||||
import * as shutdown from 'app/server/lib/shutdown';
|
||||
import {TagChecker} from 'app/server/lib/TagChecker';
|
||||
import {ITelemetry} from 'app/server/lib/Telemetry';
|
||||
import {getTelemetryPrefs, ITelemetry} from 'app/server/lib/Telemetry';
|
||||
import {startTestingHooks} from 'app/server/lib/TestingHooks';
|
||||
import {getTestLoginSystem} from 'app/server/lib/TestLogin';
|
||||
import {addUploadRoute} from 'app/server/lib/uploads';
|
||||
@@ -706,11 +708,17 @@ export class FlexServer implements GristServer {
|
||||
});
|
||||
}
|
||||
|
||||
public addTelemetry() {
|
||||
public async addTelemetry() {
|
||||
if (this._check('telemetry', 'homedb', 'json', 'api-mw')) { return; }
|
||||
|
||||
this._telemetry = this.create.Telemetry(this._dbManager, this);
|
||||
this._telemetry.addEndpoints(this.app);
|
||||
this._telemetry.addPages(this.app, [
|
||||
this._redirectToHostMiddleware,
|
||||
this._userIdMiddleware,
|
||||
this._redirectToLoginWithoutExceptionsMiddleware,
|
||||
]);
|
||||
await this._telemetry.start();
|
||||
|
||||
// Start up a monitor for memory and cpu usage.
|
||||
this._processMonitorStop = ProcessMonitor.start(this._telemetry);
|
||||
@@ -1198,7 +1206,7 @@ export class FlexServer implements GristServer {
|
||||
];
|
||||
|
||||
this.app.get('/account', ...middleware, expressWrap(async (req, resp) => {
|
||||
return this._sendAppPage(req, resp, {path: 'account.html', status: 200, config: {}});
|
||||
return this._sendAppPage(req, resp, {path: 'app.html', status: 200, config: {}});
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -1460,6 +1468,43 @@ export class FlexServer implements GristServer {
|
||||
addGoogleAuthEndpoint(this.app, messagePage);
|
||||
}
|
||||
|
||||
public addInstallEndpoints() {
|
||||
if (this._check('install')) { return; }
|
||||
|
||||
const isManager = expressWrap(
|
||||
(req: express.Request, _res: express.Response, next: express.NextFunction) => {
|
||||
if (!isDefaultUser(req)) { throw new ApiError('Access denied', 403); }
|
||||
|
||||
next();
|
||||
}
|
||||
);
|
||||
|
||||
this.app.get('/api/install/prefs', expressWrap(async (_req, resp) => {
|
||||
const activation = await this._activations.current();
|
||||
|
||||
return sendOkReply(null, resp, {
|
||||
telemetry: await getTelemetryPrefs(this._dbManager, activation),
|
||||
});
|
||||
}));
|
||||
|
||||
this.app.patch('/api/install/prefs', isManager, expressWrap(async (req, resp) => {
|
||||
const props = {prefs: req.body};
|
||||
const activation = await this._activations.current();
|
||||
activation.checkProperties(props);
|
||||
activation.updateFromProperties(props);
|
||||
await activation.save();
|
||||
|
||||
if ((props as Partial<InstallProperties>).prefs?.telemetry) {
|
||||
// Make sure the Telemetry singleton picks up the changes to telemetry preferences.
|
||||
// TODO: if there are multiple home server instances, notify them all of changes to
|
||||
// preferences (via Redis Pub/Sub).
|
||||
await this._telemetry.fetchTelemetryPrefs();
|
||||
}
|
||||
|
||||
return resp.status(200).send();
|
||||
}));
|
||||
}
|
||||
|
||||
// Get the HTML template sent for document pages.
|
||||
public async getDocTemplate(): Promise<DocTemplate> {
|
||||
const page = await fse.readFile(path.join(getAppPathTo(this.appRoot, 'static'),
|
||||
|
||||
@@ -138,8 +138,11 @@ export function createDummyGristServer(): GristServer {
|
||||
|
||||
export function createDummyTelemetry(): ITelemetry {
|
||||
return {
|
||||
logEvent() { return Promise.resolve(); },
|
||||
addEndpoints() { /* do nothing */ },
|
||||
getTelemetryLevel() { return 'off'; },
|
||||
addPages() { /* do nothing */ },
|
||||
start() { return Promise.resolve(); },
|
||||
logEvent() { return Promise.resolve(); },
|
||||
getTelemetryConfig() { return undefined; },
|
||||
fetchTelemetryPrefs() { return Promise.resolve(); },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import {ApiError} from 'app/common/ApiError';
|
||||
import {TelemetryConfig} from 'app/common/gristUrls';
|
||||
import {assertIsDefined} from 'app/common/gutil';
|
||||
import {
|
||||
buildTelemetryEventChecker,
|
||||
filterMetadata,
|
||||
removeNullishKeys,
|
||||
Level,
|
||||
TelemetryContracts,
|
||||
TelemetryEvent,
|
||||
TelemetryEventChecker,
|
||||
TelemetryEvents,
|
||||
@@ -11,18 +13,28 @@ import {
|
||||
TelemetryMetadata,
|
||||
TelemetryMetadataByLevel,
|
||||
} from 'app/common/Telemetry';
|
||||
import {HomeDBManager, HomeDBTelemetryEvents} from 'app/gen-server/lib/HomeDBManager';
|
||||
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';
|
||||
import {RequestWithLogin} from 'app/server/lib/Authorizer';
|
||||
import {expressWrap} from 'app/server/lib/expressWrap';
|
||||
import {GristServer} from 'app/server/lib/GristServer';
|
||||
import {hashId} from 'app/server/lib/hashingUtils';
|
||||
import {LogMethods} from 'app/server/lib/LogMethods';
|
||||
import {stringParam} from 'app/server/lib/requestUtils';
|
||||
import * as express from 'express';
|
||||
import fetch from 'node-fetch';
|
||||
import merge = require('lodash/merge');
|
||||
import pickBy = require('lodash/pickBy');
|
||||
|
||||
export interface ITelemetry {
|
||||
start(): Promise<void>;
|
||||
logEvent(name: TelemetryEvent, metadata?: TelemetryMetadataByLevel): Promise<void>;
|
||||
addEndpoints(app: express.Express): void;
|
||||
getTelemetryLevel(): TelemetryLevel;
|
||||
addPages(app: express.Express, middleware: express.RequestHandler[]): void;
|
||||
getTelemetryConfig(): TelemetryConfig | undefined;
|
||||
fetchTelemetryPrefs(): Promise<void>;
|
||||
}
|
||||
|
||||
const MAX_PENDING_FORWARD_EVENT_REQUESTS = 25;
|
||||
@@ -31,26 +43,30 @@ const MAX_PENDING_FORWARD_EVENT_REQUESTS = 25;
|
||||
* Manages telemetry for Grist.
|
||||
*/
|
||||
export class Telemetry implements ITelemetry {
|
||||
private _telemetryLevel: TelemetryLevel;
|
||||
private _deploymentType = this._gristServer.getDeploymentType();
|
||||
private _shouldForwardTelemetryEvents = this._deploymentType !== 'saas';
|
||||
private _forwardTelemetryEventsUrl = process.env.GRIST_TELEMETRY_URL ||
|
||||
'https://telemetry.getgrist.com/api/telemetry';
|
||||
private _activation: Activation | undefined;
|
||||
private readonly _deploymentType = this._gristServer.getDeploymentType();
|
||||
|
||||
private _telemetryPrefs: TelemetryPrefsWithSources | undefined;
|
||||
|
||||
private readonly _shouldForwardTelemetryEvents = this._deploymentType !== 'saas';
|
||||
private readonly _forwardTelemetryEventsUrl = process.env.GRIST_TELEMETRY_URL ||
|
||||
'https://telemetry.getgrist.com/api/telemetry';
|
||||
private _numPendingForwardEventRequests = 0;
|
||||
|
||||
private _installationId: string | undefined;
|
||||
|
||||
private _logger = new LogMethods('Telemetry ', () => ({}));
|
||||
private _telemetryLogger = new LogMethods('Telemetry ', () => ({
|
||||
private readonly _logger = new LogMethods('Telemetry ', () => ({}));
|
||||
private readonly _telemetryLogger = new LogMethods('Telemetry ', () => ({
|
||||
eventType: 'telemetry',
|
||||
}));
|
||||
|
||||
private _checkEvent: TelemetryEventChecker | undefined;
|
||||
private _checkTelemetryEvent: TelemetryEventChecker | undefined;
|
||||
|
||||
constructor(private _dbManager: HomeDBManager, private _gristServer: GristServer) {
|
||||
this._initialize().catch((e) => {
|
||||
this._logger.error(undefined, 'failed to initialize', e);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public async start() {
|
||||
await this.fetchTelemetryPrefs();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,19 +112,29 @@ export class Telemetry implements ITelemetry {
|
||||
event: TelemetryEvent,
|
||||
metadata?: TelemetryMetadataByLevel
|
||||
) {
|
||||
if (this._telemetryLevel === 'off') { return; }
|
||||
if (!this._checkTelemetryEvent) {
|
||||
this._logger.error(undefined, 'logEvent called but telemetry event checker is undefined');
|
||||
return;
|
||||
}
|
||||
|
||||
metadata = filterMetadata(metadata, this._telemetryLevel);
|
||||
const prefs = this._telemetryPrefs;
|
||||
if (!prefs) {
|
||||
this._logger.error(undefined, 'logEvent called but telemetry preferences are undefined');
|
||||
return;
|
||||
}
|
||||
|
||||
const {telemetryLevel} = prefs;
|
||||
if (TelemetryContracts[event] && TelemetryContracts[event].minimumTelemetryLevel > Level[telemetryLevel.value]) {
|
||||
return;
|
||||
}
|
||||
|
||||
metadata = filterMetadata(metadata, telemetryLevel.value);
|
||||
this._checkTelemetryEvent(event, metadata);
|
||||
|
||||
if (this._shouldForwardTelemetryEvents) {
|
||||
await this._forwardEvent(event, metadata);
|
||||
} else {
|
||||
this._telemetryLogger.rawLog('info', null, event, {
|
||||
eventName: event,
|
||||
eventSource: `grist-${this._deploymentType}`,
|
||||
...metadata,
|
||||
});
|
||||
this._logEvent(event, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +151,7 @@ export class Telemetry implements ITelemetry {
|
||||
* source. Otherwise, the event will only be logged after passing various
|
||||
* checks.
|
||||
*/
|
||||
app.post('/api/telemetry', async (req, resp) => {
|
||||
app.post('/api/telemetry', expressWrap(async (req, resp) => {
|
||||
const mreq = req as RequestWithLogin;
|
||||
const event = stringParam(req.body.event, 'event', TelemetryEvents.values);
|
||||
if ('eventSource' in req.body.metadata) {
|
||||
@@ -135,11 +161,12 @@ export class Telemetry implements ITelemetry {
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
this._assertTelemetryIsReady();
|
||||
await this.logEvent(event as TelemetryEvent, merge(
|
||||
{
|
||||
limited: {
|
||||
eventSource: `grist-${this._deploymentType}`,
|
||||
...(this._deploymentType !== 'saas' ? {installationId: this._installationId} : {}),
|
||||
...(this._deploymentType !== 'saas' ? {installationId: this._activation!.id} : {}),
|
||||
},
|
||||
full: {
|
||||
userId: mreq.userId,
|
||||
@@ -154,38 +181,51 @@ export class Telemetry implements ITelemetry {
|
||||
}
|
||||
}
|
||||
return resp.status(200).send();
|
||||
}));
|
||||
}
|
||||
|
||||
public addPages(app: express.Application, middleware: express.RequestHandler[]) {
|
||||
if (this._deploymentType === 'core') {
|
||||
app.get('/support-grist', ...middleware, expressWrap(async (req, resp) => {
|
||||
return this._gristServer.sendAppPage(req, resp,
|
||||
{path: 'app.html', status: 200, config: {}});
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
public getTelemetryConfig(): TelemetryConfig | undefined {
|
||||
const prefs = this._telemetryPrefs;
|
||||
if (!prefs) {
|
||||
this._logger.error(undefined, 'getTelemetryConfig called but telemetry preferences are undefined');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
telemetryLevel: prefs.telemetryLevel.value,
|
||||
};
|
||||
}
|
||||
|
||||
public async fetchTelemetryPrefs() {
|
||||
this._activation = await this._gristServer.getActivations().current();
|
||||
await this._fetchTelemetryPrefs();
|
||||
}
|
||||
|
||||
private async _fetchTelemetryPrefs() {
|
||||
this._telemetryPrefs = await getTelemetryPrefs(this._dbManager, this._activation);
|
||||
this._checkTelemetryEvent = buildTelemetryEventChecker(this._telemetryPrefs.telemetryLevel.value);
|
||||
}
|
||||
|
||||
private _logEvent(
|
||||
event: TelemetryEvent,
|
||||
metadata?: TelemetryMetadata
|
||||
) {
|
||||
this._telemetryLogger.rawLog('info', null, event, {
|
||||
eventName: event,
|
||||
eventSource: `grist-${this._deploymentType}`,
|
||||
...metadata,
|
||||
});
|
||||
}
|
||||
|
||||
public getTelemetryLevel() {
|
||||
return this._telemetryLevel;
|
||||
}
|
||||
|
||||
private async _initialize() {
|
||||
this._telemetryLevel = getTelemetryLevel();
|
||||
if (process.env.GRIST_TELEMETRY_LEVEL !== undefined) {
|
||||
this._checkTelemetryEvent = buildTelemetryEventChecker(this._telemetryLevel);
|
||||
}
|
||||
|
||||
const {id} = await this._gristServer.getActivations().current();
|
||||
this._installationId = id;
|
||||
|
||||
for (const event of HomeDBTelemetryEvents.values) {
|
||||
this._dbManager.on(event, async (metadata) => {
|
||||
this.logEvent(event, metadata).catch(e =>
|
||||
this._logger.error(undefined, `failed to log telemetry event ${event}`, e));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private _checkTelemetryEvent(event: TelemetryEvent, metadata?: TelemetryMetadata) {
|
||||
if (!this._checkEvent) {
|
||||
throw new Error('Telemetry._checkEvent is undefined');
|
||||
}
|
||||
|
||||
this._checkEvent(event, metadata);
|
||||
}
|
||||
|
||||
private async _forwardEvent(
|
||||
event: TelemetryEvent,
|
||||
metadata?: TelemetryMetadata
|
||||
@@ -198,7 +238,7 @@ export class Telemetry implements ITelemetry {
|
||||
|
||||
try {
|
||||
this._numPendingForwardEventRequests += 1;
|
||||
await this._postJsonPayload(JSON.stringify({event, metadata}));
|
||||
await this._doForwardEvent(JSON.stringify({event, metadata}));
|
||||
} catch (e) {
|
||||
this._logger.error(undefined, `failed to forward telemetry event ${event}`, e);
|
||||
} finally {
|
||||
@@ -206,7 +246,7 @@ export class Telemetry implements ITelemetry {
|
||||
}
|
||||
}
|
||||
|
||||
private async _postJsonPayload(payload: string) {
|
||||
private async _doForwardEvent(payload: string) {
|
||||
await fetch(this._forwardTelemetryEventsUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -215,12 +255,90 @@ export class Telemetry implements ITelemetry {
|
||||
body: payload,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function getTelemetryLevel(): TelemetryLevel {
|
||||
if (process.env.GRIST_TELEMETRY_LEVEL !== undefined) {
|
||||
return TelemetryLevels.check(process.env.GRIST_TELEMETRY_LEVEL);
|
||||
} else {
|
||||
return 'off';
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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]};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
12
app/server/lib/hashingUtils.ts
Normal file
12
app/server/lib/hashingUtils.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import {createHash} from 'crypto';
|
||||
|
||||
/**
|
||||
* Returns a hash of `id` prefixed with the first 4 characters of `id`. The first 4
|
||||
* characters are included to assist with troubleshooting.
|
||||
*
|
||||
* Useful for situations where potentially sensitive identifiers are logged, such as
|
||||
* doc ids of docs that have public link sharing enabled.
|
||||
*/
|
||||
export function hashId(id: string): string {
|
||||
return `${id.slice(0, 4)}:${createHash('sha256').update(id.slice(4)).digest('base64')}`;
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import {ApiError} from 'app/common/ApiError';
|
||||
import {DEFAULT_HOME_SUBDOMAIN, isOrgInPathOnly, parseSubdomain, sanitizePathTail} from 'app/common/gristUrls';
|
||||
import * as gutil from 'app/common/gutil';
|
||||
import {DocScope, QueryResult, Scope} from 'app/gen-server/lib/HomeDBManager';
|
||||
import {getUserId, RequestWithLogin} from 'app/server/lib/Authorizer';
|
||||
import {getUser, getUserId, RequestWithLogin} from 'app/server/lib/Authorizer';
|
||||
import {RequestWithOrg} from 'app/server/lib/extractOrg';
|
||||
import {RequestWithGrist} from 'app/server/lib/GristServer';
|
||||
import log from 'app/server/lib/log';
|
||||
@@ -352,3 +352,9 @@ export function addAbortHandler(req: Request, res: Writable, op: () => void) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function isDefaultUser(req: Request) {
|
||||
const defaultEmail = process.env.GRIST_DEFAULT_EMAIL;
|
||||
const {loginEmail} = getUser(req);
|
||||
return defaultEmail && defaultEmail === loginEmail;
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ export function makeGristConfig(options: MakeGristConfigOptons): GristLoadConfig
|
||||
featureFormulaAssistant: isAffirmative(process.env.GRIST_FORMULA_ASSISTANT),
|
||||
supportEmail: SUPPORT_EMAIL,
|
||||
userLocale: (req as RequestWithLogin | undefined)?.user?.options?.locale,
|
||||
telemetry: server ? getTelemetryConfig(server) : undefined,
|
||||
telemetry: server?.getTelemetry().getTelemetryConfig(),
|
||||
deploymentType: server?.getDeploymentType(),
|
||||
...extra,
|
||||
};
|
||||
@@ -163,13 +163,6 @@ function getFeatures(): IFeature[] {
|
||||
return Features.checkAll(difference(enabledFeatures, disabledFeatures));
|
||||
}
|
||||
|
||||
function getTelemetryConfig(server: GristServer) {
|
||||
const telemetry = server.getTelemetry();
|
||||
return {
|
||||
telemetryLevel: telemetry.getTelemetryLevel(),
|
||||
};
|
||||
}
|
||||
|
||||
function configuredPageTitleSuffix() {
|
||||
const result = process.env.GRIST_PAGE_TITLE_SUFFIX;
|
||||
return result === "_blank" ? "" : result;
|
||||
|
||||
@@ -119,7 +119,7 @@ export async function main(port: number, serverTypes: ServerType[],
|
||||
server.addHomeApi();
|
||||
server.addBillingApi();
|
||||
server.addNotifier();
|
||||
server.addTelemetry();
|
||||
await server.addTelemetry();
|
||||
await server.addHousekeeper();
|
||||
await server.addLoginRoutes();
|
||||
server.addAccountPage();
|
||||
@@ -127,11 +127,12 @@ export async function main(port: number, serverTypes: ServerType[],
|
||||
server.addWelcomePaths();
|
||||
server.addLogEndpoint();
|
||||
server.addGoogleAuthEndpoint();
|
||||
server.addInstallEndpoints();
|
||||
}
|
||||
|
||||
if (includeDocs) {
|
||||
server.addJsonSupport();
|
||||
server.addTelemetry();
|
||||
await server.addTelemetry();
|
||||
await server.addDoc();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user