Skip to content

Commit b81c5ea

Browse files
committed
fix(desktop): drain OTLP logs before launching the AppImage update
1 parent b0fc52b commit b81c5ea

2 files changed

Lines changed: 88 additions & 11 deletions

File tree

‎apps/desktop/src/app/DesktopObservability.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const DESKTOP_LOG_FILE_MAX_BYTES = 10 * 1024 * 1024;
3535
const DESKTOP_LOG_FILE_MAX_FILES = 10;
3636
const DESKTOP_BACKEND_CHILD_LOG_FIBER_ID = "#backend-child";
3737
const DESKTOP_TRACE_BATCH_WINDOW_MS = 1_000;
38+
const DESKTOP_OTLP_LOG_DRAIN_TIMEOUT_MS = 2_000;
3839
const DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_BYTES = 1024 * 1024;
3940
const DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_CHUNKS = 256;
4041

@@ -707,10 +708,24 @@ const layerTelemetry = Layer.unwrap(
707708
sink,
708709
...(delegate ? { delegate } : {}),
709710
});
711+
// With a logs endpoint, log records leave only through the OTLP
712+
// exporter, which sends on its interval or when the scope closes.
713+
// Drain it after the trace file, and bound the wait so a collector
714+
// that does not answer cannot hold up an update.
715+
const otlpFlusher = yield* OtlpExporter.Flusher;
716+
const flush =
717+
endpoints.logs === undefined
718+
? sink.flush
719+
: sink.flush.pipe(
720+
Effect.andThen(
721+
otlpFlusher.flush.pipe(Effect.timeoutOption(DESKTOP_OTLP_LOG_DRAIN_TIMEOUT_MS)),
722+
),
723+
Effect.asVoid,
724+
);
710725

711726
return Layer.mergeAll(
712727
Layer.succeed(Tracer.Tracer, tracer),
713-
Layer.succeed(DesktopTraceFlush, { flush: sink.flush }),
728+
Layer.succeed(DesktopTraceFlush, { flush }),
714729
);
715730
}),
716731
).pipe(Layer.provide(OtlpExporter.layerFlusher));

‎apps/desktop/src/electron/ElectronUpdater.test.ts‎

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as Layer from "effect/Layer";
1414
import * as Schema from "effect/Schema";
1515
import * as HttpClient from "effect/http/HttpClient";
1616
import * as HttpClientResponse from "effect/http/HttpClientResponse";
17+
import * as TestClock from "effect/testing/TestClock";
1718
import { AppImageUpdater } from "electron-updater";
1819
import * as DesktopConfig from "../app/DesktopConfig.ts";
1920
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
@@ -130,7 +131,8 @@ function realUpdaterFixture() {
130131
}
131132

