Skip to content

Commit 61b8142

Browse files
Register the MemoryInformationRetriever in DI
1 parent 2b2806b commit 61b8142

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

src/ServiceControl.Audit.Persistence.RavenDB/CustomChecks/CheckDirtyMemory.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@ namespace ServiceControl.Audit.Persistence.RavenDB.CustomChecks;
77
using NServiceBus.CustomChecks;
88
using NServiceBus.Logging;
99

10-
class CheckDirtyMemory(DatabaseConfiguration databaseConfiguration) : CustomCheck("ServiceControl.Audit database", "Dirty memory trends", TimeSpan.FromMinutes(5))
10+
class CheckDirtyMemory(MemoryInformationRetriever memoryInformationRetriever) : CustomCheck("ServiceControl.Audit database", "Dirty memory trends", TimeSpan.FromMinutes(5))
1111
{
1212
readonly List<int> lastDirtyMemoryReads = [];
1313
public override async Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default)
1414
{
15-
var retriever = GetMemoryRetriever();
16-
var (isHighDirty, dirtyMemory) = await retriever.GetMemoryInformation(cancellationToken);
15+
var (isHighDirty, dirtyMemoryKb) = await memoryInformationRetriever.GetMemoryInformation(cancellationToken);
1716

1817
if (isHighDirty)
1918
{
20-
var message = $"There is a high level of dirty memory ({dirtyMemory}kb). Check the ServiceControl " +
19+
var message = $"There is a high level of dirty memory ({dirtyMemoryKb}kb). Check the ServiceControl " +
2120
"troubleshooting guide for guidance on how to mitigate the issue.";
2221
Log.Warn(message);
2322
return CheckResult.Failed(message);
2423
}
2524

26-
lastDirtyMemoryReads.Add(dirtyMemory);
25+
lastDirtyMemoryReads.Add(dirtyMemoryKb);
2726
if (lastDirtyMemoryReads.Count > 20)
2827
{
2928
//cap the list at 20 which means we're keeping about 1 hour and 40 minutes of data
@@ -39,7 +38,7 @@ public override async Task<CheckResult> PerformCheck(CancellationToken cancellat
3938
// Three means we'll be observing for 15 minutes before calculating the trend
4039
case >= 3 when AnalyzeTrendUsingRegression(lastDirtyMemoryReads) == TrendDirection.Increasing:
4140
{
42-
var message = $"Dirty memory is increasing. Last available value is {dirtyMemory}kb. " +
41+
var message = $"Dirty memory is increasing. Last available value is {dirtyMemoryKb}kb. " +
4342
$"Check the ServiceControl troubleshooting guide for guidance on how to mitigate the issue.";
4443
Log.Warn(message);
4544
return CheckResult.Failed(message);
@@ -53,9 +52,6 @@ public override async Task<CheckResult> PerformCheck(CancellationToken cancellat
5352
return CheckResult.Pass;
5453
}
5554

56-
MemoryInformationRetriever _retriever;
57-
MemoryInformationRetriever GetMemoryRetriever() => _retriever ??= new MemoryInformationRetriever(databaseConfiguration.ServerConfiguration.ServerUrl);
58-
5955
static TrendDirection AnalyzeTrendUsingRegression(List<int> values)
6056
{
6157
if (values is not { Count: > 1 })

src/ServiceControl.Audit.Persistence.RavenDB/MemoryInformationRetriever.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
namespace ServiceControl.Audit.Persistence.RavenDB;
22

33
using System;
4-
using System.Linq;
54
using System.Net.Http;
65
using System.Text.Json;
76
using System.Threading;
87
using System.Threading.Tasks;
98

10-
class MemoryInformationRetriever(string serverUrl)
9+
class MemoryInformationRetriever(DatabaseConfiguration databaseConfiguration)
1110
{
12-
readonly HttpClient client = new() { BaseAddress = new Uri(serverUrl) };
11+
readonly HttpClient client = new() { BaseAddress = new Uri(databaseConfiguration.ServerConfiguration.ServerUrl) };
1312

1413
record ResponseDto
1514
{

src/ServiceControl.Audit.Persistence.RavenDB/RavenPersistence.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void AddPersistence(IServiceCollection services)
2121
static void ConfigureLifecycle(IServiceCollection services, DatabaseConfiguration databaseConfiguration)
2222
{
2323
services.AddSingleton(databaseConfiguration);
24+
services.AddSingleton<MemoryInformationRetriever>();
2425

2526
services.AddSingleton<IRavenSessionProvider, RavenSessionProvider>();
2627
services.AddHostedService<RavenPersistenceLifecycleHostedService>();

0 commit comments

Comments
 (0)