From c700ea694ffa3be02d340a300b937486b1ee749b Mon Sep 17 00:00:00 2001 From: Mauro Servienti Date: Thu, 22 Jan 2026 17:25:52 +0100 Subject: [PATCH] Update MemoryInformationRetriever to use the DocumentStore already configured HTTP Client --- .../MemoryInformationRetriever.cs | 14 +++++--------- .../MemoryInformationRetriever.cs | 14 +++++--------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/ServiceControl.Audit.Persistence.RavenDB/MemoryInformationRetriever.cs b/src/ServiceControl.Audit.Persistence.RavenDB/MemoryInformationRetriever.cs index 3564d1bcb3..d426efc7ec 100644 --- a/src/ServiceControl.Audit.Persistence.RavenDB/MemoryInformationRetriever.cs +++ b/src/ServiceControl.Audit.Persistence.RavenDB/MemoryInformationRetriever.cs @@ -6,15 +6,8 @@ namespace ServiceControl.Audit.Persistence.RavenDB; using System.Threading; using System.Threading.Tasks; -class MemoryInformationRetriever(DatabaseConfiguration databaseConfiguration) +class MemoryInformationRetriever(IRavenDocumentStoreProvider documentStoreProvider) { - // Connection string is composed of the server URL. 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. - // Only one of ConnectionString and ServerUrl will be non-null, so we'll check ConnectionString first - // to be consistent with the error instance implementation, where ServerUrl always has a value. - readonly HttpClient client = new() { BaseAddress = new Uri(databaseConfiguration.ServerConfiguration.ConnectionString ?? databaseConfiguration.ServerConfiguration.ServerUrl) }; - record ResponseDto { public MemoryInformation MemoryInformation { get; set; } @@ -28,7 +21,10 @@ record MemoryInformation public async Task<(bool IsHighDirty, string DirtyMemory)> GetMemoryInformation(CancellationToken cancellationToken = default) { - var httpResponse = await client.GetAsync("/admin/debug/memory/stats?includeThreads=false&includeMappings=false", cancellationToken); + var documentStore = await documentStoreProvider.GetDocumentStore(cancellationToken); + var client = documentStore.GetRequestExecutor().HttpClient; + var requestUrl = documentStore.Urls[0].TrimEnd('/') + "/admin/debug/memory/stats?includeThreads=false&includeMappings=false"; + var httpResponse = await client.GetAsync(requestUrl, cancellationToken); var responseDto = JsonSerializer.Deserialize(await httpResponse.Content.ReadAsStringAsync(cancellationToken)); return responseDto.MemoryInformation is null diff --git a/src/ServiceControl.Persistence.RavenDB/MemoryInformationRetriever.cs b/src/ServiceControl.Persistence.RavenDB/MemoryInformationRetriever.cs index 4e24e98ede..78b2958548 100644 --- a/src/ServiceControl.Persistence.RavenDB/MemoryInformationRetriever.cs +++ b/src/ServiceControl.Persistence.RavenDB/MemoryInformationRetriever.cs @@ -6,15 +6,8 @@ namespace ServiceControl.Persistence.RavenDB; using System.Threading; using System.Threading.Tasks; -class MemoryInformationRetriever(RavenPersisterSettings persisterSettings) +class MemoryInformationRetriever(IRavenDocumentStoreProvider documentStoreProvider) { - // Connection string is composed of the server URL. 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. - // ServerUrl is always populated by the persister settings, hence the code first checks for the - // presence of a connection string, and if null, falls back to ServerUrl - readonly HttpClient client = new() { BaseAddress = new Uri(persisterSettings.ConnectionString ?? persisterSettings.ServerUrl) }; - record ResponseDto { public MemoryInformation MemoryInformation { get; set; } @@ -28,7 +21,10 @@ record MemoryInformation public async Task<(bool IsHighDirty, string DirtyMemory)> GetMemoryInformation(CancellationToken cancellationToken = default) { - var httpResponse = await client.GetAsync("/admin/debug/memory/stats?includeThreads=false&includeMappings=false", cancellationToken); + var documentStore = await documentStoreProvider.GetDocumentStore(cancellationToken); + var client = documentStore.GetRequestExecutor().HttpClient; + var requestUrl = documentStore.Urls[0].TrimEnd('/') + "/admin/debug/memory/stats?includeThreads=false&includeMappings=false"; + var httpResponse = await client.GetAsync(requestUrl, cancellationToken); var responseDto = JsonSerializer.Deserialize(await httpResponse.Content.ReadAsStringAsync(cancellationToken)); return responseDto.MemoryInformation is null