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

relayTracing.test.ts

106 lines3.6 KB

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/unstable/http";
7import { vi } from "vite-plus/test";
8​
9import {
10 makeRelayClientTracingLayer,
11 RelayClientTracer,
12 withRelayClientTracing,
13} from "./relayTracing.ts";
14​
15function collectingTracer(spans: Array<string>): Tracer.Tracer {
16 return Tracer.make({
17 span: (options) => {
18 const span = new Tracer.NativeSpan(options);
19 const end = span.end.bind(span);
20 span.end = (endTime, exit) => {
21 end(endTime, exit);
22 spans.push(span.name);
23 };
24 return span;
25 },
26 });
28​
29describe("withRelayClientTracing", () => {
30 it.effect("uses the product tracer only for relay operations", () =>
31 Effect.gen(function* () {
32 const userSpans: Array<string> = [];
33 const productSpans: Array<string> = [];
34 const userTracer = collectingTracer(userSpans);
35 const productTracer = collectingTracer(productSpans);
36​
37 yield* Effect.void.pipe(Effect.withSpan("user.operation"), Effect.withTracer(userTracer));
38 yield* Effect.void.pipe(
39 Effect.withSpan("relay.operation"),
40 withRelayClientTracing,
41 Effect.provideService(RelayClientTracer, Option.some(productTracer)),
42 Effect.withTracer(userTracer),
43 );
44​
45 expect(userSpans).toEqual(["user.operation"]);
46 expect(productSpans).toEqual(["relay.operation"]);
47 }),
48 );
49​
50 it.effect("preserves the active tracer when product tracing is disabled", () =>
51 Effect.gen(function* () {
52 const userSpans: Array<string> = [];
53 const userTracer = collectingTracer(userSpans);
54​
55 yield* Effect.void.pipe(
56 Effect.withSpan("relay.operation"),
57 withRelayClientTracing,
58 Effect.withTracer(userTracer),
59 );
60​
61 expect(userSpans).toEqual(["relay.operation"]);
62 }),
63 );
64​
65 it.effect("preserves nested error causes in exported relay spans", () => {
66 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
67 const httpClientLayer = FetchHttpClient.layer.pipe(
68 Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetchFn)),
69 );
70 const tracingLayer = makeRelayClientTracingLayer(
71 {
72 tracesUrl: "https://api.axiom.test/v1/traces",
73 tracesDataset: "relay-traces",
74 tracesToken: "public-ingest-token",
75 },
76 {
77 serviceName: "relay-test",
78 runtime: "test",
79 client: "test",
80 },
81 ).pipe(Layer.provide(httpClientLayer));
82 const rootCause = new Error("relay socket closed");
83 const failure = new Error("relay request failed", { cause: rootCause });
84 const tracedApplication = Layer.effectDiscard(
85 Effect.fail(failure).pipe(
86 Effect.withSpan("relay.failed-operation"),
87 withRelayClientTracing,
88 Effect.exit,
89 ),
90 ).pipe(Layer.provide(tracingLayer));
91​
92 return Layer.build(tracedApplication).pipe(
93 Effect.scoped,
94 Effect.andThen(
95 Effect.sync(() => {
96 expect(fetchFn).toHaveBeenCalledOnce();
97 const payload = new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array);
98 expect(payload).toContain("relay request failed");
99 expect(payload).toContain("relay socket closed");
100 expect(payload).toContain('"key":"service.name","value":{"stringValue":"relay-test"}');
101 expect(payload).toContain('"key":"service.namespace","value":{"stringValue":"t3code"}');
102 }),
103 ),
104 );
105 });
106});