Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderEventLoggers.ts

71 lines2.8 KB

1/**
2 * ProviderEventLoggers — single observability service that owns the shared
3 * provider event log store and exposes its two runtime views:
4 *
5 * - `native` — provider-protocol events as the SDK emits them, written
6 * from inside each `<X>Adapter` factory.
7 * - `canonical` — runtime events after `ProviderService` has normalized
8 * them onto `ProviderRuntimeEvent`.
9 *
10 * Why a service tag and not constructor options?
11 *
12 * - Adapters are now constructed *inside* drivers (`<X>Driver.create()`),
13 * not at the boot Layer. There is no longer a single `make<X>AdapterLive(options)`
14 * call site where we can hand an `EventNdjsonLogger` in by hand.
15 * - Multiple driver instances per kind (`codex_personal`, `codex_work`)
16 * must share one underlying log store — opening N writers against the
17 * same rotating file would race the rotation logic. Owning the loggers on
18 * a single tag keeps that invariant intact.
19 * - Tests can swap one (or both) loggers with in-memory recorders by
20 * `Layer.succeed(ProviderEventLoggers, { native, canonical })` instead of
21 * juggling per-Layer option threading.
22 *
23 * Both fields are optional because observability must not prevent startup.
24 *
25 * @module provider/ProviderEventLoggers
26 */
27import * as Effect from "effect/Effect";
28import * as Layer from "effect/Layer";
29​
30import * as ServerConfig from "../config.ts";
31import * as ResourceAttribution from "../resourceTelemetry/ResourceAttribution.ts";
32import {
33 NoOpProviderEventLoggers,
34 ProviderEventLoggers,
35} from "@t3tools/provider-core/server/ProviderEventLoggers";
36import * as EventNdjsonLogger from "./EventNdjsonLogger.ts";
37​
38export { NoOpProviderEventLoggers, ProviderEventLoggers };
39​
40/**
41 * Builds both stream views over one shared store. Setup failures are logged
42 * and downgraded to the no-op service so diagnostics never block startup.
43 *
44 * @public Service construction is part of the canonical Effect module API.
45 */
46export const make = Effect.gen(function* () {
47 const { providerEventLogPath } = yield* ServerConfig.ServerConfig;
48 const attribution = yield* ResourceAttribution.ResourceAttribution;
49 const store = yield* EventNdjsonLogger.makeEventNdjsonLogStore(providerEventLogPath, {
50 attribution,
51 }).pipe(
52 Effect.catch((error) =>
53 Effect.logWarning(error.message, { error }).pipe(
54 Effect.annotateLogs({ scope: "provider-observability" }),
55 Effect.as<EventNdjsonLogger.EventNdjsonLogStore | undefined>(undefined),
56 ),
57 ),
58 );
59​
60 if (!store) {
61 return ProviderEventLoggers.of(NoOpProviderEventLoggers);
62 }
63​
64 yield* Effect.addFinalizer(() => store.close());
65 return ProviderEventLoggers.of({
66 native: store.logger("native"),
67 canonical: store.logger("canonical"),
68 });
69});
70​
71export const layer = Layer.effect(ProviderEventLoggers, make);