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

Observability.ts

98 lines3.7 KB

1import { httpHeaderRedactionLayer } from "@t3tools/shared/httpObservability";
2import {
3 makeLocalFileTracer,
4 makeTraceSink,
5 otlpSerializationLayer,
6} from "@t3tools/shared/observability";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as References from "effect/References";
10import * as Tracer from "effect/Tracer";
11import * as OtlpExporter from "effect/unstable/observability/OtlpExporter";
12import * as OtlpMetrics from "effect/unstable/observability/OtlpMetrics";
13import * as OtlpTracer from "effect/unstable/observability/OtlpTracer";
14​
15import * as ServerConfig from "../../config.ts";
16import * as ResourceAttribution from "../../resourceTelemetry/ResourceAttribution.ts";
17import { ServerLoggerLive } from "../../serverLogger.ts";
18import * as BrowserTraceCollector from "../BrowserTraceCollector.ts";
19​
20export const ObservabilityLive = Layer.unwrap(
21 Effect.gen(function* () {
22 const config = yield* ServerConfig.ServerConfig;
23 const serializationLayer = otlpSerializationLayer(config.otlpProtocol);
24 const attribution = yield* ResourceAttribution.ResourceAttribution;
25​
26 const traceReferencesLayer = Layer.mergeAll(
27 Layer.succeed(Tracer.MinimumTraceLevel, config.traceMinLevel),
28 Layer.succeed(References.TracerTimingEnabled, config.traceTimingEnabled),
29 httpHeaderRedactionLayer,
30 );
31​
32 const tracerLayer = Layer.unwrap(
33 Effect.gen(function* () {
34 const sink = yield* makeTraceSink({
35 filePath: config.serverTracePath,
36 maxBytes: config.traceMaxBytes,
37 maxFiles: config.traceMaxFiles,
38 batchWindowMs: config.traceBatchWindowMs,
39 onFlush: (stats) =>
40 attribution.record({
41 component: "server-trace",
42 operation: "append",
43 logicalWriteBytes: stats.logicalWriteBytes,
44 count: stats.count,
45 durationMs: stats.durationMs,
46 }),
47 });
48 const delegate =
49 config.otlpTracesUrl === undefined
50 ? undefined
51 : yield* OtlpTracer.make({
52 url: config.otlpTracesUrl,
53 exportInterval: `${config.otlpExportIntervalMs} millis`,
54 headers: config.otlpHeaders,
55 resource: {
56 serviceName: config.otlpServiceName,
57 attributes: {
58 "service.runtime": "t3-server",
59 "service.mode": config.mode,
60 },
61 },
62 });
63​
64 const tracer = yield* makeLocalFileTracer({
65 filePath: config.serverTracePath,
66 maxBytes: config.traceMaxBytes,
67 maxFiles: config.traceMaxFiles,
68 batchWindowMs: config.traceBatchWindowMs,
69 sink,
70 ...(delegate ? { delegate } : {}),
71 });
72​
73 return Layer.mergeAll(
74 Layer.succeed(Tracer.Tracer, tracer),
75 BrowserTraceCollector.layer(sink),
76 );
77 }),
78 ).pipe(Layer.provide(OtlpExporter.layerFlusher), Layer.provideMerge(serializationLayer));
79​
80 const metricsLayer =
81 config.otlpMetricsUrl === undefined
82 ? Layer.empty
83 : OtlpMetrics.layer({
84 url: config.otlpMetricsUrl,
85 exportInterval: `${config.otlpExportIntervalMs} millis`,
86 headers: config.otlpHeaders,
87 resource: {
88 serviceName: config.otlpServiceName,
89 attributes: {
90 "service.runtime": "t3-server",
91 "service.mode": config.mode,
92 },
93 },
94 }).pipe(Layer.provideMerge(serializationLayer));
95​
96 return Layer.mergeAll(ServerLoggerLive, traceReferencesLayer, tracerLayer, metricsLayer);
97 }),
98);