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

ProviderEventLoggers.ts

90 lines3.5 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/Layers/ProviderEventLoggers
26 */
27import * as Context from "effect/Context";
28import * as Effect from "effect/Effect";
29import * as Layer from "effect/Layer";
30​
31import { ServerConfig } from "../../config.ts";
32import * as ResourceAttribution from "../../resourceTelemetry/ResourceAttribution.ts";
33import * as EventNdjsonLogger from "./EventNdjsonLogger.ts";
34​
35/**
36 * Shared logger pair for native + canonical provider event streams.
37 *
38 * Service value is intentionally a struct of two optional loggers rather
39 * than two parallel tags. Construction site is one place
40 * (`layer`); consumers (drivers, `ProviderService`) read one tag and pluck the
41 * field they need.
42 */
43export class ProviderEventLoggers extends Context.Service<
44 ProviderEventLoggers,
45 {
46 readonly native: EventNdjsonLogger.EventNdjsonLogger | undefined;
47 readonly canonical: EventNdjsonLogger.EventNdjsonLogger | undefined;
48 }
49>()("t3/provider/Layers/ProviderEventLoggers") {}
50​
51/**
52 * Constant value used by tests / boot layers that want to opt out of native
53 * + canonical logging entirely. Keeps the tag non-optional in the type
54 * system while letting the runtime treat absence as a no-op.
55 */
56export const NoOpProviderEventLoggers: ProviderEventLoggers["Service"] = {
57 native: undefined,
58 canonical: undefined,
59};
60​
61/**
62 * Builds both stream views over one shared store. Setup failures are logged
63 * and downgraded to the no-op service so diagnostics never block startup.
64 */
65export const make = Effect.gen(function* () {
66 const { providerEventLogPath } = yield* ServerConfig;
67 const attribution = yield* ResourceAttribution.ResourceAttribution;
68 const store = yield* EventNdjsonLogger.makeEventNdjsonLogStore(providerEventLogPath, {
69 attribution,
70 }).pipe(
71 Effect.catch((error) =>
72 Effect.logWarning(error.message, { error }).pipe(
73 Effect.annotateLogs({ scope: "provider-observability" }),
74 Effect.as<EventNdjsonLogger.EventNdjsonLogStore | undefined>(undefined),
75 ),
76 ),
77 );
78​
79 if (!store) {
80 return ProviderEventLoggers.of(NoOpProviderEventLoggers);
81 }
82​
83 yield* Effect.addFinalizer(() => store.close());
84 return ProviderEventLoggers.of({
85 native: store.logger("native"),
86 canonical: store.logger("canonical"),
87 });
88});
89​
90export const layer = Layer.effect(ProviderEventLoggers, make);