mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-20 01:07:23 +00:00
83 lines
3.4 KiB
TypeScript
83 lines
3.4 KiB
TypeScript
import * as HostProcess from "@hanzodev/shared/HostProcess";
|
|
import * as SharedObservability from "@hanzodev/shared/observability";
|
|
import * as Cause from "effect/Cause";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Logger from "effect/Logger";
|
|
import * as Redactable from "effect/Redactable";
|
|
import * as References from "effect/References";
|
|
import * as OtlpExporter from "effect/observability/OtlpExporter";
|
|
import * as OtlpLogger from "effect/observability/OtlpLogger";
|
|
|
|
import * as ServerConfig from "./config.ts";
|
|
import * as OtlpExportFailures from "./observability/OtlpExportFailures.ts";
|
|
|
|
// HTTP errors logged with --log-ws-events carry the request URL and its connection ticket.
|
|
const redactingCredentials = <Output>(logger: Logger.Logger<unknown, Output>) =>
|
|
Logger.make<unknown, Output>((options) =>
|
|
logger.log({ ...options, cause: SharedObservability.redactCauseCredentials(options.cause) }),
|
|
);
|
|
|
|
// consolePretty collapses Cause fields via util.inspect; render them with Cause.pretty like standalone arguments.
|
|
const consolePretty = (): Logger.Logger<unknown, void> => {
|
|
const pretty = redactingCredentials(Logger.consolePretty());
|
|
return Logger.make((options) =>
|
|
pretty.log({
|
|
...options,
|
|
message: Array.isArray(options.message)
|
|
? options.message.map(prettyCauseFields)
|
|
: prettyCauseFields(options.message),
|
|
}),
|
|
);
|
|
};
|
|
|
|
function prettyCauseFields(value: unknown): unknown {
|
|
if (typeof value !== "object" || value === null) return value;
|
|
const prototype: unknown = Object.getPrototypeOf(value);
|
|
if (prototype !== Object.prototype && prototype !== null) return value;
|
|
// A message with its own redaction hook already decides how it prints.
|
|
if (Redactable.isRedactable(value)) return value;
|
|
// Copy descriptors to preserve symbols without invoking getters.
|
|
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
let changed = false;
|
|
for (const descriptor of Object.values(descriptors)) {
|
|
if ("value" in descriptor && Cause.isCause(descriptor.value)) {
|
|
descriptor.value = Cause.pretty(descriptor.value);
|
|
changed = true;
|
|
}
|
|
}
|
|
return changed ? Object.create(prototype, descriptors) : value;
|
|
}
|
|
|
|
export const layer = Effect.gen(function* () {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const hostName = yield* HostProcess.Hostname;
|
|
const layerMinimumLogLevel = Layer.succeed(References.MinimumLogLevel, config.logLevel);
|
|
|
|
const logs = config.otlpLogsExport;
|
|
const otlpLogger =
|
|
config.otlpLogsUrl === undefined
|
|
? undefined
|
|
: OtlpLogger.make({
|
|
url: config.otlpLogsUrl,
|
|
exportInterval: `${logs.exportIntervalMs} millis`,
|
|
headers: logs.headers,
|
|
resource: ServerConfig.otlpResource(config, hostName),
|
|
}).pipe(Effect.map(redactingCredentials));
|
|
|
|
// Logger.layer replaces the whole set; name every logger here.
|
|
// OTLP includes traced messages as log records, so also using tracerLogger would duplicate in-span exports.
|
|
const layerLogger = Logger.layer(
|
|
otlpLogger === undefined
|
|
? [consolePretty(), Logger.tracerLogger]
|
|
: [consolePretty(), otlpLogger],
|
|
{ mergeWithExisting: false },
|
|
).pipe(
|
|
Layer.provide(OtlpExporter.layerFlusher),
|
|
Layer.provide(OtlpExportFailures.layer("logs")),
|
|
Layer.provide(SharedObservability.layerOtlpSerialization(logs.protocol)),
|
|
);
|
|
|
|
return Layer.mergeAll(layerLogger, layerMinimumLogLevel);
|
|
}).pipe(Layer.unwrap);
|