Open History to see the last commit
1import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as ConfigProvider from "effect/ConfigProvider";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Schema from "effect/Schema";
9import * as HttpClient from "effect/http/HttpClient";
10import * as HttpClientResponse from "effect/http/HttpClientResponse";
11
12import * as DesktopConfig from "./DesktopConfig.ts";
13import * as DesktopEnvironment from "./DesktopEnvironment.ts";
14import * as DesktopObservability from "./DesktopObservability.ts";
15
16const DesktopBackendChildLogRecord = Schema.Struct({
17 message: Schema.String,
18 level: Schema.Literals(["INFO", "ERROR"]),
19 timestamp: Schema.String,
20 annotations: Schema.Record(Schema.String, Schema.Unknown),
21 spans: Schema.Record(Schema.String, Schema.Unknown),
22 fiberId: Schema.String,
23});
24
25const decodeDesktopBackendChildLogRecord = Schema.decodeEffect(
26 Schema.fromJsonString(DesktopBackendChildLogRecord),
27);
28
29const TraceRecordLine = Schema.Struct({
30 name: Schema.String,
31 attributes: Schema.Record(Schema.String, Schema.Unknown),
32 events: Schema.Array(
33 Schema.Struct({
34 name: Schema.String,
35 attributes: Schema.Record(Schema.String, Schema.Unknown),
36 }),
37 ),
38});
39
40const decodeTraceRecordLine = Schema.decodeUnknownSync(Schema.fromJsonString(TraceRecordLine));
41
42const environmentInput = (baseDir: string) =>
43 ({
44 dirname: "/repo/apps/desktop/dist-electron",
45 homeDirectory: baseDir,
46 platform: "darwin",
47 processArch: "arm64",
48 appVersion: "1.2.3",
49 appPath: "/repo",
50 isPackaged: false,
51 resourcesPath: "/repo/resources",
52 runningUnderArm64Translation: false,
53 }) satisfies DesktopEnvironment.MakeDesktopEnvironmentInput;
54
55const layerEnvironmentFor = (
56 baseDir: string,
57 isDevelopment = true,
58 env: Readonly<Record<string, string | undefined>> = {},
59) =>
60 DesktopEnvironment.layer(environmentInput(baseDir)).pipe(
61 Layer.provide(
62 Layer.mergeAll(
63 NodeServices.layer,
64 DesktopConfig.layerTest({
65 T3CODE_HOME: baseDir,
66 VITE_DEV_SERVER_URL: isDevelopment ? "http://127.0.0.1:5733" : undefined,
67 ...env,
68 }),
69 ),
70 ),
71 );
72
73interface ExportedRequest {
74 readonly url: string;
75 readonly headers: Readonly<Record<string, string>>;
76 readonly body: string;
77}
78
79const layerCollector = (requests: Array<ExportedRequest>) =>
80 Layer.succeed(
81 HttpClient.HttpClient,
82 HttpClient.make((request) =>
83 Effect.sync(() => {
84 requests.push({
85 url: request.url,
86 headers: request.headers,
87 body:
88 request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : "",
89 });
90 return HttpClientResponse.fromWeb(request, new Response(null, { status: 200 }));
91 }),
92 ),
93 );
94
95// A developer's own OTEL_* variables would otherwise pick the endpoints.
96const layerEmptyEnv = ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }));
97
98const encodeObservabilitySettingsFile = Schema.encodeSync(
99 Schema.fromJsonString(
100 Schema.Struct({ observability: Schema.Record(Schema.String, Schema.String) }),
101 ),
102);
103
104const writeObservabilitySettings = Effect.fn(function* (
105 environmentLayer: ReturnType<typeof layerEnvironmentFor>,
106 observability: Readonly<Record<string, string>>,
107) {
108 const fileSystem = yield* FileSystem.FileSystem;
109 const { path, serverSettingsPath } = yield* Effect.gen(function* () {
110 const environment = yield* DesktopEnvironment.DesktopEnvironment;
111 return environment;
112 }).pipe(Effect.provide(environmentLayer));
113 yield* fileSystem.makeDirectory(path.dirname(serverSettingsPath), { recursive: true });
114 yield* fileSystem.writeFileString(
115 serverSettingsPath,
116 encodeObservabilitySettingsFile({ observability }),
117 );
118});
119
120describe("DesktopObservability", () => {
121 it("advances a retained output offset instead of repeatedly copying a full head chunk", () => {
122 const maxBufferedBytes = 1024 * 1024;
123 const initial = DesktopObservability.appendBoundedOutputChunk(
124 {
125 runId: "test-run",
126 startDetails: "pid=123",
127 chunks: [],
128 byteLength: 0,
129 },
130 "stderr",
131 new Uint8Array(maxBufferedBytes),
132 );
133 const initialBackingBuffer = initial.chunks[0]?.chunk.buffer;
134
135 const next = DesktopObservability.appendBoundedOutputChunk(initial, "stderr", Uint8Array.of(1));
136
137 assert.equal(next.chunks[0]?.chunk.buffer, initialBackingBuffer);
138 assert.equal(next.chunks[0]?.offset, 1);
139 assert.equal(next.byteLength, maxBufferedBytes);
140 });
141
142 it.effect("persists desktop Effect logs as span events in desktop.trace.ndjson", () =>
143 Effect.gen(function* () {
144 const fileSystem = yield* FileSystem.FileSystem;
145 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
146 prefix: "t3-desktop-observability-test-",
147 });
148 const layerEnvironment = layerEnvironmentFor(baseDir);
149 const tracePath = yield* Effect.gen(function* () {
150 const environment = yield* DesktopEnvironment.DesktopEnvironment;
151 return environment.path.join(environment.logDir, "desktop.trace.ndjson");
152 }).pipe(Effect.provide(layerEnvironment));
153 const logPath = yield* Effect.gen(function* () {
154 const environment = yield* DesktopEnvironment.DesktopEnvironment;
155 return environment.path.join(environment.logDir, "desktop-main.log");
156 }).pipe(Effect.provide(layerEnvironment));
157
158 yield* Effect.scoped(
159 Effect.gen(function* () {
160 yield* Effect.annotateCurrentSpan({ "desktop.test": true });
161 yield* Effect.logInfo("desktop trace event");
162 }).pipe(
163 Effect.withSpan("desktop-observability-test"),
164 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
165 ),
166 );
167
168 const records = (yield* fileSystem.readFileString(tracePath))
169 .trim()
170 .split("\n")
171 .filter((line) => line.length > 0)
172 .map((line) => decodeTraceRecordLine(line));
173 const record = records.find((entry) => entry.name === "desktop-observability-test");
174
175 assert.notEqual(record, undefined);
176 if (!record) {
177 return;
178 }
179 assert.equal(record.attributes["desktop.test"], true);
180 assert.equal(
181 record.events.some((event) => event.name === "desktop trace event"),
182 true,
183 );
184 assert.isFalse(yield* fileSystem.exists(logPath));
185 }).pipe(
186 Effect.scoped,
187 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeHttpClient.layerUndici, layerEmptyEnv)),
188 ),
189 );
190
191 it.effect("buffers backend child output and persists it only when a failure is reported", () =>
192 Effect.gen(function* () {
193 const fileSystem = yield* FileSystem.FileSystem;
194 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
195 prefix: "t3-desktop-backend-output-log-test-",
196 });
197 const layerEnvironment = layerEnvironmentFor(baseDir, false);
198 const logPath = yield* Effect.gen(function* () {
199 const environment = yield* DesktopEnvironment.DesktopEnvironment;
200 return environment.path.join(environment.logDir, "server-child.log");
201 }).pipe(Effect.provide(layerEnvironment));
202 const tracePath = yield* Effect.gen(function* () {
203 const environment = yield* DesktopEnvironment.DesktopEnvironment;
204 return environment.path.join(environment.logDir, "desktop.trace.ndjson");
205 }).pipe(Effect.provide(layerEnvironment));
206
207 yield* Effect.gen(function* () {
208 const factory = yield* DesktopObservability.DesktopBackendOutputLogFactory;
209 const outputLog = yield* factory.forInstance("primary");
210 yield* outputLog.beginSession({
211 details: "pid=123 port=3773 cwd=/repo",
212 });
213 yield* outputLog.writeOutputChunk("stdout", new TextEncoder().encode("hello server\n"));
214 assert.isFalse(yield* fileSystem.exists(logPath));
215 yield* outputLog.persistFailure({ details: "code=1" });
216 yield* outputLog.beginSession({ details: "pid=456" });
217 yield* outputLog.writeOutputChunk("stderr", new TextEncoder().encode("normal shutdown\n"));
218 yield* outputLog.discardSession;
219 }).pipe(
220 Effect.annotateLogs({ runId: "test-run" }),
221 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
222 );
223
224 const log = yield* fileSystem.readFileString(logPath);
225 const lines = log.trimEnd().split("\n");
226 const start = yield* decodeDesktopBackendChildLogRecord(lines[0] ?? "");
227 const output = yield* decodeDesktopBackendChildLogRecord(lines[1] ?? "");
228 const end = yield* decodeDesktopBackendChildLogRecord(lines[2] ?? "");
229
230 assert.equal(lines.length, 3);
231 assert.equal(start.message, "backend child process failure output start");
232 assert.equal(start.level, "ERROR");
233 assert.equal(start.annotations.component, "desktop-backend-child");
234 assert.equal(start.annotations.runId, "test-run");
235 assert.equal(start.annotations.instanceId, "primary");
236 assert.equal(start.annotations.phase, "START");
237 assert.equal(start.annotations.details, "pid=123 port=3773 cwd=/repo");
238
239 assert.equal(output.message, "backend child process output");
240 assert.equal(output.level, "INFO");
241 assert.equal(output.annotations.component, "desktop-backend-child");
242 assert.equal(output.annotations.runId, "test-run");
243 assert.equal(output.annotations.instanceId, "primary");
244 assert.equal(output.annotations.stream, "stdout");
245 assert.equal(output.annotations.text, "hello server\n");
246
247 assert.equal(end.message, "backend child process failure output end");
248 assert.equal(end.level, "ERROR");
249 assert.equal(end.annotations.instanceId, "primary");
250 assert.equal(end.annotations.phase, "END");
251 assert.equal(end.annotations.details, "code=1");
252
253 const traceRecords = (yield* fileSystem.readFileString(tracePath))
254 .trim()
255 .split("\n")
256 .filter((line) => line.length > 0)
257 .map((line) => decodeTraceRecordLine(line));
258 assert.isFalse(
259 traceRecords.some(
260 (record) => record.name === "desktop.observability.backendOutput.writeOutputChunk",
261 ),
262 );
263 }).pipe(
264 Effect.scoped,
265 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeHttpClient.layerUndici, layerEmptyEnv)),
266 ),
267 );
268
269 it.effect("keeps buffering output after a non-terminal failure snapshot", () =>
270 Effect.gen(function* () {
271 const fileSystem = yield* FileSystem.FileSystem;
272 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
273 prefix: "t3-desktop-backend-output-snapshot-test-",
274 });
275 const layerEnvironment = layerEnvironmentFor(baseDir, false);
276 const logPath = yield* Effect.gen(function* () {
277 const environment = yield* DesktopEnvironment.DesktopEnvironment;
278 return environment.path.join(environment.logDir, "server-child.log");
279 }).pipe(Effect.provide(layerEnvironment));
280
281 yield* Effect.gen(function* () {
282 const factory = yield* DesktopObservability.DesktopBackendOutputLogFactory;
283 const outputLog = yield* factory.forInstance("primary");
284 yield* outputLog.beginSession({ details: "pid=123" });
285 yield* outputLog.writeOutputChunk("stdout", new TextEncoder().encode("before timeout\n"));
286 yield* outputLog.persistFailureSnapshot({ details: "readiness timeout" });
287 yield* outputLog.writeOutputChunk("stderr", new TextEncoder().encode("after timeout\n"));
288 yield* outputLog.persistFailure({ details: "code=1" });
289 }).pipe(
290 Effect.annotateLogs({ runId: "test-run" }),
291 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
292 );
293
294 const records = yield* Effect.forEach(
295 (yield* fileSystem.readFileString(logPath)).trimEnd().split("\n"),
296 (line) => decodeDesktopBackendChildLogRecord(line),
297 );
298 assert.equal(
299 records.some((record) => record.annotations.text === "after timeout\n"),
300 true,
301 );
302 assert.equal(records.at(-1)?.annotations.details, "code=1");
303 }).pipe(
304 Effect.scoped,
305 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeHttpClient.layerUndici, layerEmptyEnv)),
306 ),
307 );
308
309 it.effect("retains only the last mebibyte of backend child output", () =>
310 Effect.gen(function* () {
311 const fileSystem = yield* FileSystem.FileSystem;
312 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
313 prefix: "t3-desktop-backend-output-bound-test-",
314 });
315 const layerEnvironment = layerEnvironmentFor(baseDir, false);
316 const logPath = yield* Effect.gen(function* () {
317 const environment = yield* DesktopEnvironment.DesktopEnvironment;
318 return environment.path.join(environment.logDir, "server-child.log");
319 }).pipe(Effect.provide(layerEnvironment));
320 const maxBufferedBytes = 1024 * 1024;
321 const discardedPrefixBytes = 128;
322 const output = new Uint8Array(maxBufferedBytes + discardedPrefixBytes);
323 output.fill("x".charCodeAt(0));
324 output.fill("y".charCodeAt(0), 0, discardedPrefixBytes);
325
326 yield* Effect.scoped(
327 Effect.gen(function* () {
328 const factory = yield* DesktopObservability.DesktopBackendOutputLogFactory;
329 const outputLog = yield* factory.forInstance("primary");
330 yield* outputLog.beginSession({ details: "pid=123" });
331 yield* outputLog.writeOutputChunk("stderr", output);
332 yield* outputLog.persistFailure({ details: "code=1" });
333 }).pipe(
334 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
335 ),
336 );
337
338 const lines = (yield* fileSystem.readFileString(logPath)).trimEnd().split("\n");
339 const record = yield* decodeDesktopBackendChildLogRecord(lines[1] ?? "");
340 const text = record.annotations.text;
341 assert.equal(typeof text, "string");
342 if (typeof text !== "string") {
343 return;
344 }
345 assert.equal(new TextEncoder().encode(text).byteLength, maxBufferedBytes);
346 assert.isFalse(text.includes("y"));
347 }).pipe(
348 Effect.scoped,
349 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeHttpClient.layerUndici, layerEmptyEnv)),
350 ),
351 );
352
353 it.effect("bounds the number of retained backend child output chunks", () =>
354 Effect.gen(function* () {
355 const fileSystem = yield* FileSystem.FileSystem;
356 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
357 prefix: "t3-desktop-backend-output-chunks-test-",
358 });
359 const layerEnvironment = layerEnvironmentFor(baseDir, false);
360 const logPath = yield* Effect.gen(function* () {
361 const environment = yield* DesktopEnvironment.DesktopEnvironment;
362 return environment.path.join(environment.logDir, "server-child.log");
363 }).pipe(Effect.provide(layerEnvironment));
364
365 yield* Effect.scoped(
366 Effect.gen(function* () {
367 const factory = yield* DesktopObservability.DesktopBackendOutputLogFactory;
368 const outputLog = yield* factory.forInstance("primary");
369 yield* outputLog.beginSession({ details: "pid=123" });
370 for (let index = 0; index < 300; index += 1) {
371 yield* outputLog.writeOutputChunk("stderr", Uint8Array.of(index % 128));
372 }
373 yield* outputLog.persistFailure({ details: "code=1" });
374 }).pipe(
375 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
376 ),
377 );
378
379 const lines = (yield* fileSystem.readFileString(logPath)).trimEnd().split("\n");
380 assert.equal(lines.length, 258);
381 }).pipe(
382 Effect.scoped,
383 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeHttpClient.layerUndici, layerEmptyEnv)),
384 ),
385 );
386
387 it.effect("exports main process log records to the configured logs endpoint", () => {
388 const requests: Array<ExportedRequest> = [];
389 return Effect.gen(function* () {
390 const fileSystem = yield* FileSystem.FileSystem;
391 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
392 prefix: "t3-desktop-observability-test-",
393 });
394 const layerEnvironment = layerEnvironmentFor(baseDir, true, {
395 T3CODE_OTLP_LOGS_URL: "https://collector.example.com/v1/logs",
396 T3CODE_OTLP_HEADERS: "x-scope=desktop",
397 });
398 const tracePath = yield* Effect.gen(function* () {
399 const environment = yield* DesktopEnvironment.DesktopEnvironment;
400 return environment.path.join(environment.logDir, "desktop.trace.ndjson");
401 }).pipe(Effect.provide(layerEnvironment));
402
403 yield* Effect.scoped(
404 Effect.logInfo("desktop log export").pipe(
405 Effect.withSpan("desktop-log-export-test"),
406 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
407 ),
408 );
409
410 assert.lengthOf(requests, 1);
411 const [request] = requests;
412 assert.strictEqual(request?.url, "https://collector.example.com/v1/logs");
413 assert.include(request?.body ?? "", "desktop log export");
414 assert.include(request?.body ?? "", "service.runtime");
415 assert.strictEqual(request?.headers["x-scope"], "desktop");
416
417 const record = (yield* fileSystem.readFileString(tracePath))
418 .trim()
419 .split("\n")
420 .filter((line) => line.length > 0)
421 .map((line) => decodeTraceRecordLine(line))
422 .find((entry) => entry.name === "desktop-log-export-test");
423 assert.notEqual(record, undefined);
424 assert.lengthOf(record?.events ?? [], 0);
425 }).pipe(
426 Effect.scoped,
427 Effect.provide(Layer.mergeAll(NodeServices.layer, layerCollector(requests), layerEmptyEnv)),
428 );
429 });
430
431 it.effect("exports to an OTEL endpoint over Settings, with its own headers and protocol", () => {
432 const requests: Array<ExportedRequest> = [];
433 return Effect.gen(function* () {
434 const fileSystem = yield* FileSystem.FileSystem;
435 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
436 prefix: "t3-desktop-observability-test-",
437 });
438 const layerEnvironment = layerEnvironmentFor(baseDir, true, {
439 T3CODE_OTLP_HEADERS: "x-scope=desktop",
440 });
441 yield* writeObservabilitySettings(layerEnvironment, {
442 otlpLogsUrl: "https://settings.example.com/v1/logs",
443 });
444
445 yield* Effect.scoped(
446 Effect.logInfo("desktop otel export").pipe(
447 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
448 ),
449 );
450
451 assert.lengthOf(requests, 1);
452 const [request] = requests;
453 assert.strictEqual(request?.url, "https://collector.example.com/v1/logs");
454 assert.strictEqual(request?.headers["x-otel"], "desktop");
455 assert.strictEqual(request?.headers["x-scope"], undefined);
456 assert.strictEqual(request?.headers["content-type"], "application/json");
457 }).pipe(
458 Effect.scoped,
459 Effect.provide(
460 Layer.mergeAll(
461 NodeServices.layer,
462 layerCollector(requests),
463 ConfigProvider.layer(
464 ConfigProvider.fromEnv({
465 env: {
466 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector.example.com",
467 OTEL_EXPORTER_OTLP_HEADERS: "x-otel=desktop",
468 OTEL_EXPORTER_OTLP_LOGS_PROTOCOL: "http/json",
469 },
470 }),
471 ),
472 ),
473 ),
474 );
475 });
476
477 it.effect("keeps its service name while OTEL resource attributes add dimensions", () => {
478 const requests: Array<ExportedRequest> = [];
479 return Effect.gen(function* () {
480 const fileSystem = yield* FileSystem.FileSystem;
481 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
482 prefix: "t3-desktop-observability-test-",
483 });
484 const layerEnvironment = layerEnvironmentFor(baseDir, true, {
485 T3CODE_OTLP_LOGS_URL: "https://collector.example.com/v1/logs",
486 });
487
488 yield* Effect.scoped(
489 Effect.logInfo("desktop service name").pipe(
490 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
491 ),
492 );
493
494 assert.lengthOf(requests, 1);
495 const body = requests[0]?.body ?? "";
496 assert.include(body, '"stringValue":"t3code-desktop"');
497 assert.include(body, "deployment.environment.name");
498 assert.include(body, '"key":"service.namespace","value":{"stringValue":"t3code"}');
499 assert.notInclude(body, "renamed");
500 }).pipe(
501 Effect.scoped,
502 Effect.provide(
503 Layer.mergeAll(
504 NodeServices.layer,
505 layerCollector(requests),
506 ConfigProvider.layer(
507 ConfigProvider.fromEnv({
508 env: {
509 OTEL_SERVICE_NAME: "renamed",
510 OTEL_RESOURCE_ATTRIBUTES:
511 "service.name=renamed,service.namespace=renamed,deployment.environment.name=development",
512 },
513 }),
514 ),
515 ),
516 ),
517 );
518 });
519
520 it.effect("exports nothing to Settings for logs an unusable OTEL endpoint claimed", () => {
521 const requests: Array<ExportedRequest> = [];
522 return Effect.gen(function* () {
523 const fileSystem = yield* FileSystem.FileSystem;
524 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
525 prefix: "t3-desktop-observability-test-",
526 });
527 const layerEnvironment = layerEnvironmentFor(baseDir, true, {
528 T3CODE_OTLP_HEADERS: "x-scope=desktop",
529 });
530 yield* writeObservabilitySettings(layerEnvironment, {
531 otlpLogsUrl: "https://settings.example.com/v1/logs",
532 });
533
534 yield* Effect.scoped(
535 Effect.logInfo("desktop otel off").pipe(
536 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
537 ),
538 );
539
540 assert.lengthOf(requests, 0);
541 }).pipe(
542 Effect.scoped,
543 Effect.provide(
544 Layer.mergeAll(
545 NodeServices.layer,
546 layerCollector(requests),
547 ConfigProvider.layer(
548 ConfigProvider.fromEnv({
549 env: {
550 OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector.example.com",
551 OTEL_EXPORTER_OTLP_LOGS_PROTOCOL: "grpc",
552 },
553 }),
554 ),
555 ),
556 ),
557 );
558 });
559
560 it.effect("exports kill switch warnings through the configured logger", () => {
561 const requests: Array<ExportedRequest> = [];
562 return Effect.gen(function* () {
563 const fileSystem = yield* FileSystem.FileSystem;
564 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
565 prefix: "t3-desktop-observability-test-",
566 });
567 const layerEnvironment = layerEnvironmentFor(baseDir, true, {
568 T3CODE_OTLP_LOGS_URL: "https://collector.example.com/v1/logs",
569 });
570
571 yield* Effect.scoped(
572 Effect.void.pipe(
573 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
574 ),
575 );
576
577 assert.include(requests[0]?.body ?? "", "OTEL_SDK_DISABLED=1 was read as false");
578 }).pipe(
579 Effect.scoped,
580 Effect.provide(
581 Layer.mergeAll(
582 NodeServices.layer,
583 layerCollector(requests),
584 ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTEL_SDK_DISABLED: "1" } })),
585 ),
586 ),
587 );
588 });
589
590 it.effect("reads every signal endpoint from Settings when the environment names none", () => {
591 const requests: Array<ExportedRequest> = [];
592 return Effect.gen(function* () {
593 const fileSystem = yield* FileSystem.FileSystem;
594 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
595 prefix: "t3-desktop-observability-test-",
596 });
597 const layerEnvironment = layerEnvironmentFor(baseDir);
598 yield* writeObservabilitySettings(layerEnvironment, {
599 otlpTracesUrl: "https://settings.example.com/v1/traces",
600 otlpLogsUrl: "https://settings.example.com/v1/logs",
601 otlpMetricsUrl: "https://settings.example.com/v1/metrics",
602 });
603
604 yield* Effect.scoped(
605 Effect.logInfo("desktop log export from settings").pipe(
606 Effect.withSpan("desktop-settings-export-test"),
607 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
608 ),
609 );
610
611 assert.deepEqual(requests.map((request) => request.url).toSorted(), [
612 "https://settings.example.com/v1/logs",
613 "https://settings.example.com/v1/traces",
614 ]);
615 assert.include(
616 requests.find((request) => request.url.endsWith("/v1/logs"))?.body ?? "",
617 "desktop log export from settings",
618 );
619 assert.include(
620 requests.find((request) => request.url.endsWith("/v1/traces"))?.body ?? "",
621 "desktop-settings-export-test",
622 );
623 }).pipe(
624 Effect.scoped,
625 Effect.provide(Layer.mergeAll(NodeServices.layer, layerCollector(requests), layerEmptyEnv)),
626 );
627 });
628
629 it.effect("stays off the network when no endpoint is configured", () => {
630 const requests: Array<ExportedRequest> = [];
631 return Effect.gen(function* () {
632 const fileSystem = yield* FileSystem.FileSystem;
633 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
634 prefix: "t3-desktop-observability-test-",
635 });
636
637 yield* Effect.scoped(
638 Effect.logInfo("desktop log stays local").pipe(
639 Effect.withSpan("desktop-offline-test"),
640 Effect.provide(
641 DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironmentFor(baseDir))),
642 ),
643 ),
644 );
645
646 assert.lengthOf(requests, 0);
647 }).pipe(
648 Effect.scoped,
649 Effect.provide(Layer.mergeAll(NodeServices.layer, layerCollector(requests), layerEmptyEnv)),
650 );
651 });
652
653 it.effect("stops every export when the OpenTelemetry SDK is disabled", () => {
654 const requests: Array<ExportedRequest> = [];
655 return Effect.gen(function* () {
656 const fileSystem = yield* FileSystem.FileSystem;
657 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
658 prefix: "t3-desktop-observability-test-",
659 });
660 const layerEnvironment = layerEnvironmentFor(baseDir);
661 yield* writeObservabilitySettings(layerEnvironment, {
662 otlpTracesUrl: "https://settings.example.com/v1/traces",
663 otlpLogsUrl: "https://settings.example.com/v1/logs",
664 });
665
666 yield* Effect.scoped(
667 Effect.logInfo("desktop log stays local when disabled").pipe(
668 Effect.withSpan("desktop-disabled-test"),
669 Effect.provide(DesktopObservability.layer.pipe(Layer.provideMerge(layerEnvironment))),
670 ),
671 );
672
673 assert.lengthOf(requests, 0);
674 }).pipe(
675 Effect.scoped,
676 Effect.provide(
677 Layer.mergeAll(
678 NodeServices.layer,
679 layerCollector(requests),
680 ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTEL_SDK_DISABLED: "true" } })),
681 ),
682 ),
683 );
684 });
685});