|
1 | 1 | import { describe, expect, it } from "@effect/vitest"; |
2 | 2 | import * as Effect from "effect/Effect"; |
| 3 | +import * as Layer from "effect/Layer"; |
3 | 4 | import * as Option from "effect/Option"; |
4 | 5 | import * as Tracer from "effect/Tracer"; |
| 6 | +import { FetchHttpClient } from "effect/unstable/http"; |
| 7 | +import { vi } from "vite-plus/test"; |
5 | 8 |
|
6 | | -import { RelayClientTracer, withRelayClientTracing } from "./relayTracing.ts"; |
| 9 | +import { |
| 10 | + makeRelayClientTracingLayer, |
| 11 | + RelayClientTracer, |
| 12 | + withRelayClientTracing, |
| 13 | +} from "./relayTracing.ts"; |
7 | 14 |
|
8 | 15 | function collectingTracer(spans: Array<string>): Tracer.Tracer { |
9 | 16 | return Tracer.make({ |
@@ -54,4 +61,44 @@ describe("withRelayClientTracing", () => { |
54 | 61 | expect(userSpans).toEqual(["relay.operation"]); |
55 | 62 | }), |
56 | 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 | + }), |
| 101 | + ), |
| 102 | + ); |
| 103 | + }); |
57 | 104 | }); |
0 commit comments