GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(desktop): the process that starts the server reports what it did… · maria-rcks/t3libre@38c5bc2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps
    • desktop/src/app
      • DesktopConfig.ts
      • DesktopEnvironment.test.ts
      • DesktopEnvironment.ts
      • DesktopObservability.ts
      • DesktopOtlpExport.test.ts
      • DesktopOtlpExport.ts
    • server/src
      • bin.test.ts
      • cli
        • config.test.ts
        • config.ts
        • pair.ts
      • config.ts
      • environment
        • ServerEnvironment.test.ts
      • server.test.ts
      • serverLogger.test.ts
  • docs
    • fork
      • 0022-the-desktop-app-reports-its-own-work.md
      • README.md
    • operations
      • observability.md
  • packages/shared
    • package.json
    • src
      • otelEnvironment.test.ts
      • otelEnvironment.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopConfig.ts
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ export const DesktopConfig = Config.all({
4545
desktopLanHostOverride: trimmedString("T3CODE_DESKTOP_LAN_HOST"),
4646
desktopHttpsEndpointUrls: commaSeparatedStrings("T3CODE_DESKTOP_HTTPS_ENDPOINTS"),
4747
otlpTracesUrl: trimmedString("T3CODE_OTLP_TRACES_URL"),
48-
otlpExportIntervalMs: Config.int("T3CODE_OTLP_EXPORT_INTERVAL_MS").pipe(
49-
Config.withDefault(10_000),
50-
),
48+
otlpMetricsUrl: trimmedString("T3CODE_OTLP_METRICS_URL"),
49+
otlpLogsUrl: trimmedString("T3CODE_OTLP_LOGS_URL"),
50+
// Left as an Option rather than defaulted here: an unset variable is what
51+
// lets each signal fall back to the interval the OpenTelemetry environment
52+
// asked for, which the specification defines per signal.
53+
otlpExportIntervalMs: Config.int("T3CODE_OTLP_EXPORT_INTERVAL_MS").pipe(Config.option),
5154
appImagePath: trimmedString("APPIMAGE"),
5255
disableAutoUpdate: optionalBoolean("T3CODE_DISABLE_AUTO_UPDATE"),
5356
mockUpdates: optionalBoolean("T3CODE_DESKTOP_MOCK_UPDATES"),
Collapse file

‎apps/desktop/src/app/DesktopEnvironment.test.ts‎

Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopEnvironment.test.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe("DesktopEnvironment", () => {
7878
assert.deepEqual(environment.configuredBackendPort, Option.some(4949));
7979
assert.deepEqual(environment.commitHashOverride, Option.some("0123456789abcdef"));
8080
assert.deepEqual(environment.otlpTracesUrl, Option.some("http://127.0.0.1:4318/v1/traces"));
81-
assert.equal(environment.otlpExportIntervalMs, 2500);
81+
assert.deepEqual(environment.otlpExportIntervalMs, Option.some(2500));
8282
}),
8383
);
8484

Collapse file

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

Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopEnvironment.ts
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
DesktopRuntimeArch,
55
DesktopRuntimeInfo,
66
} from "@t3tools/contracts";
7+
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
78
import * as Config from "effect/Config";
89
import * as Context from "effect/Context";
910
import * as Effect from "effect/Effect";
@@ -68,7 +69,16 @@ export class DesktopEnvironment extends Context.Service<
6869
readonly configuredBackendPort: Option.Option<number>;
6970
readonly commitHashOverride: Option.Option<string>;
7071
readonly otlpTracesUrl: Option.Option<string>;
71-
readonly otlpExportIntervalMs: number;
72+
readonly otlpMetricsUrl: Option.Option<string>;
73+
readonly otlpLogsUrl: Option.Option<string>;
74+
readonly otlpExportIntervalMs: Option.Option<number>;
75+
/**
76+
* What the standard `OTEL_*` variables asked for. The `T3CODE_OTLP_*`
77+
* endpoints above still win per signal; this is what the main process
78+
* falls back to, and it carries the headers, wire format, resource, and
79+
* batching that T3 Code has no names of its own for.
80+
*/
81+
readonly otelEnvironment: OtelEnvironment.OtelEnvironment;
7282
readonly branding: DesktopAppBranding;
7383
readonly displayName: string;
7484
readonly appUserModelId: string;
@@ -145,6 +155,7 @@ const make = Effect.fn("desktop.environment.make")(function* (
145155
): Effect.fn.Return<DesktopEnvironment["Service"], Config.ConfigError, Path.Path> {
146156
const path = yield* Path.Path;
147157
const config = yield* DesktopConfig.DesktopConfig;
158+
const otelEnvironment = yield* OtelEnvironment.load;
148159
const homeDirectory = input.homeDirectory;
149160
const devServerUrl = config.devServerUrl;
150161
const isDevelopment = Option.isSome(devServerUrl);
@@ -220,7 +231,10 @@ const make = Effect.fn("desktop.environment.make")(function* (
220231
configuredBackendPort: config.configuredBackendPort,
221232
commitHashOverride: config.commitHashOverride,
222233
otlpTracesUrl: config.otlpTracesUrl,
234+
otlpMetricsUrl: config.otlpMetricsUrl,
235+
otlpLogsUrl: config.otlpLogsUrl,
223236
otlpExportIntervalMs: config.otlpExportIntervalMs,
237+
otelEnvironment,
224238
branding,
225239
displayName,
226240
appUserModelId: Option.getOrElse(config.appUserModelIdOverride, () =>
Collapse file

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

Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopObservability.ts
+154-52Lines changed: 154 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";
22
import { makeLocalFileTracer, makeTraceSink } from "@t3tools/shared/observability";
3-
import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings";
3+
import {
4+
parsePersistedServerObservabilitySettings,
5+
type PersistedServerObservabilitySettings,
6+
} from "@t3tools/shared/serverSettings";
47
import * as Context from "effect/Context";
58
import * as DateTime from "effect/DateTime";
69
import * as Effect from "effect/Effect";
@@ -17,14 +20,27 @@ import * as Scope from "effect/Scope";
1720
import * as Semaphore from "effect/Semaphore";
1821
import * as SynchronizedRef from "effect/SynchronizedRef";
1922
import * as Tracer from "effect/Tracer";
20-
import { OtlpExporter, OtlpSerialization, OtlpTracer } from "effect/unstable/observability";
23+
import {
24+
OtlpExporter,
25+
OtlpLogger,
26+
OtlpMetrics,
27+
OtlpSerialization,
28+
OtlpTracer,
29+
} from "effect/unstable/observability";
2130

2231
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
32+
import {
33+
type DesktopOtlpResource,
34+
type DesktopOtlpSignal,
35+
resolveDesktopOtlpExport,
36+
} from "./DesktopOtlpExport.ts";
2337

2438
const DESKTOP_LOG_FILE_MAX_BYTES = 10 * 1024 * 1024;
2539
const DESKTOP_LOG_FILE_MAX_FILES = 10;
2640
const DESKTOP_BACKEND_CHILD_LOG_FIBER_ID = "#backend-child";
2741
const DESKTOP_TRACE_BATCH_WINDOW_MS = 1_000;
42+
/** What the main process calls itself when nothing named the service. */
43+
const DESKTOP_SERVICE_NAME = "desktop";
2844
const DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_BYTES = 1024 * 1024;
2945
const DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_CHUNKS = 256;
3046

@@ -322,28 +338,46 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio
322338
} satisfies RotatingLogFileWriter;
323339
});
324340

325-
const readPersistedOtlpTracesUrl: Effect.Effect<
326-
Option.Option<string>,
341+
const noPersistedObservabilitySettings: PersistedServerObservabilitySettings = {
342+
otlpTracesUrl: undefined,
343+
otlpMetricsUrl: undefined,
344+
otlpLogsUrl: undefined,
345+
};
346+
347+
const readPersistedObservabilitySettings: Effect.Effect<
348+
PersistedServerObservabilitySettings,
327349
never,
328350
FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment
329351
> = Effect.gen(function* () {
330352
const fileSystem = yield* FileSystem.FileSystem;
331353
const environment = yield* DesktopEnvironment.DesktopEnvironment;
332354
const raw = yield* fileSystem.readFileString(environment.serverSettingsPath).pipe(Effect.option);
333-
if (Option.isNone(raw)) {
334-
return Option.none();
335-
}
336-
337-
const parsed = parsePersistedServerObservabilitySettings(raw.value);
338-
return Option.fromNullishOr(parsed.otlpTracesUrl);
355+
return Option.isNone(raw)
356+
? noPersistedObservabilitySettings
357+
: parsePersistedServerObservabilitySettings(raw.value);
339358
});
340359

341-
const resolveOtlpTracesUrl = Effect.gen(function* () {
360+
/**
361+
* Read once for all three signals, so the main process cannot resolve traces
362+
* against one revision of Settings and logs against another.
363+
*/
364+
const resolveOtlpExport = Effect.gen(function* () {
342365
const environment = yield* DesktopEnvironment.DesktopEnvironment;
343-
if (Option.isSome(environment.otlpTracesUrl)) {
344-
return environment.otlpTracesUrl;
345-
}
346-
return yield* readPersistedOtlpTracesUrl;
366+
const persisted = yield* readPersistedObservabilitySettings;
367+
return resolveDesktopOtlpExport({
368+
otel: environment.otelEnvironment,
369+
named: {
370+
traces: Option.getOrUndefined(environment.otlpTracesUrl) ?? persisted.otlpTracesUrl,
371+
metrics: Option.getOrUndefined(environment.otlpMetricsUrl) ?? persisted.otlpMetricsUrl,
372+
logs: Option.getOrUndefined(environment.otlpLogsUrl) ?? persisted.otlpLogsUrl,
373+
},
374+
namedExportIntervalMs: Option.getOrUndefined(environment.otlpExportIntervalMs),
375+
defaultServiceName: DESKTOP_SERVICE_NAME,
376+
runtimeAttributes: {
377+
"service.runtime": "desktop",
378+
"service.mode": environment.isDevelopment ? "development" : "packaged",
379+
},
380+
});
347381
});
348382

349383
const writeDevelopmentConsoleOutput = (
@@ -563,52 +597,120 @@ const backendOutputLogFactoryLayer = Layer.effect(
563597
}),
564598
);
565599

566-
const desktopLoggerLayer = Layer.mergeAll(
567-
Logger.layer([Logger.consolePretty(), Logger.tracerLogger], { mergeWithExisting: false }),
568-
Layer.succeed(References.MinimumLogLevel, "Info"),
569-
);
600+
const serializationFor = (signal: DesktopOtlpSignal) =>
601+
signal.protocol === "http/protobuf"
602+
? OtlpSerialization.layerProtobuf
603+
: OtlpSerialization.layerJson;
604+
605+
const otlpResourceFor = (resource: DesktopOtlpResource) => ({
606+
serviceName: resource.serviceName,
607+
...(resource.serviceVersion === undefined ? {} : { serviceVersion: resource.serviceVersion }),
608+
attributes: resource.attributes,
609+
});
570610

571-
const tracerLayer = Layer.unwrap(
611+
/**
612+
* Logs, traces, and metrics for the main process, built together because they
613+
* share one read of the environment and Settings, and because a process gets
614+
* exactly one logger set.
615+
*/
616+
const telemetryLayer = Layer.unwrap(
572617
Effect.gen(function* () {
573618
const environment = yield* DesktopEnvironment.DesktopEnvironment;
574-
const otlpTracesUrl = yield* resolveOtlpTracesUrl;
575-
const tracePath = environment.path.join(environment.logDir, "desktop.trace.ndjson");
576-
const sink = yield* makeTraceSink({
577-
filePath: tracePath,
578-
maxBytes: DESKTOP_LOG_FILE_MAX_BYTES,
579-
maxFiles: DESKTOP_LOG_FILE_MAX_FILES,
580-
batchWindowMs: DESKTOP_TRACE_BATCH_WINDOW_MS,
581-
});
582-
const delegate = Option.isNone(otlpTracesUrl)
583-
? undefined
584-
: yield* OtlpTracer.make({
585-
url: otlpTracesUrl.value,
586-
exportInterval: `${environment.otlpExportIntervalMs} millis`,
587-
resource: {
588-
serviceName: "desktop",
589-
attributes: {
590-
"service.runtime": "desktop",
591-
"service.mode": environment.isDevelopment ? "development" : "packaged",
592-
},
593-
},
619+
const resolved = yield* resolveOtlpExport;
620+
621+
for (const warning of resolved.warnings) {
622+
yield* Effect.logWarning(warning);
623+
}
624+
625+
const otlpResource = otlpResourceFor(resolved.resource);
626+
627+
const otlpLogger =
628+
resolved.logs.url === undefined
629+
? undefined
630+
: OtlpLogger.make({
631+
url: resolved.logs.url,
632+
exportInterval: `${resolved.logs.exportIntervalMs} millis`,
633+
resource: otlpResource,
634+
...(resolved.logs.headers === undefined ? {} : { headers: resolved.logs.headers }),
635+
...(resolved.logs.maxBatchSize === undefined
636+
? {}
637+
: { maxBatchSize: resolved.logs.maxBatchSize }),
638+
});
639+
640+
const loggerLayer = Logger.layer(
641+
[
642+
Logger.consolePretty(),
643+
Logger.tracerLogger,
644+
...(otlpLogger === undefined ? [] : [otlpLogger]),
645+
],
646+
{ mergeWithExisting: false },
647+
).pipe(
648+
Layer.provide(OtlpExporter.layerFlusher),
649+
Layer.provide(serializationFor(resolved.logs)),
650+
);
651+
652+
const tracerLayer = Layer.unwrap(
653+
Effect.gen(function* () {
654+
const tracePath = environment.path.join(environment.logDir, "desktop.trace.ndjson");
655+
const sink = yield* makeTraceSink({
656+
filePath: tracePath,
657+
maxBytes: DESKTOP_LOG_FILE_MAX_BYTES,
658+
maxFiles: DESKTOP_LOG_FILE_MAX_FILES,
659+
batchWindowMs: DESKTOP_TRACE_BATCH_WINDOW_MS,
660+
});
661+
const delegate =
662+
resolved.traces.url === undefined
663+
? undefined
664+
: yield* OtlpTracer.make({
665+
url: resolved.traces.url,
666+
exportInterval: `${resolved.traces.exportIntervalMs} millis`,
667+
resource: otlpResource,
668+
...(resolved.traces.headers === undefined
669+
? {}
670+
: { headers: resolved.traces.headers }),
671+
...(resolved.traces.maxBatchSize === undefined
672+
? {}
673+
: { maxBatchSize: resolved.traces.maxBatchSize }),
674+
});
675+
const tracer = yield* makeLocalFileTracer({
676+
filePath: tracePath,
677+
maxBytes: DESKTOP_LOG_FILE_MAX_BYTES,
678+
maxFiles: DESKTOP_LOG_FILE_MAX_FILES,
679+
batchWindowMs: DESKTOP_TRACE_BATCH_WINDOW_MS,
680+
sink,
681+
...(delegate ? { delegate } : {}),
594682
});
595-
const tracer = yield* makeLocalFileTracer({
596-
filePath: tracePath,
597-
maxBytes: DESKTOP_LOG_FILE_MAX_BYTES,
598-
maxFiles: DESKTOP_LOG_FILE_MAX_FILES,
599-
batchWindowMs: DESKTOP_TRACE_BATCH_WINDOW_MS,
600-
sink,
601-
...(delegate ? { delegate } : {}),
602-
});
603683

604-
return Layer.succeed(Tracer.Tracer, tracer);
684+
return Layer.succeed(Tracer.Tracer, tracer);
685+
}),
686+
).pipe(
687+
Layer.provide(OtlpExporter.layerFlusher),
688+
Layer.provide(serializationFor(resolved.traces)),
689+
);
690+
691+
const metricsLayer =
692+
resolved.metrics.url === undefined
693+
? Layer.empty
694+
: OtlpMetrics.layer({
695+
url: resolved.metrics.url,
696+
exportInterval: `${resolved.metrics.exportIntervalMs} millis`,
697+
resource: otlpResource,
698+
...(resolved.metrics.headers === undefined
699+
? {}
700+
: { headers: resolved.metrics.headers }),
701+
...(resolved.metrics.temporality === undefined
702+
? {}
703+
: { temporality: resolved.metrics.temporality }),
704+
}).pipe(Layer.provide(serializationFor(resolved.metrics)));
705+
706+
return Layer.mergeAll(loggerLayer, tracerLayer, metricsLayer);
605707
}),
606-
).pipe(Layer.provide(OtlpExporter.layerFlusher), Layer.provideMerge(OtlpSerialization.layerJson));
708+
);
607709

608710
export const layer = Layer.mergeAll(
609711
backendOutputLogFactoryLayer,
610-
desktopLoggerLayer,
611-
tracerLayer,
712+
telemetryLayer,
713+
Layer.succeed(References.MinimumLogLevel, "Info"),
612714
Layer.succeed(Tracer.MinimumTraceLevel, "Info"),
613715
Layer.succeed(References.TracerTimingEnabled, true),
614716
);

0 commit comments

Comments
0 (0)
You are visitor 01402809 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.