-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathMemoryInformationRetriever.cs
More file actions
39 lines (33 loc) · 1.82 KB
/
MemoryInformationRetriever.cs
File metadata and controls
39 lines (33 loc) · 1.82 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
namespace ServiceControl.Audit.Persistence.RavenDB;
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
class MemoryInformationRetriever(DatabaseConfiguration databaseConfiguration)
{
// TODO what does a connection string look like? Is it only a URI or could it contain other stuff?
// The ?? operator is needed because ServerUrl is populated when running embedded and connection string when running in external mode.
// However the tricky part is that when tests are run they behave like if it was external mode
readonly HttpClient client = new() { BaseAddress = new Uri(databaseConfiguration.ServerConfiguration.ServerUrl ?? databaseConfiguration.ServerConfiguration.ConnectionString) };
record ResponseDto
{
public MemoryInformation MemoryInformation { get; set; }
}
record MemoryInformation
{
public bool IsHighDirty { get; set; }
public string DirtyMemory { get; set; }
}
public async Task<(bool IsHighDirty, int DirtyMemoryKb)> GetMemoryInformation(CancellationToken cancellationToken = default)
{
var httpResponse = await client.GetAsync("/admin/debug/memory/stats?includeThreads=false&includeMappings=false", cancellationToken);
var responseDto = JsonSerializer.Deserialize<ResponseDto>(await httpResponse.Content.ReadAsStringAsync(cancellationToken));
var values = responseDto.MemoryInformation.DirtyMemory.Split(' ');
if (!string.Equals(values[1], "KBytes", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException($"Unexpected response. Was expecting memory details in KBytes, instead received: {responseDto.MemoryInformation.DirtyMemory}");
}
return (responseDto.MemoryInformation.IsHighDirty, int.Parse(values[0]));
}
}