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

Metrics.test.ts

157 lines4.4 KB

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