Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import { ProviderDriverKind } from "@t3tools/contracts";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Metric from "effect/Metric";
7import * as TestClock from "effect/testing/TestClock";
8
9import { withMetrics } from "./Metrics.ts";
10
11const hasMetricSnapshot = (
12 snapshots: ReadonlyArray<Metric.Metric.Snapshot>,
13 id: string,
14 attributes: Readonly<Record<string, string>>,
15) =>
16 snapshots.some(
17 (snapshot) =>
18 snapshot.id === id &&
19 Object.entries(attributes).every(([key, value]) => snapshot.attributes?.[key] === value),
20 );
21
22const findHistogramSnapshot = (
23 snapshots: ReadonlyArray<Metric.Metric.Snapshot>,
24 id: string,
25 attributes: Readonly<Record<string, string>>,
26) =>
27 snapshots.find(
28 (snapshot): snapshot is Extract<Metric.Metric.Snapshot, { readonly type: "Histogram" }> =>
29 snapshot.type === "Histogram" &&
30 snapshot.id === id &&
31 Object.entries(attributes).every(([key, value]) => snapshot.attributes?.[key] === value),
32 );
33
34describe("withMetrics", () => {
35 it.effect("supports pipe-style usage", () =>
36 Effect.gen(function* () {
37 const counter = Metric.counter("with_metrics_pipe_total");
38 const timer = Metric.timer("with_metrics_pipe_duration");
39
40 const result = yield* Effect.succeed("ok").pipe(
41 withMetrics({
42 counter,
43 timer,
44 attributes: {
45 operation: "pipe",
46 },
47 }),
48 );
49
50 assert.equal(result, "ok");
51
52 const snapshots = yield* Metric.snapshot;
53 assert.equal(
54 hasMetricSnapshot(snapshots, "with_metrics_pipe_total", {
55 operation: "pipe",
56 outcome: "success",
57 }),
58 true,
59 );
60 assert.equal(
61 hasMetricSnapshot(snapshots, "with_metrics_pipe_duration", {
62 operation: "pipe",
63 }),
64 true,
65 );
66 }),
67 );
68
69 it.effect("supports direct invocation", () =>
70 Effect.gen(function* () {
71 const counter = Metric.counter("with_metrics_direct_total");
72
73 yield* withMetrics(Effect.fail("boom"), {
74 counter,
75 attributes: {
76 operation: "direct",
77 },
78 }).pipe(Effect.exit);
79
80 const snapshots = yield* Metric.snapshot;
81 assert.equal(
82 hasMetricSnapshot(snapshots, "with_metrics_direct_total", {
83 operation: "direct",
84 outcome: "failure",
85 }),
86 true,
87 );
88 }),
89 );
90
91 it.effect("evaluates attributes lazily after the wrapped effect runs", () =>
92 Effect.gen(function* () {
93 const counter = Metric.counter("with_metrics_lazy_total");
94 const timer = Metric.timer("with_metrics_lazy_duration");
95 let provider = ProviderDriverKind.make("unknown");
96 const lazyInittedProvider = ProviderDriverKind.make("codex");
97
98 yield* Effect.sync(() => {
99 provider = lazyInittedProvider;
100 }).pipe(
101 withMetrics({
102 counter,
103 timer,
104 attributes: () => ({
105 provider,
106 operation: "lazy",
107 }),
108 }),
109 );
110
111 const snapshots = yield* Metric.snapshot;
112 assert.equal(
113 hasMetricSnapshot(snapshots, "with_metrics_lazy_total", {
114 provider: lazyInittedProvider,
115 operation: "lazy",
116 outcome: "success",
117 }),
118 true,
119 );
120 assert.equal(
121 hasMetricSnapshot(snapshots, "with_metrics_lazy_duration", {
122 provider: lazyInittedProvider,
123 operation: "lazy",
124 }),
125 true,
126 );
127 }),
128 );
129
130 it.effect("records timer durations from nanosecond clock readings", () =>
131 Effect.gen(function* () {
132 const duration = Duration.nanos(1_500_000n);
133 const timer = Metric.timer("with_metrics_nanos_duration");
134
135 yield* Effect.gen(function* () {
136 const fiber = yield* Effect.sleep(duration).pipe(
137 withMetrics({
138 timer,
139 attributes: {
140 operation: "nanos",
141 },
142 }),
143 Effect.forkChild,
144 );
145
146 yield* Effect.yieldNow;
147 yield* TestClock.adjust(duration);
148 yield* Fiber.join(fiber);
149 }).pipe(Effect.provide(TestClock.layer()));
150
151 const snapshots = yield* Metric.snapshot;
152 const snapshot = findHistogramSnapshot(snapshots, "with_metrics_nanos_duration", {
153 operation: "nanos",
154 });
155
156 assert.equal(snapshot?.state.count, 1);
157 assert.equal(snapshot?.state.sum, 1.5);
158 }),
159 );
160});