Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
aa1818c
Add plan for host audit ingestion in the primary instance
johnsimons Aug 20, 2026
d2c6525
Add primary audit persistence contracts and capability model
johnsimons Aug 20, 2026
959ecd7
Copy the audit ingestion runtime into the primary instance
johnsimons Aug 20, 2026
c33fea6
Add the audit ingestion only command and its startup checks
johnsimons Aug 20, 2026
d3cc627
Serve audit counts and saga history from local storage
johnsimons Aug 20, 2026
399c823
Restore platform connection details and licensing for local audit
johnsimons Aug 20, 2026
0a1eb01
Compose the audit ingestion only host
johnsimons Aug 20, 2026
a6c89bf
Document the audit hosting modes and pin the project boundary
johnsimons Aug 20, 2026
a84673d
Introduce AddServiceControlInstance and report host.name and process.…
johnsimons Sep 18, 2026
497cfbb
Add plan for EF audit persistence in the primary instance
johnsimons Aug 22, 2026
694bd89
Add the audit schema to the primary EF persisters
johnsimons Aug 22, 2026
457f986
Make audit partitioning SQL schema-aware when a custom schema is conf…
johnsimons Sep 14, 2026
f96c6af
Add code layout rules to the EF audit plan
johnsimons Sep 14, 2026
84fba2d
Move the shared ingestion helpers out of the error pipeline folder
johnsimons Sep 14, 2026
ffaaa67
Add the EF audit ingestion write path
johnsimons Sep 14, 2026
7711eb5
Add audit retention, partition lifecycle and the retention lock
johnsimons Sep 14, 2026
6ead227
Serve the message views, audit counts and saga history from EF
johnsimons Sep 14, 2026
c0b48ca
Store failed audit imports in EF
johnsimons Sep 14, 2026
623acf0
Advertise audit support on the SQL Server and PostgreSQL persisters
johnsimons Sep 14, 2026
f78e6cb
Add the audit instance host for a dedicated audit database
johnsimons Sep 14, 2026
3279a7e
Document the audit topologies of the relational persisters
johnsimons Sep 14, 2026
a30f623
Revise the audit ingestion document for clarity and completeness
johnsimons Sep 22, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions docs/audit-ingestion-in-the-primary.md

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions src/Particular.LicensingComponent.UnitTests/AuditQuery_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NUnit.Framework;
using Particular.Approvals;
using Particular.LicensingComponent.AuditThroughput;
using Particular.LicensingComponent.Contracts;
using Particular.LicensingComponent.UnitTests.Infrastructure;
using ServiceControl.Api;
using ServiceControl.Api.Contracts;
Expand Down Expand Up @@ -73,6 +74,43 @@ public async Task Should_return_audit_remotes()
}
}

[Test]
public async Task Should_return_the_local_audit_source_alongside_the_remotes()
{
//Arrange
var auditQuery = new AuditQuery(NullLogger<AuditQuery>.Instance, new FakeEndpointApi(), new FakeAuditCountApi(),
new ConfigurationApi_ReturningOneValidAuditConfig(), new LocalAuditSource_ForThisInstance());

//Act
var remotes = await auditQuery.GetAuditRemotes();

//Assert
Assert.That(remotes, Has.Count.EqualTo(2), "The local audit source and the remote should both be reported");

var local = remotes.Single(remote => remote.ApiUri == "http://localhost:33333/api/");

using (Assert.EnterMultipleScope())
{
Assert.That(local.Queues, Does.Contain("audit"), "the local audit queue must be recognised as a platform endpoint");
Assert.That(local.Queues, Does.Contain("audit.log"));
Assert.That(local.Transport, Is.EqualTo("LearningTransport"), "the report's audit service metadata is built from this");
}
}

[Test]
public async Task Should_not_report_a_local_audit_source_that_is_disabled()
{
//Arrange
var auditQuery = new AuditQuery(NullLogger<AuditQuery>.Instance, new FakeEndpointApi(), new FakeAuditCountApi(),
new ConfigurationApi_ReturningOneValidAuditConfig(), new LocalAuditSource_Disabled());

//Act
var remotes = await auditQuery.GetAuditRemotes();

//Assert
Assert.That(remotes, Has.Count.EqualTo(1));
}

