Repository navigation
Expand file tree
/
Copy pathQuick.Core.Extensions.HealthChecks.pas
More file actions
699 lines (614 loc) · 21.6 KB
/
Copy pathQuick.Core.Extensions.HealthChecks.pas
File metadata and controls
699 lines (614 loc) · 21.6 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
{ ***************************************************************************
Copyright (c) 2016-2026 Kike Perez
Unit : Quick.Core.Extensions.HealthChecks
Description : Core Extensions Health Checks
Author : Kike Perez
Version : 1.0
Created : 12/02/2021
Modified : 08/05/2026
This file is part of QuickCore: https://github.com/exilon/QuickCore
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Core.Extensions.HealthChecks;
{$i QuickCore.inc}
interface
uses
{$IFDEF DEBUG_HEALTHCHECKS}
Quick.Debug.Utils,
{$ENDIF}
System.JSON,
System.Classes,
System.SysUtils,
System.TimeSpan,
System.Net.HttpClient,
System.Generics.Collections,
Quick.Collections,
Quick.Core.Serialization.Abstractions,
Quick.Options,
Quick.Threads,
Quick.Chrono,
Quick.Core.Entity,
Quick.Core.DependencyInjection,
Quick.Core.Logging.Abstractions;
type
THealthCheckOptions = class(TOptions)
private
fLogFails : Boolean;
fRetryTimes : Integer;
fMaxSize : Integer;
fDegradedIfSlowerThanMs : Integer;
published
constructor Create; override;
property LogFails : Boolean read fLogFails write fLogFails;
property RetryTimes : Integer read fRetryTimes write fRetryTimes;
property MaxSize : Integer read fMaxSize write fMaxSize;
// If check passes but took longer than this threshold (ms), mark as Degraded. 0 = disabled.
property DegradedIfSlowerThanMs : Integer read fDegradedIfSlowerThanMs write fDegradedIfSlowerThanMs;
end;
THealthStatus = (hsNotChecked, hsPassed, hsDegraded, hsFailed);
IHealthCheck = interface
['{7FA7D505-1C60-443C-A126-3103B69BA3CC}']
function GetStatus: THealthStatus;
procedure SetStatus(const Value: THealthStatus);
function GetLastCheck: TDateTime;
function GetName: string;
procedure SetName(const Value: string);
function GetTimeElapsed: string;
procedure SetTimeElapsed(const Value: string);
function GetErrorMsg: string;
procedure SetErrorMsg(const Value: string);
function GetLastStatusChange: TDateTime;
function GetCheckEveryMSecs : Int64;
//public
property Name : string read GetName write SetName;
property LastCheck : TDateTime read GetLastCheck;
property LastStatusChange : TDateTime read GetLastStatusChange;
property TimeElapsed : string read GetTimeElapsed write SetTimeElapsed;
property Status : THealthStatus read GetStatus write SetStatus;
property CheckEveryMSecs : Int64 read GetCheckEveryMSecs;
property ErrorMsg : string read GetErrorMsg write SetErrorMsg;
procedure Check;
end;
IHealthCheckInfo = interface
['{A3F1C820-9D4B-4E7A-BC12-05E7B4D9F312}']
function GetCheckKind : string;
function GetTags : string;
property CheckKind : string read GetCheckKind;
property Tags : string read GetTags;
end;
THealthCheck = class(TInterfacedObject, IHealthCheck, IHealthCheckInfo)
private
function GetStatus: THealthStatus;
procedure SetStatus(const Value: THealthStatus);
function GetLastCheck: TDateTime;
function GetName: string;
procedure SetName(const Value: string);
function GetTimeElapsed: string;
procedure SetTimeElapsed(const Value: string);
function GetErrorMsg: string;
procedure SetErrorMsg(const Value: string);
function GetLastStatusChange: TDateTime;
function GetCheckEveryMSecs : Int64;
protected
fName : string;
fLastCheck : TDateTime;
fLastStatusChange : TDateTime;
fTimeElapsed : string;
fCheckEveryMSecs : Int64;
fStatus : THealthStatus;
fErrorMsg : string;
public
constructor Create(aTimeSpan : TTimeSpan);
property Name : string read GetName write SetName;
property LastCheck : TDateTime read GetLastCheck;
property TimeElapsed : string read GetTimeElapsed write SetTimeElapsed;
property Status : THealthStatus read GetStatus;
property LastStatusChange : TDateTime read GetLastStatusChange;
property CheckEveryMSecs : Int64 read GetCheckEveryMSecs;
property ErrorMsg : string read GetErrorMsg write SetErrorMsg;
function GetCheckKind : string; virtual;
function GetTags : string; virtual;
procedure Check; virtual;
end;
TUrlHealthCheck = class(THealthCheck)
private
fUrl : string;
public
constructor Create(const aUrl: string; aTimeSpan : TTimeSpan);
destructor Destroy; override;
function GetCheckKind : string; override;
function GetTags : string; override;
procedure Check; override;
end;
THealthCheckFailEvent = reference to procedure(aHealtCheck : IHealthCheck);
{$M+}
THealthCheckMetric = class
private
fName : string;
fLastCheck : TDateTime;
fTimeElapsed : string;
fCheckEveryMSecs : Int64;
fStatus : THealthStatus;
fLastStatusChange : TDateTime;
fErrorMsg : string;
fCheckKind : string;
fTags : string;
published
property Name : string read fName write fName;
property LastCheck : TDateTime read fLastCheck write fLastCheck;
property TimeElapsed : string read fTimeElapsed write fTimeElapsed;
property CheckEveryMSecs : Int64 read fCheckEveryMSecs write fCheckEveryMSecs;
property Status : THealthStatus read fStatus write fStatus;
property LastStatusChange : TDateTime read fLastStatusChange write fLastStatusChange;
property ErrorMsg : string read fErrorMsg write fErrorMsg;
property CheckKind : string read fCheckKind write fCheckKind;
property Tags : string read fTags write fTags;
end;
{$M-}
IHealthChecksService = interface
['{BC78D855-4E19-4DBD-869A-10B423B08076}']
function GetMetrics : IList<THealthCheckMetric>;
function GetCheckHistory(const aName : string; aCount : Integer) : IList<THealthCheckMetric>;
end;
IHealthChecksStore = interface
['{9B16807B-FF16-4FBF-AB36-85D30364D70C}']
procedure Add(aHealthCheck : IHealthCheck);
function Last : IList<THealthCheckMetric>;
function GetHistory(const aName : string; aCount : Integer) : IList<THealthCheckMetric>;
end;
THealthCheckStore = class(TInterfacedObject,IHealthChecksStore)
protected
function MapMetric(aHealthCheck : IHealthCheck) : THealthCheckMetric;
public
class function MapMetricStatic(aHealthCheck : IHealthCheck) : THealthCheckMetric; static;
procedure Add(aHealthCheck : IHealthCheck); virtual; abstract;
function Last : IList<THealthCheckMetric>; virtual; abstract;
function GetHistory(const aName : string; aCount : Integer) : IList<THealthCheckMetric>; virtual; abstract;
end;
TMemoryHealthCheckStore = class(THealthCheckStore)
private type
THealthCheckMetrics = TObjectList<THealthCheckMetric>;
private
fCheckMetrics : TObjectDictionary<string,THealthCheckMetrics>;
fMaxSize : Integer;
public
constructor Create(aMaxSize : Integer);
destructor Destroy; override;
procedure Add(aHealthCheck : IHealthCheck); override;
function Last : IList<THealthCheckMetric>; override;
function GetHistory(const aName : string; aCount : Integer) : IList<THealthCheckMetric>; override;
end;
THealthChecksService = class(TInterfacedObject,IHealthChecksService)
private
fServiceCollection : TServiceCollection;
fHistory : IHealthChecksStore;
fOptions : THealthCheckOptions;
fLogger : ILogger;
fHealthCheckList : TList<IHealthCheck>;
fScheduler : TScheduledTasks;
fHealthCheckFailEvent : THealthCheckFailEvent;
function GetMetrics : IList<THealthCheckMetric>;
function GetCheckHistory(const aName : string; aCount : Integer) : IList<THealthCheckMetric>;
public
constructor Create(aServiceCollection : TServiceCollection; aOptions : THealthCheckOptions; aLogger : ILogger);
destructor Destroy; override;
property ServiceCollection : TServiceCollection read fServiceCollection;
function OnCheckFail(aHealthFailProc : THealthCheckFailEvent) : THealthChecksService;
function AddCheck(aHealthCheck : IHealthCheck) : THealthChecksService;
function AddDBContextCheck<T : TDBContext>(const aName : string; aTimeSpan : TTimeSpan) : THealthChecksService;
function AddUrlCheck(const aName, aUrl : string; aTimeSpan : TTimeSpan) : THealthChecksService;
function AddDiskStorageCheck(const aName, aPath : string; aMinFreeGB : Double; aTimeSpan : TTimeSpan) : THealthChecksService;
function AddMemoryCheck(const aName : string; aMaxUsedMB : Int64; aTimeSpan : TTimeSpan) : THealthChecksService;
function AddInMemoryStorage : THealthChecksService;
end;
THealthChecksServiceExtension = class(TServiceCollectionExtension)
class function AddHealthChecks(aConfigureOptions : TConfigureOptionsProc<THealthCheckOptions> = nil) : THealthChecksService;
end;
THealthChecksExtension = class
private class var
fHealthChecksService : THealthChecksService;
class function SetService(aHealthChecksService : THealthChecksService) : THealthChecksExtension;
public
class property HealthChecksService : THealthChecksService read fHealthChecksService;
end;
THealthChecksHelper = class helper for THealthChecksService
function Extension<T : THealthChecksExtension> : T;
end;
implementation
uses
Quick.Core.Extensions.HealthChecks.Entity,
Quick.Core.Extensions.HealthChecks.DiskStorage,
Quick.Core.Extensions.HealthChecks.Memory;
{ THealthChecksServiceExtension }
class function THealthChecksServiceExtension.AddHealthChecks(aConfigureOptions : TConfigureOptionsProc<THealthCheckOptions>) : THealthChecksService;
var
options : THealthCheckOptions;
begin
if not ServiceCollection.IsRegistered<THealthChecksService> then
begin
options := THealthCheckOptions.Create;
if Assigned(aConfigureOptions) then
begin
aConfigureOptions(options);
end;
Result := THealthChecksService.Create(ServiceCollection, options,ServiceCollection.AppServices.Logger);
ServiceCollection.AddSingleton<IHealthChecksService>(Result);
end
else raise Exception.Create('Already registered HealthChecks extension!');
end;
{ THealthChecksService }
constructor THealthChecksService.Create(aServiceCollection : TServiceCollection; aOptions : THealthCheckOptions; aLogger : ILogger);
begin
fServiceCollection := aServiceCollection;
fOptions := aOptions;
fLogger := aLogger;
fHealthCheckList := TList<IHealthCheck>.Create;
fScheduler := TScheduledTasks.Create;
fScheduler.Start;
end;
destructor THealthChecksService.Destroy;
begin
fScheduler.Stop;
fScheduler.Free;
fHealthCheckList.Free;
inherited;
end;
function THealthChecksService.AddCheck(aHealthCheck: IHealthCheck) : THealthChecksService;
begin
Result := Self;
fHealthCheckList.Add(aHealthCheck);
fScheduler.AddTask('',procedure(task : ITask)
var
chrono : TChronometer;
retryCount : Integer;
lastErrMsg : string;
succeeded : Boolean;
waitMs : Integer;
begin
{$IFDEF DEBUG_HEALTHCHECKS}
TDebugger.Trace(Self,'HealthCheck %s...',[aHealthCheck.Name]);
{$ENDIF}
retryCount := 0;
lastErrMsg := '';
succeeded := False;
repeat
// delay before each retry: 100ms on 1st, 200ms on subsequent
if retryCount > 0 then
begin
if retryCount = 1 then waitMs := 100 else waitMs := 200;
TThread.Sleep(waitMs);
end;
chrono := TChronometer.Create(True);
try
try
aHealthCheck.Check;
chrono.Stop;
aHealthCheck.TimeElapsed := chrono.ElapsedTime(False);
aHealthCheck.ErrorMsg := '';
if (retryCount > 0) or
((fOptions.DegradedIfSlowerThanMs > 0) and
(chrono.ElapsedMilliseconds >= fOptions.DegradedIfSlowerThanMs)) then
aHealthCheck.Status := THealthStatus.hsDegraded
else
aHealthCheck.Status := THealthStatus.hsPassed;
{$IFDEF DEBUG_HEALTHCHECKS}
TDebugger.Trace(Self,'HealthCheck %s status ok (%s, retries=%d)',[aHealthCheck.Name,aHealthCheck.TimeElapsed,retryCount]);
{$ENDIF}
succeeded := True;
except
on E : Exception do
begin
chrono.Stop;
lastErrMsg := E.Message;
Inc(retryCount);
end;
end;
finally
chrono.Free;
end;
until succeeded or (retryCount > fOptions.RetryTimes);
// all attempts exhausted - raise to trigger OnException
if not succeeded then raise Exception.Create(lastErrMsg);
end)
.OnException(procedure(task : ITask; aException : Exception)
begin
//mark health as failed
aHealthCheck.Status := THealthStatus.hsFailed;
aHealthCheck.ErrorMsg := aException.Message;
{$IFDEF DEBUG_HEALTHCHECKS}
TDebugger.Trace(Self,'HealthCheck %s status failed (%s)',[aHealthCheck.Name,aHealthCheck.ErrorMsg]);
{$ENDIF}
if fOptions.LogFails then fLogger.Critical('HealthCheck %s status failed (%s)',[aHealthCheck.Name,aHealthCheck.ErrorMsg]);
if Assigned(fHealthCheckFailEvent) then fHealthCheckFailEvent(aHealthCheck);
end)
.OnTerminated(procedure(task : ITask)
begin
if fHistory <> nil then fHistory.Add(aHealthCheck);
end)
.StartInSeconds(5).RepeatEvery(aHealthCheck.CheckEveryMSecs,TTimeMeasure.tmMilliseconds);
end;
function THealthChecksService.AddDBContextCheck<T>(const aName : string; aTimeSpan : TTimeSpan) : THealthChecksService;
begin
Result := Extension<TEntityHealthChecksExtension>.AddDBContextCheck<T>(aName, aTimeSpan);
end;
function THealthChecksService.AddInMemoryStorage: THealthChecksService;
begin
Result := Self;
fHistory := TMemoryHealthCheckStore.Create(fOptions.MaxSize);
end;
function THealthChecksService.AddDiskStorageCheck(const aName, aPath : string; aMinFreeGB : Double; aTimeSpan : TTimeSpan) : THealthChecksService;
var
check : IHealthCheck;
begin
Result := Self;
check := TDiskStorageHealthCheck.Create(aPath, aMinFreeGB, aTimeSpan);
check.Name := aName;
AddCheck(check);
end;
function THealthChecksService.AddMemoryCheck(const aName : string; aMaxUsedMB : Int64; aTimeSpan : TTimeSpan) : THealthChecksService;
var
check : IHealthCheck;
begin
Result := Self;
check := TMemoryHealthCheck.Create(aMaxUsedMB, aTimeSpan);
check.Name := aName;
AddCheck(check);
end;
function THealthChecksService.AddUrlCheck(const aName, aUrl : string; aTimeSpan : TTimeSpan) : THealthChecksService;
var
check : IHealthCheck;
begin
Result := Self;
check := TUrlHealthCheck.Create(aUrl,aTimeSpan);
check.Name := aName;
AddCheck(check);
end;
function THealthChecksService.GetMetrics : IList<THealthCheckMetric>;
var
healthcheck : IHealthCheck;
lastMetrics : IList<THealthCheckMetric>;
begin
{$IFDEF DEBUG_HEALTHCHECKS}
TDebugger.TimeIt(Self,'GetMetrics','Getting HealthChecks Metric');
{$ENDIF}
if fHistory <> nil then lastMetrics := fHistory.Last
else lastMetrics := nil;
// If we have history results, return them
if (lastMetrics <> nil) and (lastMetrics.Count > 0) then
begin
Result := lastMetrics;
Exit;
end;
// No checks have run yet — return all registered checks as Pending
Result := TxList<THealthCheckMetric>.Create;
for healthcheck in fHealthCheckList do
begin
var metric := THealthCheckStore.MapMetricStatic(healthcheck);
Result.Add(metric);
end;
end;
function THealthChecksService.GetCheckHistory(const aName : string; aCount : Integer) : IList<THealthCheckMetric>;
begin
if fHistory <> nil then Result := fHistory.GetHistory(aName, aCount)
else Result := TxList<THealthCheckMetric>.Create;
end;
function THealthChecksService.OnCheckFail(aHealthFailProc: THealthCheckFailEvent): THealthChecksService;
begin
Result := Self;
fHealthCheckFailEvent := aHealthFailProc;
end;
{ THealthCheck }
procedure THealthCheck.Check;
begin
fLastCheck := Now();
end;
function THealthCheck.GetCheckEveryMSecs: Int64;
begin
Result := fCheckEveryMSecs;
end;
constructor THealthCheck.Create(aTimeSpan: TTimeSpan);
begin
fName := '';
fStatus := THealthStatus.hsNotChecked;
fCheckEveryMSecs := Round(aTimeSpan.TotalMilliseconds);
end;
function THealthCheck.GetErrorMsg: string;
begin
Result := fErrorMsg;
end;
function THealthCheck.GetLastCheck: TDateTime;
begin
Result := fLastCheck;
end;
function THealthCheck.GetLastStatusChange: TDateTime;
begin
Result := fLastStatusChange;
end;
function THealthCheck.GetName: string;
begin
if not fName.IsEmpty then Result := fName
else Result := Self.ClassName.Substring(1);
end;
function THealthCheck.GetStatus: THealthStatus;
begin
Result := fStatus;
end;
function THealthCheck.GetTimeElapsed: string;
begin
Result := fTimeElapsed;
end;
procedure THealthCheck.SetErrorMsg(const Value: string);
begin
fErrorMsg := Value;
end;
procedure THealthCheck.SetName(const Value: string);
begin
fName := Value;
end;
procedure THealthCheck.SetStatus(const Value: THealthStatus);
begin
if fStatus <> Value then fLastStatusChange := Now();
fStatus := Value;
end;
procedure THealthCheck.SetTimeElapsed(const Value: string);
begin
fTimeElapsed := Value;
end;
function THealthCheck.GetCheckKind: string;
begin
Result := 'custom';
end;
function THealthCheck.GetTags: string;
begin
Result := '{}';
end;
{ TUrlHealthCheck }
procedure TUrlHealthCheck.Check;
var
http : THttpClient;
statuscode : Integer;
begin
inherited;
http := THTTPClient.Create;
try
statuscode := http.Get(fUrl).StatusCode;
if (statuscode < 200) or (statuscode > 299) then raise Exception.CreateFmt('Url returned %d StatusCode',[statuscode]);
finally
http.Free;
end;
end;
constructor TUrlHealthCheck.Create(const aUrl: string; aTimeSpan : TTimeSpan);
begin
inherited Create(aTimeSpan);
fName := 'Url';
fUrl := aUrl;
end;
destructor TUrlHealthCheck.Destroy;
begin
inherited;
end;
function TUrlHealthCheck.GetCheckKind: string;
begin
Result := 'http';
end;
function TUrlHealthCheck.GetTags: string;
begin
Result := Format('{"url":"%s"}',[fUrl]);
end;
{ THealthCheckOptions }
constructor THealthCheckOptions.Create;
begin
fLogFails := True;
fRetryTimes := 0;
fMaxSize := 100;
fDegradedIfSlowerThanMs := 0;
end;
{ THealthChecksExtension }
class function THealthChecksExtension.SetService(aHealthChecksService: THealthChecksService): THealthChecksExtension;
begin
Result := THealthChecksExtension(Self);
fHealthChecksService := aHealthChecksService;
end;
{ THealthChecksHelper }
function THealthChecksHelper.Extension<T>: T;
begin
Result := T(THealthChecksExtension.SetService(Self));
end;
{ THealthCheckStore }
function THealthCheckStore.MapMetric(aHealthCheck: IHealthCheck): THealthCheckMetric;
var
info : IHealthCheckInfo;
begin
Result := THealthCheckMetric.Create;
Result.Name := aHealthCheck.Name;
Result.Status := aHealthCheck.Status;
Result.LastStatusChange := aHealthCheck.LastStatusChange;
Result.LastCheck := aHealthCheck.LastCheck;
Result.TimeElapsed := aHealthCheck.TimeElapsed;
Result.ErrorMsg := aHealthCheck.ErrorMsg;
Result.CheckEveryMSecs := aHealthCheck.CheckEveryMSecs;
if Supports(aHealthCheck, IHealthCheckInfo, info) then
begin
Result.CheckKind := info.GetCheckKind;
Result.Tags := info.GetTags;
end;
end;
class function THealthCheckStore.MapMetricStatic(aHealthCheck: IHealthCheck): THealthCheckMetric;
var
info : IHealthCheckInfo;
begin
Result := THealthCheckMetric.Create;
Result.Name := aHealthCheck.Name;
Result.Status := aHealthCheck.Status;
Result.LastStatusChange := aHealthCheck.LastStatusChange;
Result.LastCheck := aHealthCheck.LastCheck;
Result.TimeElapsed := aHealthCheck.TimeElapsed;
Result.ErrorMsg := aHealthCheck.ErrorMsg;
Result.CheckEveryMSecs := aHealthCheck.CheckEveryMSecs;
if Supports(aHealthCheck, IHealthCheckInfo, info) then
begin
Result.CheckKind := info.GetCheckKind;
Result.Tags := info.GetTags;
end;
end;
{ TMemoryHealthCheckStore }
constructor TMemoryHealthCheckStore.Create(aMaxSize: Integer);
begin
fMaxSize := aMaxSize;
fCheckMetrics := TObjectDictionary<string,THealthCheckMetrics>.Create([doOwnsValues]);
end;
destructor TMemoryHealthCheckStore.Destroy;
begin
fCheckMetrics.Free;
inherited;
end;
procedure TMemoryHealthCheckStore.Add(aHealthCheck: IHealthCheck);
var
metrics : THealthCheckMetrics;
metric : THealthCheckMetric;
begin
if not fCheckMetrics.TryGetValue(aHealthCheck.Name,metrics) then
begin
metrics := THealthCheckMetrics.Create(True);
fCheckMetrics.Add(aHealthCheck.Name,metrics)
end;
metric := MapMetric(aHealthCheck);
metrics.Add(metric);
if (fMaxSize > 0) and (metrics.Count > fMaxSize) then
metrics.Delete(0);
end;
function TMemoryHealthCheckStore.Last: IList<THealthCheckMetric>;
var
metrics : THealthCheckMetrics;
begin
Result := TxList<THealthCheckMetric>.Create;
for metrics in fCheckMetrics.Values do
begin
Result.Add(metrics.Last);
end;
end;
function TMemoryHealthCheckStore.GetHistory(const aName : string; aCount : Integer) : IList<THealthCheckMetric>;
var
metrics : THealthCheckMetrics;
i : Integer;
startIdx : Integer;
begin
Result := TxList<THealthCheckMetric>.Create;
if fCheckMetrics.TryGetValue(aName, metrics) then
begin
startIdx := metrics.Count - aCount;
if startIdx < 0 then startIdx := 0;
for i := startIdx to metrics.Count - 1 do
Result.Add(metrics[i]);
end;
end;
end.