Open History to see the last commit
1import {
2 HostProcessArchitecture,
3 HostProcessEnvironment,
4 HostProcessPlatform,
5} from "@t3tools/shared/hostProcess";
6import * as Context from "effect/Context";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Layer from "effect/Layer";
10import * as Option from "effect/Option";
11import * as Path from "effect/Path";
12import * as Schema from "effect/Schema";
13
14import { ServerConfig } from "../config.ts";
15
16export class ResourceMonitorBinaryUnsupported extends Schema.TaggedErrorClass<ResourceMonitorBinaryUnsupported>()(
17 "ResourceMonitorBinaryUnsupported",
18 {
19 platform: Schema.String,
20 architecture: Schema.String,
21 },
22) {
23 override get message(): string {
24 return `Resource monitoring is unsupported on ${this.platform}/${this.architecture}.`;
25 }
26}
27
28export class ResourceMonitorBinaryNotFound extends Schema.TaggedErrorClass<ResourceMonitorBinaryNotFound>()(
29 "ResourceMonitorBinaryNotFound",
30 {
31 platform: Schema.String,
32 architecture: Schema.String,
33 candidates: Schema.Array(Schema.String),
34 },
35) {
36 override get message(): string {
37 return `Resource monitor binary was not found for ${this.platform}/${this.architecture}.`;
38 }
39}
40
41export class ResourceMonitorBinaryNotExecutable extends Schema.TaggedErrorClass<ResourceMonitorBinaryNotExecutable>()(
42 "ResourceMonitorBinaryNotExecutable",
43 {
44 path: Schema.String,
45 mode: Schema.Number,
46 },
47) {
48 override get message(): string {
49 return `Resource monitor binary at '${this.path}' is not executable.`;
50 }
51}
52
53export type ResourceMonitorBinaryError =
54 | ResourceMonitorBinaryUnsupported
55 | ResourceMonitorBinaryNotFound
56 | ResourceMonitorBinaryNotExecutable;
57
58export class ResourceMonitorBinary extends Context.Service<
59 ResourceMonitorBinary,
60 {
61 readonly resolve: Effect.Effect<string, ResourceMonitorBinaryError>;
62 }
63>()("t3/resourceTelemetry/ResourceMonitorBinary") {}
64
65function binaryName(platform: NodeJS.Platform): string {
66 return platform === "win32" ? "t3-resource-monitor.exe" : "t3-resource-monitor";
67}
68
69export type ResourceMonitorLinuxLibc = "gnu" | "musl";
70
71function detectResourceMonitorLinuxLibc(): ResourceMonitorLinuxLibc {
72 try {
73 const report = process.report?.getReport() as
74 | {
75 readonly header?: {
76 readonly glibcVersionRuntime?: unknown;
77 };
78 }
79 | undefined;
80 return typeof report?.header?.glibcVersionRuntime === "string" ? "gnu" : "musl";
81 } catch {
82 return "musl";
83 }
84}
85
86export const ResourceMonitorHostLinuxLibc = Context.Reference<ResourceMonitorLinuxLibc>(
87 "t3/resourceTelemetry/ResourceMonitorHostLinuxLibc",
88 {
89 defaultValue: detectResourceMonitorLinuxLibc,
90 },
91);
92
93export function resourceMonitorPlatformKey(
94 platform: NodeJS.Platform,
95 architecture: NodeJS.Architecture,
96): string | undefined {
97 if (
98 (platform !== "darwin" && platform !== "linux" && platform !== "win32") ||
99 (architecture !== "arm64" && architecture !== "x64")
100 ) {
101 return undefined;
102 }
103 return `${platform}-${architecture}`;
104}
105
106export function resourceMonitorRustTarget(
107 platform: NodeJS.Platform,
108 architecture: NodeJS.Architecture,
109 linuxLibc?: ResourceMonitorLinuxLibc,
110): string | undefined {
111 if (platform === "darwin") {
112 return architecture === "arm64"
113 ? "aarch64-apple-darwin"
114 : architecture === "x64"
115 ? "x86_64-apple-darwin"
116 : undefined;
117 }
118 if (platform === "linux") {
119 if (linuxLibc !== "gnu") {
120 return undefined;
121 }
122 return architecture === "arm64"
123 ? "aarch64-unknown-linux-gnu"
124 : architecture === "x64"
125 ? "x86_64-unknown-linux-gnu"
126 : undefined;
127 }
128 if (platform === "win32") {
129 return architecture === "arm64"
130 ? "aarch64-pc-windows-msvc"
131 : architecture === "x64"
132 ? "x86_64-pc-windows-msvc"
133 : undefined;
134 }
135 return undefined;
136}
137
138export const make = Effect.fn("resourceTelemetry.resourceMonitorBinary.make")(function* () {
139 const config = yield* ServerConfig;
140 const fileSystem = yield* FileSystem.FileSystem;
141 const path = yield* Path.Path;
142 const platform = yield* HostProcessPlatform;
143 const architecture = yield* HostProcessArchitecture;
144 const environment = yield* HostProcessEnvironment;
145 const linuxLibc = platform === "linux" ? yield* ResourceMonitorHostLinuxLibc : undefined;
146 const executableName = binaryName(platform);
147 const platformKey = resourceMonitorPlatformKey(platform, architecture);
148 const rustTarget = resourceMonitorRustTarget(platform, architecture, linuxLibc);
149 const overrideCandidates = [
150 environment.T3CODE_RESOURCE_MONITOR_PATH,
151 config.resourceMonitorPath,
152 ].filter((candidate): candidate is string => Boolean(candidate));
153 const bundledCandidates =
154 platformKey === undefined || rustTarget === undefined
155 ? []
156 : [
157 path.resolve(import.meta.dirname, "resource-monitor", platformKey, executableName),
158 path.resolve(import.meta.dirname, "resource-monitor", executableName),
159 path.resolve(import.meta.dirname, "../resource-monitor", executableName),
160 path.resolve(
161 import.meta.dirname,
162 "../../../../native/resource-monitor/target",
163 rustTarget,
164 "release",
165 executableName,
166 ),
167 path.resolve(
168 import.meta.dirname,
169 "../../../native/resource-monitor/target",
170 rustTarget,
171 "release",
172 executableName,
173 ),
174 path.resolve(
175 import.meta.dirname,
176 "../../../../native/resource-monitor/target/release",
177 executableName,
178 ),
179 path.resolve(
180 import.meta.dirname,
181 "../../../../native/resource-monitor/target/debug",
182 executableName,
183 ),
184 ];
185 if (overrideCandidates.length === 0 && bundledCandidates.length === 0) {
186 return ResourceMonitorBinary.of({
187 resolve: Effect.fail(
188 new ResourceMonitorBinaryUnsupported({
189 platform,
190 architecture,
191 }),
192 ),
193 });
194 }
195
196 const candidates = [...overrideCandidates, ...bundledCandidates];
197
198 const resolve: ResourceMonitorBinary["Service"]["resolve"] = Effect.gen(function* () {
199 for (const candidate of candidates) {
200 const exists = yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false));
201 if (!exists) continue;
202
203 if (platform !== "win32") {
204 const stat = yield* fileSystem.stat(candidate).pipe(Effect.option);
205 if (Option.isSome(stat) && (stat.value.mode & 0o111) === 0) {
206 return yield* new ResourceMonitorBinaryNotExecutable({
207 path: candidate,
208 mode: stat.value.mode,
209 });
210 }
211 }
212
213 return candidate;
214 }
215
216 return yield* new ResourceMonitorBinaryNotFound({
217 platform,
218 architecture,
219 candidates,
220 });
221 });
222
223 return ResourceMonitorBinary.of({ resolve });
224});
225
226export const layer = Layer.effect(ResourceMonitorBinary, make());