[Test]
public async Task Should_return_successful_audit_connection_if_instances_exist_and_are_online()
{
Expand Down Expand Up @@ -203,6 +241,29 @@ public Task<List<Endpoint>> GetEndpoints(CancellationToken cancellationToken = d

}

class LocalAuditSource_ForThisInstance : ILocalAuditSource
{
public bool Enabled => true;

public RemoteInstanceInformation Describe() => new()
{
ApiUri = "http://localhost:33333/api/",
VersionString = "6.0.0",
SemanticVersion = new NuGet.Versioning.SemanticVersion(6, 0, 0),
Status = "online",
Retention = TimeSpan.FromDays(10),
Queues = ["audit", "audit.log"],
Transport = "LearningTransport"
};
}

class LocalAuditSource_Disabled : ILocalAuditSource
{
public bool Enabled => false;

public RemoteInstanceInformation Describe() => throw new InvalidOperationException("Describe must not be called when the source is disabled.");
}

class AuditCountApi_ReturningThreeAuditCounts : IAuditCountApi
{
public async Task<IList<AuditCount>> GetEndpointAuditCounts(string endpoint, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using ServiceControl.Api;
using AuditCount = Contracts.AuditCount;

public class AuditQuery(ILogger<AuditQuery> logger, IEndpointsApi endpointsApi, IAuditCountApi auditCountApi, IConfigurationApi configurationApi) : IAuditQuery
public class AuditQuery(ILogger<AuditQuery> logger, IEndpointsApi endpointsApi, IAuditCountApi auditCountApi, IConfigurationApi configurationApi, ILocalAuditSource? localAuditSource = null) : IAuditQuery
{
// Customers are expected to run at least version 4.29 for their Audit instances
public SemanticVersion MinAuditCountsVersion => new(4, 29, 0);
Expand Down Expand Up @@ -45,6 +45,11 @@ public async Task<List<RemoteInstanceInformation>> GetAuditRemotes(CancellationT
var remotes = await configurationApi.GetRemoteConfigs(cancellationToken);
var remotesInfo = new List<RemoteInstanceInformation>();

if (localAuditSource is { Enabled: true })
{
remotesInfo.Add(localAuditSource.Describe());
}

if (remotes.Any())
{
List<string> queues = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Particular.LicensingComponent.AuditThroughput;

using Particular.LicensingComponent.Contracts;

/// <summary>
/// Audit throughput collection is driven entirely by audit remotes. A primary that holds audit data
/// itself has no remote to describe it, so without this its own audit queues are counted as customer
/// endpoints and the audit service metadata in the licensing report is blank.
/// </summary>
public interface ILocalAuditSource
{
bool Enabled { get; }

RemoteInstanceInformation Describe();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Particular.LicensingComponent;
namespace Particular.LicensingComponent;

using AuditThroughput;
using BrokerThroughput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@

<!-- RavenDB is the one persister that supports maintenance mode; StartupModeTests covers it. -->
<Compile Remove="..\ServiceControl.AcceptanceTests\MaintenanceModeTests.cs" />

<Compile Remove="..\ServiceControl.AcceptanceTests\Auditing\When_composing_audit_ingestion_in_the_primary.cs" />

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a wildcard exclude since raven will never support primary audit ingestion?

<Compile Remove="..\ServiceControl.AcceptanceTests\Auditing\When_hosting_an_audit_instance.cs" />
<Compile Remove="..\ServiceControl.AcceptanceTests\Auditing\When_hosting_audit_ingestion_only.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
namespace ServiceControl.AcceptanceTests.Auditing
{
using System;
using System.IO;
using System.Linq;
using System.Runtime.Loader;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus;
using NUnit.Framework;
using Particular.LicensingComponent.AuditThroughput;
using Particular.ServiceControl;
using ServiceBus.Management.Infrastructure.Settings;
using ServiceControl.Auditing;
using ServiceControl.Connection;
using ServiceControl.Infrastructure;
using ServiceControl.Infrastructure.WebApi;
using ServiceControl.Persistence;
using ServiceControl.SagaAudit;

class When_composing_audit_ingestion_in_the_primary : AcceptanceTest
{
[Test]
public async Task Should_host_the_audit_runtime()
{
var (app, services) = await BuildHost();

try
{
using (Assert.EnterMultipleScope())
{
Assert.That(HostsAuditIngestion(services), Is.True);
Assert.That(app.Services.GetService<AuditIngestor>(), Is.Not.Null);
Assert.That(app.Services.GetService<ImportFailedAudits>(), Is.Not.Null);
Assert.That(app.Services.GetService<IFailedAuditImportDataStore>(), Is.Not.Null);
Assert.That(app.Services.GetService<IAuditCountsDataStore>(), Is.Not.Null);
Assert.That(app.Services.GetService<ISagaHistoryDataStore>(), Is.Not.Null);

Assert.That(app.Services.GetService<ILocalAuditSource>(), Is.Not.Null,
"without it the local audit queues are counted as customer endpoints in the licensing report");
Assert.That(services.Any(descriptor =>
descriptor.ServiceType == typeof(IProvidePlatformConnectionDetails)
&& descriptor.ImplementationType == typeof(AuditPlatformConnectionDetailsProvider)),
Is.True,
"/api/connection must still tell endpoints where to send audit and saga data");
}
}
finally
{
await app.DisposeAsync();
}
}

[Test]
public async Task Should_keep_every_audit_capability_but_the_receiver_when_ingestion_is_disabled()
{
var (app, services) = await BuildHost(settings => settings.IngestAuditMessages = false);

try
{
using (Assert.EnterMultipleScope())
{
Assert.That(HostsAuditIngestion(services), Is.False,
"the receiver is the only thing the setting turns off, because other processes may still be ingesting");
Assert.That(app.Services.GetService<AuditIngestor>(), Is.Not.Null);
Assert.That(app.Services.GetService<IAuditCountsDataStore>(), Is.Not.Null);
}
}
finally
{
await app.DisposeAsync();
}
}

[Test]
public async Task Should_stand_aside_from_audit_entirely_when_the_audit_data_is_remote()
{
var (app, services) = await BuildHost(settings =>
{
settings.AuditDataLocation = AuditDataLocation.Remote;
settings.RemoteInstances = [new RemoteInstanceSetting("http://localhost:44444/api")];
});

try
{
using (Assert.EnterMultipleScope())
{
Assert.That(HostsAuditIngestion(services), Is.False, "the audit host ingests, not this primary");
Assert.That(app.Services.GetService<AuditIngestor>(), Is.Null);
Assert.That(app.Services.GetService<ILocalAuditSource>(), Is.Null, "licensing learns about audit from the remote");
Assert.That(app.Services.GetRequiredService<IAuditCountsDataStore>(), Is.TypeOf<EmptyAuditCountsDataStore>(),
"the local source stands aside so the scatter gather treats this instance as a non-participant");
Assert.That(app.Services.GetRequiredService<ISagaHistoryDataStore>(), Is.TypeOf<EmptySagaHistoryDataStore>());
Assert.That(app.Services.GetService<GetSagaByIdApi>(), Is.Not.Null, "the audit routes still answer, from the remote");
}
}
finally
{
await app.DisposeAsync();
}
}

[Test]
public async Task Should_refuse_local_audit_ingestion_alongside_remote_instances()
{
var exception = Assert.ThrowsAsync<Exception>(() => BuildHost(settings =>
settings.RemoteInstances = [new RemoteInstanceSetting("http://localhost:44444/api")]));

Assert.That(exception.Message, Does.Contain("AuditDataLocation"));
}

// The registrations are inspected rather than resolved. A normal primary hosts an NServiceBus
// endpoint, and constructing every hosted service without starting it fails inside the
// transport's receive component.
static bool HostsAuditIngestion(IServiceCollection services) =>
services.Any(descriptor =>
descriptor.ServiceType == typeof(IHostedService) && descriptor.ImplementationType == typeof(AuditIngestion));

async Task<(WebApplication App, IServiceCollection Services)> BuildHost(Action<Settings> customize = null)
{
var settings = await CreateSettings();

customize?.Invoke(settings);

var endpointConfiguration = new EndpointConfiguration(settings.InstanceName);
endpointConfiguration.AssemblyScanner().Disable = true;

var hostBuilder = WebApplication.CreateBuilder();
hostBuilder.AddServiceControl(settings, endpointConfiguration);
hostBuilder.AddServiceControlApi(settings.CorsSettings);

return (hostBuilder.Build(), hostBuilder.Services);
}

async Task<Settings> CreateSettings()
{
var settings = new Settings(TransportIntegration.TypeName, StorageConfiguration.PersistenceType,
CreateLoggingSettings(), forwardErrorMessages: false, errorRetentionPeriod: TimeSpan.FromDays(10))
{
InstanceName = $"AuditComposition.{Guid.NewGuid():n}",
TransportConnectionString = TransportIntegration.ConnectionString,
MaximumConcurrencyLevel = 2,
DisableHealthChecks = true,
AssemblyLoadContextResolver = static _ => AssemblyLoadContext.Default
};

await StorageConfiguration.CustomizeSettings(settings);

return settings;
}

static LoggingSettings CreateLoggingSettings()
{
var logPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(logPath);
return new LoggingSettings(Settings.SettingsRootNamespace, defaultLevel: LogLevel.Debug, logPath: logPath);
}
}
}
Loading
Loading