132133
// Passing `logExports` turns on OTLP logs and keeps each exported body there.
133-
function traceLayer(root: string, logExports?: Array<string>) {
134+
// The collector takes every export, and never replies while `answering` is false.
135+
function traceLayer(root: string, logExports?: Array<string>, answering = () => true) {
134136
const otlpLogs = logExports !== undefined;
135137
const environment = DesktopEnvironment.layer({
136138
dirname: "/repo/apps/desktop/dist-electron",
@@ -161,11 +163,15 @@ function traceLayer(root: string, logExports?: Array<string>) {
161163
? Layer.succeed(
162164
HttpClient.HttpClient,
163165
HttpClient.make((request) =>
164-
Effect.sync(() => {
166+
Effect.suspend(() => {
165167
if (request.body._tag === "Uint8Array") {
166168
logExports.push(new TextDecoder().decode(request.body.body));
167169
}
168-
return HttpClientResponse.fromWeb(request, new Response(null, { status: 200 }));
170+
return answering()
171+
? Effect.succeed(
172+
HttpClientResponse.fromWeb(request, new Response(null, { status: 200 })),
173+
)
174+
: Effect.never;
169175
}),
170176
),
171177
)
@@ -249,13 +255,19 @@ describe("ElectronUpdater", () => {
249255

250256
// Runs the real install with the explicit-quit flags, then the adapter quit.
251257
// The dependency's intervening Electron event needs a packaged app.
252-
// Returns the trace file as it was when the original quit ran.
253-
const traceAtQuit = (input: ReturnType<typeof realUpdaterFixture>) =>
258+
// Returns the trace file as it was when the original quit ran, and the
259+
// OTLP log exports as they were when the update launched. Production
260+
// closes no scope before either, so neither does this.
261+
const traceAtQuit = (
262+
input: ReturnType<typeof realUpdaterFixture>,
263+
logExports: ReadonlyArray<string> = [],
264+
) =>
254265
Effect.gen(function* () {
255266
const updater = yield* ElectronUpdater.ElectronUpdater;
256267
const environment = yield* DesktopEnvironment.DesktopEnvironment;
257268
const originalQuit = input.adapter.quit;
258269
let trace = "";
270+
let exported: ReadonlyArray<string> = [];
259271
let finish = () => {};
260272
const quit = new Promise<void>((resolve) => {
261273
finish = resolve;
@@ -265,13 +277,16 @@ describe("ElectronUpdater", () => {
265277
trace = NodeFS.existsSync(tracePath) ? NodeFS.readFileSync(tracePath, "utf8") : "";
266278
finish();
267279
});
268-
vi.spyOn(input.updater, "spawnLog").mockResolvedValue(true);
280+
vi.spyOn(input.updater, "spawnLog").mockImplementation(() => {
281+
exported = [...logExports];
282+
return Promise.resolve(true);
283+
});
269284
yield* updater.setAutoDownload(false);
270285
assert.isTrue(input.updater.install(true, true));
271286
input.adapter.quit();
272287
yield* Effect.promise(() => quit);
273288
assert.equal(originalQuit.mock.calls.length, 1);
274-
return trace;
289+
return { trace, exported };
275290
});
276291

277292
it.effect.each([false, true])(
@@ -280,8 +295,7 @@ describe("ElectronUpdater", () => {
280295
const input = realUpdaterFixture();
281296
const logExports: Array<string> = [];
282297
return Effect.gen(function* () {
283-
// Closing the layer scope drains the OTLP log exporter.
284-
const persisted = yield* traceAtQuit(input).pipe(
298+
const { trace: persisted, exported } = yield* traceAtQuit(input, logExports).pipe(
285299
Effect.provide(traceLayer(input.root, otlpLogs ? logExports : undefined)),
286300
);
287301
const spans = updaterSpans(persisted);
@@ -306,7 +320,7 @@ describe("ElectronUpdater", () => {
306320
);
307321
assert.equal(persisted.includes(record), !otlpLogs);
308322
assert.equal(
309-
logExports.some((body) => body.includes(record)),
323+
exported.some((body) => body.includes(record)),
310324
otlpLogs,
311325
);
312326
}
@@ -430,6 +444,54 @@ describe("ElectronUpdater", () => {
430444
),
431445
);
432446
});
447+
448+
it.effect("waits a bounded time for a log collector that never answers", () => {
449+
const input = realUpdaterFixture();
450+
const logExports: Array<string> = [];
451+
const steps: Array<string> = [];
452+
let answering = false;
453+
vi.spyOn(input.updater, "spawnLog").mockImplementation(() => {
454+
steps.push("launch");
455+
return Promise.resolve(true);
456+
});
457+
input.adapter.quit.mockImplementation(() => {
458+
steps.push("quit");
459+
});
460+
return Effect.gen(function* () {
461+
const updater = yield* ElectronUpdater.ElectronUpdater;
462+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
463+
const tracePath = NodePath.join(environment.logDir, "desktop.trace.ndjson");
464+
yield* updater.setAutoDownload(false);
465+
assert.isTrue(input.updater.install(true, true));
466+
input.adapter.quit();
467+
// The test clock holds the bound, so the file write and the export
468+
// are awaited in real time.
469+
yield* Effect.promise(() =>
470+
vi.waitFor(
471+
() => {
472+
assert.include(NodeFS.readFileSync(tracePath, "utf8"), "desktop.updater");
473+
assert.lengthOf(logExports, 1);
474+
},
475+
{ timeout: 5_000 },
476+
),
477+
);
478+
yield* Effect.promise(() => new Promise((resolve) => setImmediate(resolve)));
479+
assert.include(logExports[0], "AppImage update committed");
480+
assert.deepEqual(steps, []);
481+
yield* TestClock.adjust("2 seconds");
482+
yield* Effect.promise(() =>
483+
vi.waitFor(() => assert.deepEqual(steps, ["launch", "quit"]), { timeout: 5_000 }),
484+
);
485+
}).pipe(
486+
// The scope's own final export must not wait on the test clock.
487+
Effect.ensuring(
488+
Effect.sync(() => {
489+
answering = true;
490+
}),
491+
),
492+
Effect.provide(traceLayer(input.root, logExports, () => answering)),
493+
);
494+
});
433495
});
434496

435497
it.effect("scopes updater event listeners", () =>

0 commit comments

Comments
 (0)