Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 66x 66x 7x 7x 59x 59x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 78x 78x 78x 2x 2x 15x 15x 15x 2x 2x 14x 1x 1x 13x 15x 6x 6x 6x 15x 15x 2x 2x 17x 17x 2x 2x 15x 15x 6x 6x 17x 9x 9x 6x 6x 17x 3x 3x 3x 17x 17x 2x 2x 45x 45x 45x 45x 19x 19x 17x 17x 5x 5x 5x 5x 5x 5x 4x 4x 1x 1x 17x 15x 15x 15x 45x 1x 1x 45x 45x 2x 2x 45x 45x 45x 9x 9x 9x 45x 2x 2x 2x 2x 45x 45x 45x 42x 42x 3x 3x 3x 3x 45x 45x 45x 2x 2x 45x 45x 45x 45x 2x 2x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 2x 2x 2x 2x 2x 45x 45x 43x 43x 43x 2x 2x 2x 45x 2x 2x 2x 61x 61x 61x 61x 61x 10x 10x 61x 51x 51x 2x 2x 853x 853x 1x 1x 852x 852x 6x 6x 6x 852x 853x 5x 5x 5x 847x 847x 847x 847x 847x 847x 847x 847x 847x 853x 853x 853x 853x 2x 2x 54x 54x 54x 4x 4x 4x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 54x 1x 1x 50x 50x 2x 2x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 7x 2x 2x 61x 61x 61x 61x 61x 61x 61x 61x 54x 54x 60x 7x 7x 61x 61x 61x 61x 61x 2x 2x 2x 57x 1x 1x 57x 1x 1x 55x 55x 55x 54x 54x 6x 6x 6x 6x 54x 55x 55x | import { analytics as analyticsConfig, defaultSettings } from '@/../config.json';
import { CACHE } from '@/constants/common';
import { canSendAnalytics, getFingerprintDistinctId, getSessionId } from '@/helpers/analytics';
import { scrubText, scrubValue } from '@/helpers/scrubPii';
import {
isRemoteLogLevel,
Logger,
type RemoteLogLevel,
type RemoteLogRecord,
type RemoteLogSink,
} from '@/utils/Logger';
import { cstorage } from '@/utils/storage';
import { isTraceId } from '@/utils/traceId';
import type { CaptureLogOptions, LogSeverityLevel, PostHog } from 'posthog-js';
/**
* Forwards `Logger` output to PostHog Logs.
*
* Uses the `posthog-js` bundle that inlines the logs extension and never loads external
* scripts (MV3 forbids them), and imports it lazily so the popup bundle and the service
* worker never evaluate it. Autocapture, session replay, surveys and feature flags are all
* switched off, so the only request is the logs ingest POST. Gated by the same
* `shareUsageData` opt-out as the rest of the analytics, and a no-op in the service worker,
* which has no `window`.
*
* @module remoteLogs
* @category Helpers
* @source
*/
/** Log-capture settings from `config.json` (`analytics.logs`). */
const LOG_CONFIG = analyticsConfig.logs;
/**
* How many logs a rate window lets through before `trace` and `debug` start being dropped. The rest
* of `maxLogsPerInterval` is kept for `log`, `warn`, `error` and `fatal`, so a burst of detail can't
* crowd out the logs that matter (posthog-js itself drops whatever exceeds the window's cap).
*/
const LOW_PRIORITY_LIMIT = Math.floor(LOG_CONFIG.maxLogsPerInterval * LOG_CONFIG.lowPriorityShare);
/** Maps a remote level to the PostHog severity it is sent as. */
const SEVERITY_BY_LEVEL: Record<RemoteLogLevel, LogSeverityLevel> = {
trace: 'trace',
debug: 'debug',
log: 'info',
warn: 'warn',
error: 'error',
fatal: 'fatal',
};
/**
* Builds a level set from a stored or configured value, ignoring unknown entries.
*
* @param value - A possibly-array value, such as `userSettings.remoteLogLevels`
* @returns The valid levels, or `undefined` when the value isn't an array
* @example
* ```typescript
* parseLevels(['warn', 'bogus']); // Set { 'warn' }
* parseLevels('warn'); // undefined
* ```
* @source
*/
function parseLevels(value: unknown): ReadonlySet<RemoteLogLevel> | undefined {
if (!Array.isArray(value)) {
return undefined;
}
return new Set(value.filter(isRemoteLogLevel));
}
/** Levels sent when the user hasn't chosen any, from `config.json` `defaultSettings`. */
const DEFAULT_LEVELS: ReadonlySet<RemoteLogLevel> =
parseLevels(defaultSettings.remoteLogLevels) ?? new Set();
/** The posthog-js instance once loaded. */
let client: PostHog | undefined;
/** Whether records may currently be handed to PostHog; `beforeSend` drops them otherwise. */
let active = false;
/** The levels currently being forwarded. */
let enabledLevels: ReadonlySet<RemoteLogLevel> = DEFAULT_LEVELS;
/** Records emitted before posthog-js finished loading, sent once it has. */
let pending: CaptureLogOptions[] = [];
/** Identity attached to every record at send time. */
let identity: { distinctId: string; sessionId: string } | undefined;
/** Start (ms since epoch) of the current rate window. */
let windowStart = 0;
/** Logs let through in the current rate window. */
let windowCount = 0;
/** Low-priority logs dropped since the last log that was sent, reported on that next log. */
let shedCount = 0;
/** Guards `applySettings` against overlapping runs. */
let applying = false;
/** Set when settings changed while `applySettings` was already running. */
let reapply = false;
/** Whether the storage-change listener has been attached. */
let watching = false;
/**
* Trims a string to a maximum length, marking the cut.
*
* @param text - The text to trim
* @param max - The maximum number of characters to keep
* @returns The text, or its first `max` characters followed by an ellipsis
* @example
* ```typescript
* truncate('abcdef', 3); // 'abc…'
* truncate('abc', 3); // 'abc'
* ```
* @source
*/
function truncate(text: string, max: number): string {
return text.length > max ? `${text.slice(0, max)}…` : text;
}
/**
* Renders one logger argument as a bounded string with personal data scrubbed.
*
* @param arg - Any value passed to a logger call
* @returns A string of at most `maxArgChars` characters
* @example
* ```typescript
* stringifyArg({ a: 1 }); // '{"a":1}'
* stringifyArg(undefined); // 'undefined'
* ```
* @source
*/
function stringifyArg(arg: unknown): string {
const max = LOG_CONFIG.maxArgChars;
if (typeof arg === 'string') {
return truncate(scrubText(arg), max);
}
if (typeof arg === 'function') {
return '[function]';
}
if (typeof arg === 'symbol') {
return truncate(scrubText(arg.toString()), max);
}
if (typeof arg !== 'object' || arg === null) {
return truncate(String(arg), max);
}
// scrubValue yields a bounded, cycle-free copy, so stringifying it can't throw.
return truncate(JSON.stringify(scrubValue(arg)) ?? Object.prototype.toString.call(arg), max);
}
/**
* Separates an `Error` from a logger argument. A bare `Error` is taken whole; for a plain
* object, the first top-level property holding an `Error` is taken (the `{ error }` style
* every warn/error call uses) and the rest of the object is returned.
*
* @param arg - One logger argument
* @returns The `Error` found (if any) and whatever is left of the argument to log as-is
* @example
* ```typescript
* splitError(new Error('boom')); // { error: Error('boom'), rest: undefined }
* splitError({ error: new Error('x'), id: 7 }); // { error: Error('x'), rest: { id: 7 } }
* splitError({ id: 7 }); // { rest: { id: 7 } }
* ```
* @source
*/
function splitError(arg: unknown): { error?: Error; rest: unknown } {
if (arg instanceof Error) {
return { error: arg, rest: undefined };
}
const plain =
typeof arg === 'object' &&
arg !== null &&
(Object.getPrototypeOf(arg) === Object.prototype || Object.getPrototypeOf(arg) === null);
if (!plain) {
return { rest: arg };
}
const entries = Object.entries(arg);
const found = entries.find(([, value]) => value instanceof Error);
if (!found || !(found[1] instanceof Error)) {
return { rest: arg };
}
const rest = Object.fromEntries(entries.filter(([key]) => key !== found[0]));
return { error: found[1], rest: Object.keys(rest).length > 0 ? rest : undefined };
}
/**
* Flattens a logger call's extra arguments into bounded, scrubbed log attributes. The first
* `Error`, whether passed bare or as a property of an object argument (`{ error }`), becomes
* `error_name`/`error_message`/`error_stack`; everything else becomes `arg_0`, `arg_1`, ...
*
* @param args - The extra arguments passed to the logger
* @returns Attributes that are all strings (plus an omitted-count when args were dropped)
* @example
* ```typescript
* describeArgs([new Error('boom'), { id: 7 }]);
* // { error_name: 'Error', error_message: 'boom', error_stack: '...', arg_0: '{"id":7}' }
* describeArgs([{ error: new Error('boom'), id: 7 }]);
* // { error_name: 'Error', error_message: 'boom', error_stack: '...', arg_0: '{"id":7}' }
* ```
* @source
*/
function describeArgs(args: readonly unknown[]): Record<string, string | number> {
const attributes: Record<string, string | number> = {};
let index = 0;
for (const original of args.slice(0, LOG_CONFIG.maxArgs)) {
let arg = original;
if (attributes.error_name === undefined) {
const { error, rest } = splitError(original);
if (error) {
attributes.error_name = error.name;
attributes.error_message = truncate(scrubText(error.message), LOG_CONFIG.maxArgChars);
if (error.stack) {
attributes.error_stack = truncate(scrubText(error.stack), LOG_CONFIG.maxStackChars);
}
if (rest === undefined) {
continue;
}
arg = rest;
}
}
attributes[`arg_${index}`] = stringifyArg(arg);
index += 1;
}
if (args.length > LOG_CONFIG.maxArgs) {
attributes.args_omitted = args.length - LOG_CONFIG.maxArgs;
}
return attributes;
}
/**
* Turns a log's context (e.g. the active search query) into bounded, scrubbed attributes.
*
* @param context - The context the logger carried when the call was made, if any
* @returns One attribute per context entry; empty when there is no context
* @example
* ```typescript
* contextAttributes({ search_query: 'acetone' }); // { search_query: 'acetone' }
* contextAttributes(undefined); // {}
* ```
* @source
*/
function contextAttributes(context?: Readonly<Record<string, string>>): Record<string, string> {
return Object.fromEntries(
Object.entries(context ?? {}).map(([key, value]) => [
key,
truncate(scrubText(value), LOG_CONFIG.maxArgChars),
]),
);
}
/** Matches a build-injected call location: `path:line` with an optional `#function`. */
const LOCATION_PATTERN = /^(.+?):(\d+)(?:#(.+))?$/;
/**
* Converts a call location recorded at build time into OpenTelemetry source attributes.
*
* @param location - `path:line#function` as injected by `tools/logCallSites.js`, if any
* @returns `code.filepath`, `code.lineno` and (when known) `code.function`; empty when the
* call wasn't instrumented or the value is malformed
* @example
* ```typescript
* locationAttributes('suppliers/SupplierBase.ts:1431#SupplierBase.fetch');
* // { 'code.filepath': 'suppliers/SupplierBase.ts', 'code.lineno': 1431,
* // 'code.function': 'SupplierBase.fetch' }
* locationAttributes(undefined); // {}
* ```
* @source
*/
function locationAttributes(location?: string): Record<string, string | number> {
const match = location === undefined ? null : LOCATION_PATTERN.exec(location);
if (!match) {
return {};
}
const [, filepath, line, fn] = match;
return {
'code.filepath': filepath,
'code.lineno': Number(line),
...(fn ? { 'code.function': fn } : {}),
};
}
/**
* Names the extension page the log came from.
*
* @returns `'options'` on the options page, otherwise `'app'`
* @example
* ```typescript
* pageContext(); // 'app'
* ```
* @source
*/
function pageContext(): string {
return typeof location !== 'undefined' && location.pathname.includes('options')
? 'options'
: 'app';
}
/**
* Converts a logger call into a bounded PostHog log record.
*
* @param record - The log call handed over by `Logger`
* @returns A record with a truncated body and sanitized attributes
* @example
* ```typescript
* toCaptureLog({ level: 'warn', prefix: 'Cache', message: 'Miss', args: [] });
* // { body: 'Miss', level: 'warn', attributes: { logger: 'Cache', context: 'app', ... } }
* ```
* @source
*/
function toCaptureLog(record: RemoteLogRecord): CaptureLogOptions {
const traceId = record.context?.trace_id;
return {
// The OpenTelemetry trace id lets PostHog group a search's logs; it is also kept as an
// attribute (below) so it's visible and filterable in the log details.
...(isTraceId(traceId) ? { trace_id: traceId } : {}),
body: truncate(scrubText(record.message), LOG_CONFIG.maxMessageChars),
level: SEVERITY_BY_LEVEL[record.level],
attributes: {
logger: record.prefix,
context: pageContext(),
app_version: __APP_VERSION__,
...contextAttributes(record.context),
...locationAttributes(record.location),
...describeArgs(record.args),
},
};
}
/**
* The sink registered with `Logger`: buffers records until posthog-js has loaded, then
* passes them straight through.
*/
const sink: RemoteLogSink = {
isEnabled: (level) => enabledLevels.has(level),
emit: (record) => {
const log = toCaptureLog(record);
if (client) {
client.captureLog(log);
return;
}
if (pending.length < LOG_CONFIG.preInitBufferSize) {
pending.push(log);
}
},
};
/**
* Reads the log levels the user chose, falling back to the configured defaults.
*
* @returns The levels to forward
* @example
* ```typescript
* await readConfiguredLevels(); // Set { 'log', 'warn', 'error' }
* ```
* @source
*/
async function readConfiguredLevels(): Promise<ReadonlySet<RemoteLogLevel>> {
try {
const stored = await cstorage.local.get(CACHE.USER_SETTINGS);
const settings = stored[CACHE.USER_SETTINGS];
if (settings && typeof settings === 'object') {
return parseLevels(Reflect.get(settings, 'remoteLogLevels')) ?? DEFAULT_LEVELS;
}
} catch {
return DEFAULT_LEVELS;
}
return DEFAULT_LEVELS;
}
/**
* Pre-send filter: drops everything while sending is switched off, sheds `trace` and `debug`
* when a rate window is nearly full (reporting how many on the next log as `logs_shed_before`),
* and stamps the shared analytics identity on what remains. Runs before PostHog adds its own
* context.
*
* @param log - The record about to be queued
* @returns The record with identity attributes, or `null` to drop it
* @example
* ```typescript
* beforeSend({ body: 'x', attributes: {} }); // { body: 'x', attributes: { distinct_id: 'fp_...', ... } }
* ```
* @source
*/
function beforeSend(log: CaptureLogOptions): CaptureLogOptions | null {
if (!active || !identity) {
return null;
}
const now = Date.now();
if (now - windowStart >= LOG_CONFIG.flushIntervalMs) {
windowStart = now;
windowCount = 0;
}
// Once the window is mostly full, shed the lowest-priority logs so the rest are never dropped.
if ((log.level === 'trace' || log.level === 'debug') && windowCount >= LOW_PRIORITY_LIMIT) {
shedCount += 1;
return null;
}
windowCount += 1;
const shed = shedCount;
shedCount = 0;
return {
...log,
attributes: {
...log.attributes,
distinct_id: identity.distinctId,
session_id: identity.sessionId,
...(shed > 0 ? { logs_shed_before: shed } : {}),
},
};
}
/**
* Loads posthog-js and initializes it for logs only, or resumes it after an opt-out.
* Flushes anything buffered while it was loading.
*
* @returns A promise that resolves once logging is live
* @example
* ```typescript
* await startClient();
* ```
* @source
*/
async function startClient(): Promise<void> {
active = true;
if (client) {
client.opt_in_capturing({ captureEventName: false });
return;
}
identity = { distinctId: getFingerprintDistinctId(), sessionId: await getSessionId() };
const { default: posthog } = await import('posthog-js/full/no-external');
posthog.init(analyticsConfig.apiKey, {
api_host: analyticsConfig.host,
autocapture: false,
capture_pageview: false,
capture_pageleave: false,
capture_dead_clicks: false,
capture_exceptions: false,
disable_session_recording: true,
disable_surveys: true,
advanced_disable_flags: true,
persistence: 'memory',
bootstrap: { distinctID: identity.distinctId, sessionID: identity.sessionId },
logs: {
serviceName: LOG_CONFIG.serviceName,
environment: import.meta.env.MODE,
serviceVersion: __APP_VERSION__,
flushIntervalMs: LOG_CONFIG.flushIntervalMs,
maxBufferSize: LOG_CONFIG.maxBufferSize,
maxLogsPerInterval: LOG_CONFIG.maxLogsPerInterval,
beforeSend,
},
});
client = posthog;
for (const log of pending) {
posthog.captureLog(log);
}
pending = [];
}
/**
* Stops sending immediately: detaches the sink, drops buffered and queued records, and
* opts posthog-js out.
*
* @example
* ```typescript
* stopSending();
* ```
* @source
*/
function stopSending(): void {
active = false;
windowStart = 0;
windowCount = 0;
shedCount = 0;
Logger.setRemoteSink(undefined);
pending = [];
if (client) {
client.reset();
client.opt_out_capturing();
}
}
/**
* Re-evaluates whether and what to send from the current opt-out and level settings.
* Runs are serialized; a change that lands mid-run triggers one more pass.
*
* @returns A promise that resolves once settings have been applied
* @example
* ```typescript
* await applySettings();
* ```
* @source
*/
async function applySettings(): Promise<void> {
if (applying) {
reapply = true;
return;
}
applying = true;
try {
do {
reapply = false;
enabledLevels = await readConfiguredLevels();
if (await canSendAnalytics()) {
Logger.setRemoteSink(sink);
await startClient();
} else {
stopSending();
}
} while (reapply);
} finally {
applying = false;
}
}
/**
* Starts forwarding `Logger` output to PostHog Logs on the current extension page. Safe to
* call more than once. Does nothing outside a page with a DOM (the service worker), in
* tests, or in the e2e build; stops again if the user opts out of usage sharing.
*
* @category Helpers
* @group Bug reporting
* @returns A promise that resolves once the initial settings have been applied
* @example
* ```typescript
* void initRemoteLogs();
* new Logger('Cache').warn('Miss'); // also sent to PostHog Logs when enabled
* ```
* @source
*/
export async function initRemoteLogs(): Promise<void> {
if (typeof window === 'undefined' || typeof document === 'undefined') {
return;
}
if (import.meta.env.MODE === 'test' || __IS_E2E_BUILD__) {
return;
}
// Register right away so startup logs are buffered; applySettings detaches it if sending is off.
Logger.setRemoteSink(sink);
if (!watching) {
watching = true;
cstorage.onChanged.addListener((changes) => {
if (CACHE.USER_SETTINGS in changes) {
void applySettings();
}
});
}
await applySettings();
}
|