Skip to content

Commit b54b47c

Browse files
committed
Add a metrics wrapper
1 parent d759847 commit b54b47c

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace ServiceControl.Audit.Auditing;
2+
3+
using System.Diagnostics.Metrics;
4+
5+
public class AuditIngestionMetrics
6+
{
7+
public AuditIngestionMetrics(IMeterFactory meterFactory)
8+
{
9+
var meter = meterFactory.Create(MeterName, MeterVersion);
10+
forwardedMessagesCounter = meter.CreateCounter<long>(CreateInstrumentName("forwarded"), description: "Audit ingestion forwarded message count");
11+
}
12+
13+
public void IncrementMessagesForwarded(int count) => forwardedMessagesCounter.Add(count);
14+
15+
static string CreateInstrumentName(string instrumentName) => $"sc.audit.ingestion.{instrumentName}".ToLower();
16+
17+
readonly Counter<long> forwardedMessagesCounter;
18+
19+
const string MeterName = "Particular.ServiceControl.Audit";
20+
const string MeterVersion = "0.1.0";
21+
}

src/ServiceControl.Audit/Auditing/AuditIngestor.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Diagnostics.Metrics;
65
using System.Linq;
76
using System.Threading.Tasks;
87
using Infrastructure.Settings;
@@ -14,8 +13,7 @@
1413
using Persistence.UnitOfWork;
1514
using Recoverability;
1615
using SagaAudit;
17-
using ServiceControl.Infrastructure;
18-
using ServiceControl.Transports;
16+
using Transports;
1917

2018
public class AuditIngestor
2119
{
@@ -26,11 +24,13 @@ public AuditIngestor(
2624
IEnumerable<IEnrichImportedAuditMessages> auditEnrichers, // allows extending message enrichers with custom enrichers registered in the DI container
2725
IMessageSession messageSession,
2826
Lazy<IMessageDispatcher> messageDispatcher,
29-
ITransportCustomization transportCustomization
27+
ITransportCustomization transportCustomization,
28+
AuditIngestionMetrics metrics
3029
)
3130
{
3231
this.settings = settings;
3332
this.messageDispatcher = messageDispatcher;
33+
this.metrics = metrics;
3434
var enrichers = new IEnrichImportedAuditMessages[] { new MessageTypeEnricher(), new EnrichWithTrackingIds(), new ProcessingStatisticsEnricher(), new DetectNewEndpointsFromAuditImportsEnricher(endpointInstanceMonitoring), new DetectSuccessfulRetriesEnricher(), new SagaRelationshipsEnricher() }.Concat(auditEnrichers).ToArray();
3535

3636
logQueueAddress = transportCustomization.ToTransportQualifiedQueueName(settings.AuditLogQueue);
@@ -52,7 +52,7 @@ public async Task Ingest(List<MessageContext> contexts)
5252
if (settings.ForwardAuditMessages)
5353
{
5454
await Forward(stored, logQueueAddress);
55-
forwardedMessagesCounter.Add(stored.Count);
55+
metrics.IncrementMessagesForwarded(stored.Count);
5656
}
5757

5858
foreach (var context in contexts)
@@ -131,8 +131,8 @@ public async Task VerifyCanReachForwardingAddress()
131131
readonly AuditPersister auditPersister;
132132
readonly Settings settings;
133133
readonly Lazy<IMessageDispatcher> messageDispatcher;
134+
readonly AuditIngestionMetrics metrics;
134135
readonly string logQueueAddress;
135-
readonly Counter<long> forwardedMessagesCounter = Telemetry.Meter.CreateCounter<long>(Telemetry.CreateInstrumentName("ingestion", "forwarded"), description: "Audit ingestion forwarded message count");
136136

137137
static readonly ILog Log = LogManager.GetLogger<AuditIngestor>();
138138
}

src/ServiceControl.Audit/HostApplicationBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public static void AddServiceControlAudit(this IHostApplicationBuilder builder,
7171
NServiceBusFactory.Configure(settings, transportCustomization, transportSettings, onCriticalError, configuration);
7272
builder.UseNServiceBus(configuration);
7373

74+
services.AddSingleton<AuditIngestionMetrics>();
75+
7476
if (!string.IsNullOrEmpty(settings.OtlpEndpointUrl))
7577
{
7678
if (!Uri.TryCreate(settings.OtlpEndpointUrl, UriKind.Absolute, out var otelMetricsUri))

0 commit comments

Comments
 (0)