Repository navigation
Expand file tree
/
Copy pathNativeTelemetryClient.ts
More file actions
1127 lines (1067 loc) · 38 KB
/
Copy pathNativeTelemetryClient.ts
File metadata and controls
1127 lines (1067 loc) · 38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type {
HostPowerSnapshot,
ResourceMonitorCapabilities,
ResourceMonitorCommand,
ResourceMonitorEvent,
ResourceMonitorExternalProcess,
ResourceMonitorHelloEvent,
ResourceMonitorProcessTableEntry,
ResourceMonitorSnapshotEvent,
ResourceTelemetrySourceStatus,
} from "@t3tools/contracts";
import {
RESOURCE_MONITOR_PROTOCOL_VERSION,
ResourceMonitorCommand as ResourceMonitorCommandSchema,
ResourceMonitorEvent as ResourceMonitorEventSchema,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as DateTime from "effect/DateTime";
import * as Deferred from "effect/Deferred";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as PubSub from "effect/PubSub";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as Result from "effect/Result";
import * as Schema from "effect/Schema";
import * as Semaphore from "effect/Semaphore";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import * as Ndjson from "effect/encoding/Ndjson";
import { ChildProcess, ChildProcessSpawner } from "effect/process";
import * as ResourceMonitorBinary from "./ResourceMonitorBinary.ts";
import * as ServerConfig from "../config.ts";
import { subscribeBeforeSnapshotWithoutMutex } from "../utils/subscribeBeforeSnapshot.ts";
const SAMPLE_INTERVAL_MS = 1_000;
// The sidecar treats 0 as "sample only on request".
const IDLE_SAMPLE_INTERVAL_MS = 0;
const BATTERY_SAMPLE_INTERVAL_MS = 5_000;
const CONSTRAINED_SAMPLE_INTERVAL_MS = 15_000;
const HANDSHAKE_TIMEOUT = Duration.seconds(5);
const SAMPLE_REQUEST_TIMEOUT = Duration.seconds(5);
const PROCESS_TABLE_REQUEST_TIMEOUT = Duration.seconds(5);
const HISTORY_REQUEST_TIMEOUT = Duration.seconds(15);
const INITIAL_RESTART_DELAY = Duration.millis(500);
const MAX_RESTART_DELAY = Duration.seconds(10);
const FAILURE_WINDOW_MS = 60_000;
const MAX_FAILURES_PER_WINDOW = 5;
export class NativeTelemetrySpawnFailed extends Schema.TaggedError<NativeTelemetrySpawnFailed>()(
"NativeTelemetrySpawnFailed",
{
path: Schema.String,
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Failed to start resource monitor '${this.path}'.`;
}
}
export class NativeTelemetryHandshakeTimedOut extends Schema.TaggedError<NativeTelemetryHandshakeTimedOut>()(
"NativeTelemetryHandshakeTimedOut",
{
timeoutMs: Schema.Number,
},
) {
override get message(): string {
return `Resource monitor handshake timed out after ${this.timeoutMs}ms.`;
}
}
class NativeTelemetryRequestTimedOut extends Schema.TaggedError<NativeTelemetryRequestTimedOut>()(
"NativeTelemetryRequestTimedOut",
{
operation: Schema.Literals(["processTable", "readHistory", "sampleNow"]),
timeoutMs: Schema.Number,
},
) {
override get message(): string {
return `Resource monitor '${this.operation}' request timed out after ${this.timeoutMs}ms.`;
}
}
export class NativeTelemetryProtocolMismatch extends Schema.TaggedError<NativeTelemetryProtocolMismatch>()(
"NativeTelemetryProtocolMismatch",
{
expectedVersion: Schema.Number,
receivedVersion: Schema.Number,
},
) {
override get message(): string {
return `Resource monitor protocol ${this.receivedVersion} is incompatible with expected protocol ${this.expectedVersion}.`;
}
}
export class NativeTelemetryDecodeFailed extends Schema.TaggedError<NativeTelemetryDecodeFailed>()(
"NativeTelemetryDecodeFailed",
{
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Failed to decode resource monitor output.";
}
}
export class NativeTelemetryCommandFailed extends Schema.TaggedError<NativeTelemetryCommandFailed>()(
"NativeTelemetryCommandFailed",
{
operation: Schema.String,
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Resource monitor command '${this.operation}' failed.`;
}
}
export class NativeTelemetryExited extends Schema.TaggedError<NativeTelemetryExited>()(
"NativeTelemetryExited",
{
exitCode: Schema.Number,
},
) {
override get message(): string {
return `Resource monitor exited with code ${this.exitCode}.`;
}
}
class NativeTelemetryStreamClosed extends Schema.TaggedError<NativeTelemetryStreamClosed>()(
"NativeTelemetryStreamClosed",
{},
) {
override get message(): string {
return "Resource monitor event stream closed unexpectedly.";
}
}
export class NativeTelemetryUnavailable extends Schema.TaggedError<NativeTelemetryUnavailable>()(
"NativeTelemetryUnavailable",
{
reason: Schema.String,
},
) {
override get message(): string {
return `Resource monitor is unavailable: ${this.reason}`;
}
}
export type NativeTelemetryClientError =
| ResourceMonitorBinary.ResourceMonitorBinaryError
| NativeTelemetrySpawnFailed
| NativeTelemetryHandshakeTimedOut
| NativeTelemetryRequestTimedOut
| NativeTelemetryProtocolMismatch
| NativeTelemetryDecodeFailed
| NativeTelemetryCommandFailed
| NativeTelemetryExited
| NativeTelemetryStreamClosed
| NativeTelemetryUnavailable;
export interface NativeTelemetryClientHealth {
readonly status: ResourceTelemetrySourceStatus;
readonly hello: Option.Option<ResourceMonitorHelloEvent>;
readonly lastSampleAt: Option.Option<DateTime.Utc>;
readonly lastError: Option.Option<string>;
readonly restartCount: number;
readonly sampleIntervalMs: number;
}
export interface NativeTelemetrySnapshot {
readonly generation: number;
readonly snapshot: ResourceMonitorSnapshotEvent;
}
export class NativeTelemetryClient extends Context.Service<
NativeTelemetryClient,
{
readonly capabilities: Effect.Effect<ResourceMonitorCapabilities, NativeTelemetryClientError>;
readonly snapshots: Stream.Stream<NativeTelemetrySnapshot, NativeTelemetryClientError>;
readonly readHistory: (
windowMs: number,
) => Effect.Effect<ReadonlyArray<ResourceMonitorSnapshotEvent>, NativeTelemetryClientError>;
readonly setExternalProcesses: (
processes: ReadonlyArray<ResourceMonitorExternalProcess>,
) => Effect.Effect<void, NativeTelemetryClientError>;
readonly setHostPowerState: (
snapshot: HostPowerSnapshot,
) => Effect.Effect<void, NativeTelemetryClientError>;
readonly sampleNow: Effect.Effect<NativeTelemetrySnapshot, NativeTelemetryClientError>;
readonly processTable: Effect.Effect<
ReadonlyArray<ResourceMonitorProcessTableEntry>,
NativeTelemetryClientError
>;
readonly retry: Effect.Effect<boolean>;
readonly health: Effect.Effect<NativeTelemetryClientHealth>;
readonly subscribeHealth: Effect.Effect<
{
readonly latest: NativeTelemetryClientHealth;
readonly changes: Stream.Stream<NativeTelemetryClientHealth>;
},
never,
Scope.Scope
>;
}
>()("t3/resourceTelemetry/NativeTelemetryClient") {}
interface ClientState {
readonly status: ResourceTelemetrySourceStatus;
readonly handle: Option.Option<ChildProcessSpawner.ChildProcessHandle>;
readonly hello: Option.Option<ResourceMonitorHelloEvent>;
readonly lastSampleAt: Option.Option<DateTime.Utc>;
readonly lastError: Option.Option<string>;
readonly restartCount: number;
}
export interface CollectionControl {
readonly hostPower: HostPowerSnapshot;
readonly liveSubscriberCount: number;
readonly sampleIntervalMs: number;
}
interface PendingHistoryRequest {
readonly deferred: Deferred.Deferred<
ReadonlyArray<ResourceMonitorSnapshotEvent>,
NativeTelemetryClientError
>;
readonly snapshots: ReadonlyArray<ResourceMonitorSnapshotEvent>;
}
const initialState: ClientState = {
status: "starting",
handle: Option.none(),
hello: Option.none(),
lastSampleAt: Option.none(),
lastError: Option.none(),
restartCount: 0,
};
function toHealth(state: ClientState, sampleIntervalMs: number): NativeTelemetryClientHealth {
return {
status: state.status,
hello: state.hello,
lastSampleAt: state.lastSampleAt,
lastError: state.lastError,
restartCount: state.restartCount,
sampleIntervalMs,
};
}
function isThermallyConstrained(snapshot: HostPowerSnapshot): boolean {
return snapshot.thermalState === "serious" || snapshot.thermalState === "critical";
}
// Without a live subscriber the sidecar does not sample on a timer, so an idle server
// never walks the process table. History then covers only time with a subscriber.
function resolveNativeSampleIntervalMs(
snapshot: HostPowerSnapshot,
liveSubscriberCount: number,
): number {
if (liveSubscriberCount === 0) return IDLE_SAMPLE_INTERVAL_MS;
if (snapshot.stale || snapshot.source === "unknown") return SAMPLE_INTERVAL_MS;
if (
snapshot.suspended ||
snapshot.locked === "true" ||
snapshot.lowPowerMode === "true" ||
isThermallyConstrained(snapshot)
) {
return CONSTRAINED_SAMPLE_INTERVAL_MS;
}
if (snapshot.onBattery === "true") return BATTERY_SAMPLE_INTERVAL_MS;
return SAMPLE_INTERVAL_MS;
}
// Desktop power samples also commit control state; avoid a span for each sample.
export const commitCollectionControlUpdate = Effect.fnUntraced(function* <E, R>(
desiredState: Ref.Ref<CollectionControl>,
appliedState: Ref.Ref<CollectionControl>,
update: (current: CollectionControl) => CollectionControl,
apply: (previous: CollectionControl, next: CollectionControl) => Effect.Effect<void, E, R>,
): Effect.fn.Return<readonly [CollectionControl, CollectionControl], E, R> {
const [previousDesired, next] = yield* Ref.modify(desiredState, (previous) => {
const next = update(previous);
return [[previous, next] as const, next];
});
const previousApplied = yield* Ref.get(appliedState);
yield* apply(previousApplied, next);
yield* Ref.set(appliedState, next);
return [previousDesired, next] as const;
});
export function synchronizeCollectionControlOnStart<E1, R1, E2, R2>(
mutex: Semaphore.Semaphore,
desiredState: Ref.Ref<CollectionControl>,
appliedState: Ref.Ref<CollectionControl>,
apply: (control: CollectionControl) => Effect.Effect<void, E1, R1>,
markReady: Effect.Effect<void, E2, R2>,
) {
return mutex.withPermits(1)(
Effect.gen(function* () {
const control = yield* Ref.get(desiredState);
yield* apply(control);
yield* Ref.set(appliedState, control);
yield* markReady;
return control;
}),
);
}
const decodeMonitorEvent: (
value: unknown,
) => Effect.Effect<ResourceMonitorEvent, Schema.SchemaError> = Schema.decodeUnknownEffect(
ResourceMonitorEventSchema,
);
const encodeMonitorCommand = Schema.encodeEffect(
Schema.fromJsonString(ResourceMonitorCommandSchema),
);
const isProtocolMismatch = Schema.is(NativeTelemetryProtocolMismatch);
const isDecodeFailed = Schema.is(NativeTelemetryDecodeFailed);
const isCommandFailed = Schema.is(NativeTelemetryCommandFailed);
export function eventVersion(value: unknown): number | undefined {
if (typeof value !== "object" || value === null) return undefined;
const version = Reflect.get(value, "version");
return typeof version === "number" ? version : undefined;
}
function restartDelay(attempt: number): Duration.Duration {
return Duration.min(Duration.times(INITIAL_RESTART_DELAY, 2 ** attempt), MAX_RESTART_DELAY);
}
function retainRecentNativeTelemetryFailures(
failures: ReadonlyArray<number>,
now: number,
): ReadonlyArray<number> {
return failures.filter((failedAt) => now - failedAt <= FAILURE_WINDOW_MS);
}
function errorMessage(error: NativeTelemetryClientError): string {
return error.message;
}
function canRequestNativeTelemetryRetry(
status: ResourceTelemetrySourceStatus,
hasHandle: boolean,
): boolean {
return status !== "healthy" && status !== "starting" && !hasHandle;
}
function canCommandNativeTelemetrySidecar(
status: ResourceTelemetrySourceStatus,
hasHandle: boolean,
): boolean {
return hasHandle && (status === "healthy" || status === "degraded");
}
/** @public */
export const make = Effect.fn("resourceTelemetry.nativeTelemetryClient.make")(function* () {
const binary = yield* ResourceMonitorBinary.ResourceMonitorBinary;
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const random = yield* SecureRandom.SecureRandom;
const config = yield* ServerConfig.ServerConfig;
const initializedAt = yield* DateTime.now;
const state = yield* Ref.make(initialState);
const collectionControl = yield* Ref.make<CollectionControl>({
hostPower: {
source: "unknown",
idle: "unknown",
idleSeconds: null,
locked: "unknown",
suspended: false,
onBattery: "unknown",
lowPowerMode: "unknown",
thermalState: "unknown",
stale: true,
updatedAt: initializedAt,
},
liveSubscriberCount: 0,
sampleIntervalMs: IDLE_SAMPLE_INTERVAL_MS,
});
const appliedCollectionControl = yield* Ref.make(yield* Ref.get(collectionControl));
const externalProcesses = yield* Ref.make<ReadonlyArray<ResourceMonitorExternalProcess>>([]);
const pendingSamples = yield* Ref.make(
new Map<string, Deferred.Deferred<NativeTelemetrySnapshot, NativeTelemetryClientError>>(),
);
const pendingProcessTables = yield* Ref.make(
new Map<
string,
Deferred.Deferred<ReadonlyArray<ResourceMonitorProcessTableEntry>, NativeTelemetryClientError>
>(),
);
const pendingHistories = yield* Ref.make(new Map<string, PendingHistoryRequest>());
const snapshots = yield* PubSub.sliding<NativeTelemetrySnapshot>(8);
const healthChanges = yield* PubSub.sliding<NativeTelemetryClientHealth>(4);
const retryQueue = yield* Queue.sliding<void>(1);
const commandMutex = yield* Semaphore.make(1);
const controlMutex = yield* Semaphore.make(1);
const currentHealth = Effect.all([Ref.get(state), Ref.get(collectionControl)]).pipe(
Effect.map(([current, control]) => toHealth(current, control.sampleIntervalMs)),
);
const publishHealth = currentHealth.pipe(
Effect.flatMap((health) => PubSub.publish(healthChanges, health)),
Effect.asVoid,
);
const failPending = Effect.fn("NativeTelemetryClient.failPending")(function* (
error: NativeTelemetryClientError,
) {
const samples = yield* Ref.getAndSet(pendingSamples, new Map());
const processTables = yield* Ref.getAndSet(pendingProcessTables, new Map());
const histories = yield* Ref.getAndSet(pendingHistories, new Map());
yield* Effect.forEach(samples.values(), (deferred) => Deferred.fail(deferred, error), {
discard: true,
});
yield* Effect.forEach(processTables.values(), (deferred) => Deferred.fail(deferred, error), {
discard: true,
});
yield* Effect.forEach(histories.values(), (request) => Deferred.fail(request.deferred, error), {
discard: true,
});
});
const writeCommand = (
handle: ChildProcessSpawner.ChildProcessHandle,
command: ResourceMonitorCommand,
): Effect.Effect<void, NativeTelemetryClientError> =>
commandMutex.withPermits(1)(
encodeMonitorCommand(command).pipe(
Effect.map((encoded) => `${encoded}\n`),
Effect.mapError(
(cause) =>
new NativeTelemetryCommandFailed({
operation: command.type,
cause,
}),
),
Effect.flatMap((encoded) =>
Stream.run(Stream.encodeText(Stream.make(encoded)), handle.stdin),
),
Effect.mapError(
(cause) =>
new NativeTelemetryCommandFailed({
operation: command.type,
cause,
}),
),
),
);
const processEvent = (
event: ResourceMonitorEvent,
helloDeferred: Deferred.Deferred<ResourceMonitorHelloEvent>,
generation: number,
): Effect.Effect<void, NativeTelemetryClientError> => {
switch (event.type) {
case "hello":
return Ref.update(state, (current) => ({
...current,
status: "starting" as const,
hello: Option.some(event),
lastError: Option.none(),
})).pipe(
Effect.andThen(publishHealth),
Effect.andThen(Deferred.succeed(helloDeferred, event)),
Effect.asVoid,
);
case "snapshot":
return Effect.gen(function* () {
const nativeSnapshot = { generation, snapshot: event } satisfies NativeTelemetrySnapshot;
const sampledAt = DateTime.makeUnsafe(event.sampledAtUnixMs);
const healthChanged = yield* Ref.modify(state, (current) => [
current.status !== "healthy" || Option.isSome(current.lastError),
{
...current,
status: "healthy" as const,
lastSampleAt: Option.some(sampledAt),
lastError: Option.none(),
},
]);
if (healthChanged) yield* publishHealth;
yield* PubSub.publish(snapshots, nativeSnapshot);
if (event.requestId) {
const deferred = yield* Ref.modify(pendingSamples, (pending) => {
const next = new Map(pending);
const current = next.get(event.requestId!);
next.delete(event.requestId!);
return [Option.fromUndefinedOr(current), next];
});
if (Option.isSome(deferred)) {
yield* Deferred.succeed(deferred.value, nativeSnapshot);
}
}
});
case "processTable":
return Ref.modify(pendingProcessTables, (pending) => {
const next = new Map(pending);
const deferred = next.get(event.requestId);
next.delete(event.requestId);
return [Option.fromUndefinedOr(deferred), next] as const;
}).pipe(
Effect.flatMap(
Option.match({
onNone: () => Effect.void,
onSome: (deferred) => Deferred.succeed(deferred, event.processes),
}),
),
Effect.asVoid,
);
case "historyChunk":
return Effect.gen(function* () {
const latestSnapshot = event.snapshots.at(-1);
const healthChanged = yield* Ref.modify(state, (current) => [
current.status !== "healthy" || Option.isSome(current.lastError),
{
...current,
status: "healthy" as const,
lastSampleAt: latestSnapshot
? Option.some(DateTime.makeUnsafe(latestSnapshot.sampledAtUnixMs))
: current.lastSampleAt,
lastError: Option.none(),
},
]);
if (healthChanged) yield* publishHealth;
const completed = yield* Ref.modify(pendingHistories, (pending) => {
const request = pending.get(event.requestId);
if (!request) return [Option.none(), pending] as const;
const snapshots = [...request.snapshots, ...event.snapshots];
const next = new Map(pending);
if (event.done) {
next.delete(event.requestId);
return [Option.some({ deferred: request.deferred, snapshots }), next] as const;
}
next.set(event.requestId, { deferred: request.deferred, snapshots });
return [Option.none(), next] as const;
});
if (Option.isSome(completed)) {
yield* Deferred.succeed(completed.value.deferred, completed.value.snapshots);
}
});
case "error":
return Ref.update(state, (current) => ({
...current,
status: "degraded" as const,
lastError: Option.some(event.message),
})).pipe(
Effect.andThen(publishHealth),
Effect.andThen(
event.recoverable
? Effect.void
: Effect.fail(
new NativeTelemetryCommandFailed({
operation: event.code,
cause: event.message,
}),
),
),
);
}
};
const runAttempt: Effect.Effect<void, NativeTelemetryClientError> = Effect.scoped(
Effect.gen(function* () {
const executablePath = yield* binary.resolve;
const command = ChildProcess.make(executablePath, [], {
cwd: config.cwd,
stdin: {
stream: "pipe",
endOnDone: false,
},
stdout: "pipe",
stderr: "pipe",
killSignal: "SIGTERM",
forceKillAfter: Duration.seconds(2),
});
const handle = yield* Effect.acquireRelease(
spawner
.spawn(command)
.pipe(
Effect.mapError(
(cause) => new NativeTelemetrySpawnFailed({ path: executablePath, cause }),
),
),
(child) => child.kill().pipe(Effect.ignore),
);
yield* Ref.update(state, (current) => ({
...current,
status: "starting" as const,
handle: Option.some(handle),
hello: Option.none(),
}));
yield* publishHealth;
const generation = (yield* Ref.get(state)).restartCount;
const helloDeferred = yield* Deferred.make<ResourceMonitorHelloEvent>();
const eventFiber = yield* handle.stdout.pipe(
Stream.pipeThroughChannel(Ndjson.decode({ ignoreEmptyLines: true })),
Stream.mapEffect(
(
value,
): Effect.Effect<
ResourceMonitorEvent,
NativeTelemetryProtocolMismatch | NativeTelemetryDecodeFailed
> => {
const version = eventVersion(value);
if (version !== undefined && version !== RESOURCE_MONITOR_PROTOCOL_VERSION) {
return Effect.fail(
new NativeTelemetryProtocolMismatch({
expectedVersion: RESOURCE_MONITOR_PROTOCOL_VERSION,
receivedVersion: version,
}),
);
}
return decodeMonitorEvent(value).pipe(
Effect.mapError((cause) => new NativeTelemetryDecodeFailed({ cause })),
);
},
),
Stream.runForEach((event) => processEvent(event, helloDeferred, generation)),
Effect.mapError((cause) =>
isProtocolMismatch(cause) || isDecodeFailed(cause) || isCommandFailed(cause)
? cause
: new NativeTelemetryDecodeFailed({ cause }),
),
Effect.forkScoped,
);
yield* handle.stderr.pipe(Stream.runDrain, Effect.ignore, Effect.forkScoped);
const hello = yield* Deferred.await(helloDeferred).pipe(
Effect.timeoutOption(HANDSHAKE_TIMEOUT),
Effect.flatMap(
Option.match({
onNone: () =>
Effect.fail(
new NativeTelemetryHandshakeTimedOut({
timeoutMs: Duration.toMillis(HANDSHAKE_TIMEOUT),
}),
),
onSome: Effect.succeed,
}),
),
);
yield* synchronizeCollectionControlOnStart(
controlMutex,
collectionControl,
appliedCollectionControl,
(control) =>
Effect.gen(function* () {
yield* writeCommand(handle, {
version: RESOURCE_MONITOR_PROTOCOL_VERSION,
type: "configure",
rootPid: process.pid,
sampleIntervalMs: control.sampleIntervalMs,
externalProcesses: [...(yield* Ref.get(externalProcesses))],
});
if (control.liveSubscriberCount > 0) {
yield* writeCommand(handle, {
version: RESOURCE_MONITOR_PROTOCOL_VERSION,
type: "setStreaming",
enabled: true,
});
}
}),
Ref.update(state, (current) => ({
...current,
status: "healthy" as const,
hello: Option.some(hello),
})).pipe(Effect.andThen(publishHealth)),
);
yield* writeCommand(handle, {
version: RESOURCE_MONITOR_PROTOCOL_VERSION,
type: "setExternalProcesses",
processes: [...(yield* Ref.get(externalProcesses))],
});
const exitEffect = handle.exitCode.pipe(
Effect.mapError(
(cause) =>
new NativeTelemetryCommandFailed({
operation: "waitForExit",
cause,
}),
),
Effect.flatMap((exitCode) =>
Effect.fail(new NativeTelemetryExited({ exitCode: Number(exitCode) })),
),
);
const decoderEffect = Fiber.join(eventFiber).pipe(
Effect.andThen(Effect.fail(new NativeTelemetryStreamClosed())),
);
return yield* Effect.raceFirst(exitEffect, decoderEffect);
}),
).pipe(
Effect.ensuring(
Ref.update(state, (current) => ({
...current,
handle: Option.none(),
})),
),
);
yield* Effect.gen(function* () {
let failures: ReadonlyArray<number> = [];
let restartAttempt = 0;
while (true) {
const result = yield* Effect.result(runAttempt);
if (Result.isSuccess(result)) {
return;
}
const error = result.failure;
const now = DateTime.toEpochMillis(yield* DateTime.now);
const recentFailures = retainRecentNativeTelemetryFailures(failures, now);
if (recentFailures.length === 0) {
restartAttempt = 0;
}
failures = [...recentFailures, now];
const exhausted = failures.length >= MAX_FAILURES_PER_WINDOW;
yield* Ref.update(state, (current) => ({
...current,
status: exhausted ? ("unavailable" as const) : ("degraded" as const),
hello: Option.none(),
lastError: Option.some(errorMessage(error)),
restartCount: current.restartCount + 1,
}));
yield* publishHealth;
yield* failPending(error);
if (exhausted) {
yield* Queue.take(retryQueue);
failures = [];
restartAttempt = 0;
yield* Ref.update(state, (current) => ({
...current,
status: "starting" as const,
hello: Option.none(),
lastError: Option.none(),
}));
yield* publishHealth;
continue;
}
const manuallyRetried = yield* Effect.raceFirst(
Effect.sleep(restartDelay(restartAttempt)).pipe(Effect.as(false)),
Queue.take(retryQueue).pipe(Effect.as(true)),
);
restartAttempt = manuallyRetried ? 0 : restartAttempt + 1;
}
}).pipe(
Effect.catchCause((cause) =>
Cause.hasInterruptsOnly(cause)
? Effect.void
: Ref.update(state, (current) => ({
...current,
status: "unavailable" as const,
hello: Option.none(),
lastError: Option.some("Resource monitor supervisor stopped unexpectedly."),
})).pipe(
Effect.andThen(publishHealth),
Effect.andThen(
Effect.logWarning("Resource monitor supervisor failed", {
cause: Cause.pretty(cause),
}),
),
),
),
Effect.forkScoped,
);
// Desktop power samples reach this even when the control state has not changed.
const applyCollectionControl = Effect.fnUntraced(function* (
previous: CollectionControl,
next: CollectionControl,
) {
const current = yield* Ref.get(state);
if (canCommandNativeTelemetrySidecar(current.status, Option.isSome(current.handle))) {
const handle = Option.getOrThrow(current.handle);
if (previous.sampleIntervalMs !== next.sampleIntervalMs) {
yield* writeCommand(handle, {
version: RESOURCE_MONITOR_PROTOCOL_VERSION,
type: "setSampleInterval",
sampleIntervalMs: next.sampleIntervalMs,
});
}
const wasStreaming = previous.liveSubscriberCount > 0;
const isStreaming = next.liveSubscriberCount > 0;
if (wasStreaming !== isStreaming) {
yield* writeCommand(handle, {
version: RESOURCE_MONITOR_PROTOCOL_VERSION,
type: "setStreaming",
enabled: isStreaming,
});
}
}
});
const updateCollectionControl = (update: (current: CollectionControl) => CollectionControl) =>
controlMutex.withPermits(1)(
commitCollectionControlUpdate(
collectionControl,
appliedCollectionControl,
update,
applyCollectionControl,
).pipe(Effect.ensuring(publishHealth), Effect.asVoid),
);
const setHostPowerState: NativeTelemetryClient["Service"]["setHostPowerState"] = (hostPower) =>
updateCollectionControl((current) => ({
...current,
hostPower,
sampleIntervalMs: resolveNativeSampleIntervalMs(hostPower, current.liveSubscriberCount),
}));
const changeLiveSubscriberCount = Effect.fn(
"resourceTelemetry.nativeTelemetryClient.changeLiveSubscriberCount",
)(function* (delta: 1 | -1) {
yield* updateCollectionControl((current) => {
const liveSubscriberCount = Math.max(0, current.liveSubscriberCount + delta);
return {
...current,
liveSubscriberCount,
sampleIntervalMs: resolveNativeSampleIntervalMs(current.hostPower, liveSubscriberCount),
};
});
});
const liveSnapshots = Stream.unwrap(
Effect.gen(function* () {
const subscription = yield* PubSub.subscribe(snapshots);
yield* Effect.acquireRelease(changeLiveSubscriberCount(1), () =>
changeLiveSubscriberCount(-1).pipe(Effect.ignore),
);
return Stream.fromSubscription(subscription);
}),
);
// Desktop telemetry refreshes this process set on every sample.
const setExternalProcesses: NativeTelemetryClient["Service"]["setExternalProcesses"] =
Effect.fnUntraced(function* (processes) {
yield* Ref.set(externalProcesses, [...processes]);
const current = yield* Ref.get(state);
if (!canCommandNativeTelemetrySidecar(current.status, Option.isSome(current.handle))) return;
yield* writeCommand(Option.getOrThrow(current.handle), {
version: RESOURCE_MONITOR_PROTOCOL_VERSION,
type: "setExternalProcesses",
processes: [...processes],
});
});
const readHistory: NativeTelemetryClient["Service"]["readHistory"] = Effect.fn(
"NativeTelemetryClient.readHistory",
)(function* (windowMs) {
const current = yield* Ref.get(state);
if (!canCommandNativeTelemetrySidecar(current.status, Option.isSome(current.handle))) {
return yield* new NativeTelemetryUnavailable({
reason: Option.getOrElse(current.lastError, () => "sidecar is not running"),
});
}
const requestId = yield* random.uuidV4().pipe(
Effect.mapError(
(cause) =>
new NativeTelemetryCommandFailed({
operation: "createHistoryRequestId",
cause,
}),
),
);
const deferred = yield* Deferred.make<
ReadonlyArray<ResourceMonitorSnapshotEvent>,
NativeTelemetryClientError
>();
yield* Ref.update(pendingHistories, (pending) => {
const next = new Map(pending);
next.set(requestId, { deferred, snapshots: [] });
return next;
});
return yield* writeCommand(Option.getOrThrow(current.handle), {
version: RESOURCE_MONITOR_PROTOCOL_VERSION,
type: "readHistory",
requestId,
windowMs: Math.max(0, Math.round(windowMs)),
}).pipe(
Effect.andThen(
Deferred.await(deferred).pipe(
Effect.timeoutOption(HISTORY_REQUEST_TIMEOUT),
Effect.flatMap(
Option.match({
onNone: () =>
Effect.fail(
new NativeTelemetryRequestTimedOut({
operation: "readHistory",
timeoutMs: Duration.toMillis(HISTORY_REQUEST_TIMEOUT),
}),
),
onSome: Effect.succeed,
}),
),
),
),
Effect.ensuring(
Ref.update(pendingHistories, (pending) => {
const next = new Map(pending);
next.delete(requestId);
return next;
}),
),
);
});
const sampleNow: NativeTelemetryClient["Service"]["sampleNow"] = Effect.gen(function* () {
const current = yield* Ref.get(state);
if (!canCommandNativeTelemetrySidecar(current.status, Option.isSome(current.handle))) {
return yield* new NativeTelemetryUnavailable({
reason: Option.getOrElse(current.lastError, () => "sidecar is not running"),
});
}
const requestId = yield* random.uuidV4().pipe(
Effect.mapError(
(cause) =>
new NativeTelemetryCommandFailed({
operation: "createRequestId",
cause,
}),
),
);
const deferred = yield* Deferred.make<NativeTelemetrySnapshot, NativeTelemetryClientError>();
yield* Ref.update(pendingSamples, (pending) => {
const next = new Map(pending);
next.set(requestId, deferred);
return next;
});
return yield* writeCommand(Option.getOrThrow(current.handle), {
version: RESOURCE_MONITOR_PROTOCOL_VERSION,
type: "sampleNow",
requestId,
}).pipe(
Effect.andThen(
Deferred.await(deferred).pipe(
Effect.timeoutOption(SAMPLE_REQUEST_TIMEOUT),
Effect.flatMap(
Option.match({
onNone: () =>
Effect.fail(
new NativeTelemetryRequestTimedOut({
operation: "sampleNow",
timeoutMs: Duration.toMillis(SAMPLE_REQUEST_TIMEOUT),
}),
),
onSome: Effect.succeed,
}),
),
),
),
Effect.ensuring(
Ref.update(pendingSamples, (pending) => {
const next = new Map(pending);
next.delete(requestId);
return next;
}),
),
);
});
const processTable: NativeTelemetryClient["Service"]["processTable"] = Effect.gen(function* () {
const current = yield* Ref.get(state);
if (!canCommandNativeTelemetrySidecar(current.status, Option.isSome(current.handle))) {
return yield* new NativeTelemetryUnavailable({
reason: Option.getOrElse(current.lastError, () => "sidecar is not running"),
});
}
const requestId = yield* random
.uuidV4()
.pipe(
Effect.mapError(
(cause) => new NativeTelemetryCommandFailed({ operation: "createRequestId", cause }),
),
);
const deferred = yield* Deferred.make<
ReadonlyArray<ResourceMonitorProcessTableEntry>,
NativeTelemetryClientError
>();
yield* Ref.update(pendingProcessTables, (pending) => {
const next = new Map(pending);
next.set(requestId, deferred);
return next;
});
return yield* writeCommand(Option.getOrThrow(current.handle), {
version: RESOURCE_MONITOR_PROTOCOL_VERSION,
type: "processTable",
requestId,