Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5import * as Tracer from "effect/Tracer";
6import { FetchHttpClient } from "effect/http";
7import { vi } from "vite-plus/test";
8
9import { RelayClientTracer, withLocalTracing, withRelayClientTracing } from "./relayTracing.ts";
10import * as RelayTracing from "./relayTracing.ts";
11
12function collectingTracer(spans: Array<string>): Tracer.Tracer {
13 return Tracer.make({
14 span: (options) => {
15 const span = new Tracer.NativeSpan(options);
16 const end = span.end.bind(span);
17 span.end = (endTime, exit) => {
18 end(endTime, exit);
19 spans.push(span.name);
20 };
21 return span;
22 },
23 });
24}
25
26describe("withRelayClientTracing", () => {
27 it.effect("uses the product tracer only for relay operations", () =>
28 Effect.gen(function* () {
29 const userSpans: Array<string> = [];
30 const productSpans: Array<string> = [];
31 const userTracer = collectingTracer(userSpans);
32 const productTracer = collectingTracer(productSpans);
33
34 yield* Effect.void.pipe(Effect.withSpan("user.operation"), Effect.withTracer(userTracer));
35 yield* Effect.void.pipe(
36 Effect.withSpan("relay.operation"),
37 withRelayClientTracing,
38 Effect.provideService(RelayClientTracer, Option.some(productTracer)),
39 Effect.withTracer(userTracer),
40 );
41
42 expect(userSpans).toEqual(["user.operation"]);
43 expect(productSpans).toEqual(["relay.operation"]);
44 }),
45 );
46
47 it.effect("preserves the active tracer when product tracing is disabled", () =>
48 Effect.gen(function* () {
49 const userSpans: Array<string> = [];
50 const userTracer = collectingTracer(userSpans);
51
52 yield* Effect.void.pipe(
53 Effect.withSpan("relay.operation"),
54 withRelayClientTracing,
55 Effect.withTracer(userTracer),
56 );
57
58 expect(userSpans).toEqual(["relay.operation"]);
59 }),
60 );
61
62 it.effect("preserves nested error causes in exported relay spans", () => {
63 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
64 const layerHttpClient = FetchHttpClient.layer.pipe(
65 Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetchFn)),
66 );
67 const layerTracing = RelayTracing.layer(
68 {
69 tracesUrl: "https://api.axiom.test/v1/traces",
70 tracesDataset: "relay-traces",
71 tracesToken: "public-ingest-token",
72 },
73 {
74 serviceName: "relay-test",
75 runtime: "test",
76 client: "test",
77 },
78 ).pipe(Layer.provide(layerHttpClient));
79 const rootCause = new Error("relay socket closed");
80 const failure = new Error("relay request failed", { cause: rootCause });
81 const layerTracedApplication = Layer.effectDiscard(
82 Effect.fail(failure).pipe(
83 Effect.withSpan("relay.failed-operation"),
84 withRelayClientTracing,
85 Effect.exit,
86 ),
87 ).pipe(Layer.provide(layerTracing));
88
89 return Layer.build(layerTracedApplication).pipe(
90 Effect.scoped,
91 Effect.andThen(
92 Effect.sync(() => {
93 expect(fetchFn).toHaveBeenCalledOnce();
94 const payload = new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array);
95 expect(payload).toContain("relay request failed");
96 expect(payload).toContain("relay socket closed");
97 expect(payload).toContain('"key":"service.name","value":{"stringValue":"relay-test"}');
98 expect(payload).toContain('"key":"service.namespace","value":{"stringValue":"t3code"}');
99 }),
100 ),
101 );
102 });
103});
104
105describe("withLocalTracing", () => {
106 it.effect("keeps local work inside a relay span off the product tracer", () =>
107 Effect.gen(function* () {
108 const localSpans: Array<string> = [];
109 const productSpans: Array<string> = [];
110 const localTracer = collectingTracer(localSpans);
111 const productTracer = collectingTracer(productSpans);
112
113 yield* Effect.void.pipe(
114 Effect.withSpan("relay.connection.nested"),
115 Effect.andThen(
116 Effect.void.pipe(
117 Effect.withSpan("sql.execute"),
118 Effect.withSpan("ServerSecretStore.get"),
119 withLocalTracing,
120 ),
121 ),
122 Effect.withSpan("environment.orchestration.threadSnapshot"),
123 withRelayClientTracing,
124 Effect.provideService(RelayClientTracer, Option.some(productTracer)),
125 Effect.withTracer(localTracer),
126 );
127
128 expect(productSpans).toEqual([
129 "relay.connection.nested",
130 "environment.orchestration.threadSnapshot",
131 ]);
132 expect(localSpans).toEqual(["sql.execute", "ServerSecretStore.get"]);
133 }),
134 );
135
136 it.effect("leaves the current tracer alone outside relay tracing", () =>
137 Effect.gen(function* () {
138 const localSpans: Array<string> = [];
139
140 yield* Effect.void.pipe(
141 Effect.withSpan("sql.execute"),
142 withLocalTracing,
143 Effect.withTracer(collectingTracer(localSpans)),
144 );
145
146 expect(localSpans).toEqual(["sql.execute"]);
147 }),
148 );
149});