Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Tracer from "effect/Tracer";
5import { HttpServerRequest } from "effect/http";
6import { RelayClientTracer } from "@t3tools/shared/relayTracing";
7
8import {
9 traceAuthenticatedRelayRequest,
10 traceLocalHandlerWork,
11 traceRelayRequest,
12} from "./traceRelayRequest.ts";
13
14const AUTHENTICATION = { startTime: 1_000n, endTime: 2_000n };
15
16describe("relay request tracing", () => {
17 it.effect("does not accept an unauthenticated request trace parent", () =>
18 Effect.gen(function* () {
19 const spans: Array<Tracer.Span> = [];
20 const productTracer = Tracer.make({
21 span: (options) => {
22 const span = new Tracer.NativeSpan(options);
23 spans.push(span);
24 return span;
25 },
26 });
27 const request = HttpServerRequest.fromWeb(
28 new Request("https://environment.example.test/api/t3-cloud/mint-credential", {
29 headers: {
30 traceparent: "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01",
31 },
32 }),
33 );
34
35 yield* traceRelayRequest(Effect.void.pipe(Effect.withSpan("relay.mint.handler"))).pipe(
36 Effect.provideService(HttpServerRequest.HttpServerRequest, request),
37 Effect.provideService(RelayClientTracer, Option.some(productTracer)),
38 );
39
40 expect(spans).toHaveLength(1);
41 const span = spans[0]!;
42 expect(span.traceId).not.toBe("0123456789abcdef0123456789abcdef");
43 expect(Option.isNone(span.parent)).toBe(true);
44 }),
45 );
46
47 it.effect("continues an authenticated relay trace with the product tracer", () =>
48 Effect.gen(function* () {
49 const spans: Array<Tracer.Span> = [];
50 const productTracer = Tracer.make({
51 span: (options) => {
52 const span = new Tracer.NativeSpan(options);
53 spans.push(span);
54 return span;
55 },
56 });
57 const request = HttpServerRequest.fromWeb(
58 new Request("https://environment.example.test/api/t3-cloud/mint-credential", {
59 headers: {
60 traceparent: "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01",
61 },
62 }),
63 );
64
65 yield* traceAuthenticatedRelayRequest(
66 Effect.void.pipe(Effect.withSpan("relay.mint.handler")),
67 AUTHENTICATION,
68 ).pipe(
69 Effect.provideService(HttpServerRequest.HttpServerRequest, request),
70 Effect.provideService(RelayClientTracer, Option.some(productTracer)),
71 );
72
73 expect(spans.map((span) => span.name)).toEqual([
74 "environment.relay.request",
75 "EnvironmentAuth.authenticateHttpRequest",
76 "relay.mint.handler",
77 ]);
78 const [relaySpan, authentication, handler] = spans;
79 expect(Option.getOrUndefined(authentication!.parent)?.spanId).toBe(relaySpan!.spanId);
80 expect(relaySpan!.traceId).toBe("0123456789abcdef0123456789abcdef");
81 expect(Option.getOrUndefined(relaySpan!.parent)?.spanId).toBe("0123456789abcdef");
82 expect(Option.getOrUndefined(handler!.parent)?.spanId).toBe(relaySpan!.spanId);
83 }),
84 );
85});
86
87describe("relay request tracing boundary", () => {
88 it.effect("exports a T3 Connect handler span but not its local work", () =>
89 Effect.gen(function* () {
90 const productSpans: Array<string> = [];
91 const localSpans: Array<string> = [];
92 const collect = (into: Array<string>) =>
93 Tracer.make({
94 span: (options) => {
95 into.push(options.name);
96 return new Tracer.NativeSpan(options);
97 },
98 });
99 const request = HttpServerRequest.fromWeb(
100 new Request("https://environment.example.test/api/orchestration/threads/thread-1"),
101 );
102
103 // Shaped like a real handler: an Effect.fn span whose service call runs
104 // on the local tracer.
105 const handler = Effect.fn("environment.orchestration.threadSnapshot")(function* () {
106 yield* Effect.void.pipe(
107 Effect.withSpan("sql.execute"),
108 Effect.withSpan("ServerSecretStore.get"),
109 traceLocalHandlerWork,
110 );
111 });
112
113 yield* traceAuthenticatedRelayRequest(handler(), AUTHENTICATION).pipe(
114 Effect.provideService(HttpServerRequest.HttpServerRequest, request),
115 Effect.provideService(RelayClientTracer, Option.some(collect(productSpans))),
116 Effect.withTracer(collect(localSpans)),
117 );
118
119 expect(productSpans).toEqual([
120 "environment.relay.request",
121 "EnvironmentAuth.authenticateHttpRequest",
122 "environment.orchestration.threadSnapshot",
123 ]);
124 expect(localSpans).toEqual(["ServerSecretStore.get", "sql.execute"]);
125 }),
126 );
127});