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

relayTracing.test.ts

174 lines6.0 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Cause from "effect/Cause";
3import * as ConfigProvider from "effect/ConfigProvider";
4import * as Effect from "effect/Effect";
5import * as Exit from "effect/Exit";
6import * as Layer from "effect/Layer";
7import * as Option from "effect/Option";
8import * as Tracer from "effect/Tracer";
9import { FetchHttpClient } from "effect/http";
10import { vi } from "vite-plus/test";
11​
12import {
13 layer,
14 nonInterferingTracer,
15 RelayClientTracer,
16 withLocalTracing,
17 withRelayClientTracing,
18} from "./relayTracing.ts";
19​
20function collectingTracer(spans: Array<string>): Tracer.Tracer {
21 return Tracer.make({
22 span: (options) => {
23 const span = new Tracer.NativeSpan(options);
24 const end = span.end.bind(span);
25 span.end = (endTime, exit) => {
26 end(endTime, exit);
27 spans.push(span.name);
28 };
29 return span;
30 },
31 });
33​
34describe("withRelayClientTracing", () => {
35 it.effect("uses the product tracer only for relay operations", () =>
36 Effect.gen(function* () {
37 const userSpans: Array<string> = [];
38 const productSpans: Array<string> = [];
39 const userTracer = collectingTracer(userSpans);
40 const productTracer = collectingTracer(productSpans);
41​
42 yield* Effect.void.pipe(Effect.withSpan("user.operation"), Effect.withTracer(userTracer));
43 yield* Effect.void.pipe(
44 Effect.withSpan("relay.operation"),
45 withRelayClientTracing,
46 Effect.provideService(RelayClientTracer, Option.some(productTracer)),
47 Effect.withTracer(userTracer),
48 );
49​
50 expect(userSpans).toEqual(["user.operation"]);
51 expect(productSpans).toEqual(["relay.operation"]);
52 }),
53 );
54});
55​
56describe("nonInterferingTracer", () => {
57 it.effect("exports failure types without messages or frames", () =>
58 Effect.gen(function* () {
59 const exits: Array<Exit.Exit<unknown, unknown>> = [];
60 const delegate = Tracer.make({
61 span: (options) => {
62 const span = new Tracer.NativeSpan(options);
63 const end = span.end.bind(span);
64 span.end = (endTime, exit) => {
65 end(endTime, exit);
66 exits.push(exit);
67 };
68 return span;
69 },
70 });
71 const failure = Object.assign(
72 new Error("studio-mac could not establish a WebSocket connection.", {
73 cause: new Error("connect ECONNREFUSED studio-mac.local"),
74 }),
75 { name: "ConnectionTransientError" },
76 );
77​
78 failure.stack = " at connect (/home/user/private.mjs:12:34)";
79​
80 yield* Effect.fail(failure).pipe(
81 Effect.withSpan("relay.connection.attempt"),
82 Effect.withTracer(nonInterferingTracer(delegate)),
83 Effect.ignore,
84 );
85​
86 const exit = exits[0];
87 if (exit === undefined || !Exit.isFailure(exit)) throw new Error("expected a failed span");
88 const exported = Cause.prettyErrors(exit.cause);
89 expect(exported.map((error) => error.name)).toEqual(["ConnectionTransientError"]);
90 const text = exported.map((error) => `${error.message}\n${error.stack}`).join("\n");
91 expect(text).toContain("Caused by: Error");
92 expect(text).not.toContain("studio-mac");
93 expect(text).not.toContain("private.mjs");
94 }),
95 );
96});
97​
98it.effect.each([true, false])("filters relay exports at flush (%s)", (enabledAtFlush) => {
99 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
100 let enabled = true;
101 return Effect.logWarning("private error detail").pipe(
102 Effect.withSpan("relay.request", {
103 attributes: {
104 "url.full": "http://studio-mac.local:3773/api/threads?cursor=secret",
105 "url.path": "/api/threads",
106 "url.query": "cursor=secret",
107 "server.address": "studio-mac.local",
108 "server.port": 3773,
109 },
110 }),
111 withRelayClientTracing,
112 Effect.andThen(Effect.sync(() => (enabled = enabledAtFlush))),
113 Layer.effectDiscard,
114 Layer.provide(
115 layer(
116 { tracesUrl: "https://traces.example.com", tracesDataset: "relay", tracesToken: "token" },
117 { serviceName: "t3code-test", runtime: "node", serviceInstanceId: "id", client: "test" },
118 { isEnabled: () => enabled },
119 ),
120 ),
121 Layer.provide(FetchHttpClient.layer),
122 Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetchFn)),
123 Layer.provide(
124 ConfigProvider.layer(
125 ConfigProvider.fromEnvRecord({ OTEL_RESOURCE_ATTRIBUTES: "host.name=private-host" }),
126 ),
127 ),
128 Layer.build,
129 Effect.scoped,
130 Effect.andThen(
131 Effect.sync(() => {
132 expect(fetchFn).toHaveBeenCalledTimes(Number(enabledAtFlush));
133 if (!enabledAtFlush) return;
134 const sent = new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array);
135 expect(sent).toContain("/api/threads");
136 expect(sent).not.toMatch(
137 /studio-mac|cursor=secret|server\.port|private error detail|private-host/,
138 );
139 }),
140 ),
141 );
142});
143​
144describe("withLocalTracing", () => {
145 it.effect("keeps local work inside a relay span off the product tracer", () =>
146 Effect.gen(function* () {
147 const localSpans: Array<string> = [];
148 const productSpans: Array<string> = [];
149 const localTracer = collectingTracer(localSpans);
150 const productTracer = collectingTracer(productSpans);
151​
152 yield* Effect.void.pipe(
153 Effect.withSpan("relay.connection.nested"),
154 Effect.andThen(
155 Effect.void.pipe(
156 Effect.withSpan("sql.execute"),
157 Effect.withSpan("ServerSecretStore.get"),
158 withLocalTracing,
159 ),
160 ),
161 Effect.withSpan("environment.orchestration.threadSnapshot"),
162 withRelayClientTracing,
163 Effect.provideService(RelayClientTracer, Option.some(productTracer)),
164 Effect.withTracer(localTracer),
165 );
166​
167 expect(productSpans).toEqual([
168 "relay.connection.nested",
169 "environment.orchestration.threadSnapshot",
170 ]);
171 expect(localSpans).toEqual(["sql.execute", "ServerSecretStore.get"]);
172 }),
173 );
174});