58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { describe, expect, it } from "@effect/vitest";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Option from "effect/Option";
|
|
import * as Tracer from "effect/Tracer";
|
|
|
|
import { RelayClientTracer, withRelayClientTracing } from "./relayTracing.ts";
|
|
|
|
function collectingTracer(spans: Array<string>): Tracer.Tracer {
|
|
return Tracer.make({
|
|
span: (options) => {
|
|
const span = new Tracer.NativeSpan(options);
|
|
const end = span.end.bind(span);
|
|
span.end = (endTime, exit) => {
|
|
end(endTime, exit);
|
|
spans.push(span.name);
|
|
};
|
|
return span;
|
|
},
|
|
});
|
|
}
|
|
|
|
describe("withRelayClientTracing", () => {
|
|
it.effect("uses the product tracer only for relay operations", () =>
|
|
Effect.gen(function* () {
|
|
const userSpans: Array<string> = [];
|
|
const productSpans: Array<string> = [];
|
|
const userTracer = collectingTracer(userSpans);
|
|
const productTracer = collectingTracer(productSpans);
|
|
|
|
yield* Effect.void.pipe(Effect.withSpan("user.operation"), Effect.withTracer(userTracer));
|
|
yield* Effect.void.pipe(
|
|
Effect.withSpan("relay.operation"),
|
|
withRelayClientTracing,
|
|
Effect.provideService(RelayClientTracer, Option.some(productTracer)),
|
|
Effect.withTracer(userTracer),
|
|
);
|
|
|
|
expect(userSpans).toEqual(["user.operation"]);
|
|
expect(productSpans).toEqual(["relay.operation"]);
|
|
}),
|
|
);
|
|
|
|
it.effect("preserves the active tracer when product tracing is disabled", () =>
|
|
Effect.gen(function* () {
|
|
const userSpans: Array<string> = [];
|
|
const userTracer = collectingTracer(userSpans);
|
|
|
|
yield* Effect.void.pipe(
|
|
Effect.withSpan("relay.operation"),
|
|
withRelayClientTracing,
|
|
Effect.withTracer(userTracer),
|
|
);
|
|
|
|
expect(userSpans).toEqual(["relay.operation"]);
|
|
}),
|
|
);
|
|
});
|