Repository navigation
Expand file tree
/
Copy pathserverLogger.test.ts
More file actions
317 lines (290 loc) · 11.1 KB
/
Copy pathserverLogger.test.ts
File metadata and controls
317 lines (290 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import * as NodePath from "@effect/platform-node/NodePath";
import { assert, describe, it } from "@effect/vitest";
import * as NodeOS from "node:os";
import * as NodeUtil from "node:util";
import { ExternalLauncherBrowserSpawnError } from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Console from "effect/Console";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
import * as Redactable from "effect/Redactable";
import * as Path from "effect/Path";
import * as Tracer from "effect/Tracer";
import * as HttpClient from "effect/http/HttpClient";
import * as HttpClientResponse from "effect/http/HttpClientResponse";
import * as TestConsole from "effect/testing/TestConsole";
import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
import * as ServerConfig from "./config.ts";
import * as ExternalLauncher from "./process/externalLauncher.ts";
import * as ServerLogger from "./serverLogger.ts";
import {
announceStartupPairingUrl,
logBrowserAutoOpenUnavailable,
maybeOpenBrowser,
} from "./serverRuntimeStartup.ts";
interface ExportedRequest {
readonly url: string;
readonly headers: Readonly<Record<string, string>>;
readonly body: string;
}
const layerCollector = (requests: Array<ExportedRequest>) =>
Layer.succeed(
HttpClient.HttpClient,
HttpClient.make((request) =>
Effect.sync(() => {
requests.push({
url: request.url,
headers: request.headers,
body:
request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : "",
});
return HttpClientResponse.fromWeb(request, new Response(null, { status: 200 }));
}),
),
);
const layerConfig = (overrides: Partial<ServerConfig.ServerConfig["Service"]>) =>
Layer.effect(
ServerConfig.ServerConfig,
Effect.gen(function* () {
const path = yield* Path.Path;
const baseDir = path.join(NodeOS.tmpdir(), "t3-server-logger-test");
const derivedPaths = yield* ServerConfig.deriveServerPaths(baseDir, undefined);
return ServerConfig.make({
logLevel: "Info",
traceMinLevel: "Info",
traceTimingEnabled: false,
traceBatchWindowMs: 200,
traceMaxBytes: 1024,
traceMaxFiles: 1,
otlpTracesUrl: undefined,
otlpMetricsUrl: undefined,
otlpLogsUrl: undefined,
otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
otelEnvironment: OtelEnvironment.none,
cwd: baseDir,
baseDir,
...derivedPaths,
mode: "web",
autoBootstrapProjectFromCwd: false,
logWebSocketEvents: false,
tailscaleServeEnabled: false,
tailscaleServePort: 443,
port: 0,
host: undefined,
desktopBootstrapToken: undefined,
desktopTelemetryFd: undefined,
desktopTelemetryControlFd: undefined,
resourceMonitorPath: undefined,
staticDir: undefined,
devUrl: undefined,
devAllowedOrigins: [],
noBrowser: false,
startupPresentation: "browser",
...overrides,
});
}),
).pipe(Layer.provide(NodePath.layer));
/**
* Runs `program` (one log line by default) inside a span so the tracer logger
* has somewhere to attach an event, then reports both what the collector
* received and what landed on the span.
*/
const logInSpanThrough = (
overrides: Partial<ServerConfig.ServerConfig["Service"]>,
program: Effect.Effect<void> = Effect.log("server logger under test"),
) =>
Effect.gen(function* () {
const requests: Array<ExportedRequest> = [];
const spans: Array<Tracer.NativeSpan> = [];
const layerTracer = Layer.succeed(
Tracer.Tracer,
Tracer.make({
span: (spanOptions) => {
const span = new Tracer.NativeSpan(spanOptions);
spans.push(span);
return span;
},
}),
);
yield* program.pipe(
Effect.withSpan("server-logger-test"),
Effect.provide(
Layer.mergeAll(
ServerLogger.layer.pipe(
Layer.provide(layerConfig(overrides)),
Layer.provide(layerCollector(requests)),
),
layerTracer,
),
),
);
return { requests, spans };
});
const printThroughConsole = (fields: object) =>
Effect.gen(function* () {
const printed: Array<string> = [];
const testConsole = {
...globalThis.console,
log: (...args: ReadonlyArray<unknown>) => {
printed.push(NodeUtil.format(...args));
},
} satisfies Console.Console;
yield* Effect.logWarning("pull request watch check failed", fields).pipe(
Effect.provide(
ServerLogger.layer.pipe(Layer.provide(layerConfig({})), Layer.provide(layerCollector([]))),
),
Effect.provideService(Console.Console, testConsole),
);
return printed.join("\n");
});
describe("ServerLoggerLive", () => {
it.effect("exports the operator's service instance id instead of a generated one", () =>
Effect.gen(function* () {
const { requests } = yield* logInSpanThrough({
otlpLogsUrl: "https://collector.example.com/v1/logs",
otelEnvironment: {
...OtelEnvironment.none,
resourceAttributes: { "service.instance.id": "pod-7" },
},
});
const body = requests.map((request) => request.body).join("\n");
assert.include(body, '"key":"service.instance.id","value":{"stringValue":"pod-7"}');
}),
);
it.effect("exports a resource that names whether the desktop app launched the server", () =>
Effect.gen(function* () {
const { requests } = yield* logInSpanThrough({
otlpLogsUrl: "https://collector.example.com/v1/logs",
mode: "desktop",
});
const body = requests.map((request) => request.body).join("\n");
assert.include(body, '"key":"t3code.server.managed_by","value":{"stringValue":"desktop"}');
assert.notInclude(body, "service.mode");
}),
);
it.effect("exports logged request failures without their connection tickets", () =>
Effect.gen(function* () {
const { requests } = yield* logInSpanThrough(
{ otlpLogsUrl: "https://collector.example.com/v1/logs" },
Effect.logError(
Cause.die(
new Error("RequestParseError (GET /ws?wsTicket=ticket-secret&dpop=proof-secret)"),
),
),
);
const body = requests.map((request) => request.body).join("\n");
assert.include(body, "wsTicket=<redacted>&dpop=<redacted>");
assert.notInclude(body, "ticket-secret");
assert.notInclude(body, "proof-secret");
}),
);
it.effect("prints a cause passed as a log field instead of collapsing it", () =>
Effect.gen(function* () {
const output = yield* printThroughConsole({
threadId: "thread-1",
cause: Cause.fail(new Error("gh: HTTP 502 from api.github.com")),
});
assert.include(output, "threadId: 'thread-1'");
assert.include(output, "gh: HTTP 502 from api.github.com");
assert.notInclude(output, "[Object]");
}),
);
// regression: #16642 logs printed causes as [Object]
it.effect("leaves getters uncalled while printing a cause field", () =>
Effect.gen(function* () {
const output = yield* printThroughConsole({
cause: Cause.fail(new Error("upstream refused")),
get detail(): string {
throw new Error("getter must not run");
},
});
assert.include(output, "upstream refused");
assert.notInclude(output, "getter must not run");
}),
);
it.effect("leaves a message with its own redaction hook to that hook", () =>
Effect.gen(function* () {
const output = yield* printThroughConsole({
token: "secret-token-value",
cause: Cause.fail(new Error("upstream refused")),
[Redactable.symbolRedactable](this: { readonly cause: unknown }) {
return { token: "<redacted>", cause: Cause.isCause(this.cause) ? "cause" : "missing" };
},
});
assert.include(output, "token: '<redacted>'");
assert.include(output, "cause: 'cause'");
assert.notInclude(output, "secret-token-value");
}),
);
describe("startup pairing URL", () => {
const pairingToken = "PAIRTOKEN7Q4K9";
const pairingUrl = `http://localhost:3773/pair#token=${pairingToken}`;
const announce = announceStartupPairingUrl(pairingUrl).pipe(
Effect.andThen(logBrowserAutoOpenUnavailable),
);
it.effect("prints the full URL but keeps its token out of exported log records", () =>
Effect.gen(function* () {
const { requests } = yield* logInSpanThrough(
{ otlpLogsUrl: "https://collector.example.com/v1/logs" },
announce,
);
const body = requests.map((request) => request.body).join("\n");
assert.include(body, "Authentication required");
assert.notInclude(body, pairingToken);
const printed = (yield* TestConsole.logLines).map((line) =>
Array.isArray(line) ? line.join(" ") : String(line),
);
assert.isTrue(printed.some((line) => line.includes(`pairingUrl: ${pairingUrl}`)));
}).pipe(Effect.provide(TestConsole.layer)),
);
it.effect("keeps the token off the span events the trace file records", () =>
Effect.gen(function* () {
const { spans } = yield* logInSpanThrough({}, announce);
const events = JSON.stringify(
spans.flatMap((span) => span.events.map(([name, , attributes]) => [name, attributes])),
);
assert.include(events, "Authentication required");
assert.notInclude(events, pairingToken);
}).pipe(Effect.provide(TestConsole.layer)),
);
it.effect("keeps the token out of traces when the browser fails to open", () =>
Effect.gen(function* () {
const failingLaunch = maybeOpenBrowser(pairingUrl).pipe(
Effect.provide(
Layer.mergeAll(
layerConfig({ noBrowser: false }),
Layer.mock(ExternalLauncher.ExternalLauncher)({
launchBrowser: (target) =>
Effect.fail(
new ExternalLauncherBrowserSpawnError({
target,
command: "xdg-open",
args: [target],
cause: new Error("spawn xdg-open ENOENT"),
}),
).pipe(Effect.withSpan("externalLauncher.launchBrowser")),
}),
),
),
);
const { spans } = yield* logInSpanThrough({}, failingLaunch);
const recorded = JSON.stringify(
spans.map((span) => ({
name: span.name,
events: span.events.map(([name, , attributes]) => [name, attributes]),
exit:
span.status._tag === "Ended" && Exit.isFailure(span.status.exit)
? Cause.pretty(span.status.exit.cause)
: null,
})),
);
assert.include(recorded, "browser auto-open unavailable");
assert.notInclude(recorded, pairingToken);
}),
);
});
});