|
1 | 1 | import { assert, describe, it } from "@effect/vitest"; |
2 | | -import type { ResourceTelemetryProcess } from "@t3tools/contracts"; |
| 2 | +import type { ResourceTelemetryProcess, ResourceTelemetrySnapshot } from "@t3tools/contracts"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as Logger from "effect/Logger"; |
| 6 | +import * as References from "effect/References"; |
| 7 | +import * as Ref from "effect/Ref"; |
| 8 | +import * as TestClock from "effect/testing/TestClock"; |
3 | 9 |
|
4 | | -import { groupProcesses } from "./ProcessGroupMetrics.ts"; |
| 10 | +import * as ServerConfig from "../config.ts"; |
| 11 | +import * as ProcessGroupMetrics from "./ProcessGroupMetrics.ts"; |
| 12 | +import * as ResourceTelemetry from "./ResourceTelemetry.ts"; |
| 13 | + |
| 14 | +const { groupProcesses } = ProcessGroupMetrics; |
5 | 15 |
|
6 | 16 | const process = ( |
7 | 17 | pid: number, |
@@ -57,3 +67,54 @@ describe("groupProcesses", () => { |
57 | 67 | }); |
58 | 68 | }); |
59 | 69 | }); |
| 70 | + |
| 71 | +describe("ProcessGroupMetrics.layer", () => { |
| 72 | + it.effect("logs the first failed sample and the recovery, not every repeat", () => { |
| 73 | + const logs: Array<string> = []; |
| 74 | + const logger = Logger.make(({ logLevel, message }) => { |
| 75 | + logs.push(`${logLevel.toUpperCase()} ${String([message].flat()[0])}`); |
| 76 | + }); |
| 77 | + return Effect.gen(function* () { |
| 78 | + const outcomes = yield* Ref.make<ReadonlyArray<"fail" | "ok">>(["fail", "fail", "ok"]); |
| 79 | + const refresh = Ref.modify(outcomes, ([next = "ok", ...rest]) => [next, rest] as const).pipe( |
| 80 | + Effect.flatMap((next) => |
| 81 | + next === "fail" |
| 82 | + ? Effect.fail( |
| 83 | + new ResourceTelemetry.ResourceTelemetryRefreshFailed({ |
| 84 | + operation: "refresh", |
| 85 | + cause: new Error("Resource monitor binary was not found for darwin/arm64."), |
| 86 | + }), |
| 87 | + ) |
| 88 | + : Effect.succeed({ processes: [] } as unknown as ResourceTelemetrySnapshot), |
| 89 | + ), |
| 90 | + ); |
| 91 | + const telemetry = { refresh } as unknown as ResourceTelemetry.ResourceTelemetry["Service"]; |
| 92 | + |
| 93 | + yield* Layer.build( |
| 94 | + ProcessGroupMetrics.layer.pipe( |
| 95 | + Layer.provide(Layer.succeed(ResourceTelemetry.ResourceTelemetry, telemetry)), |
| 96 | + Layer.provide( |
| 97 | + ServerConfig.layer({ |
| 98 | + otlpMetricsUrl: "http://localhost:4318/v1/metrics", |
| 99 | + } as ServerConfig.ServerConfig["Service"]), |
| 100 | + ), |
| 101 | + ), |
| 102 | + ); |
| 103 | + yield* TestClock.adjust("3 minutes"); |
| 104 | + |
| 105 | + assert.deepStrictEqual(logs, [ |
| 106 | + "WARN process group metrics are not exported: sample failed", |
| 107 | + "DEBUG process group metrics sample failed", |
| 108 | + "INFO process group metrics sample recovered", |
| 109 | + ]); |
| 110 | + }).pipe( |
| 111 | + Effect.scoped, |
| 112 | + Effect.provide( |
| 113 | + Layer.mergeAll( |
| 114 | + Logger.layer([logger], { mergeWithExisting: false }), |
| 115 | + Layer.succeed(References.MinimumLogLevel, "Debug"), |
| 116 | + ), |
| 117 | + ), |
| 118 | + ); |
| 119 | + }); |
| 120 | +}); |
0 commit comments