Skip to content

Commit 6afcb4a

Browse files
Merge pull request #562 from TransactionProcessing/task/#550_projection_trace_threshold
Added trace threshold for projection
2 parents 21a8b4e + 6908aa0 commit 6afcb4a

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

TransactionProcessor.ProjectionEngine.Tests/ProjectionHandlerTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
namespace TransactionProcessor.ProjectionEngine.Tests;
22

33
using Dispatchers;
4+
using Microsoft.Extensions.Configuration;
45
using Moq;
56
using Projections;
67
using Repository;
78
using Shared.DomainDrivenDesign.EventSourcing;
9+
using Shared.General;
810
using Shouldly;
911
using State;
1012

1113
public class ProjectionHandlerTests{
1214

15+
public ProjectionHandlerTests(){
16+
Shared.Logger.Logger.Initialise(Shared.Logger.NullLogger.Instance);
17+
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
18+
ConfigurationReader.Initialise(configurationRoot);
19+
}
20+
1321
[Fact]
1422
public async Task ProjectionHandler_Handle_NullEvent_EventHandled(){
1523
Mock<IProjectionStateRepository<MerchantBalanceState>> repo = new Mock<IProjectionStateRepository<MerchantBalanceState>>();
@@ -57,8 +65,6 @@ public async Task ProjectionHandler_Handle_StateNotFoundInRepository_EventHandle
5765
[Fact]
5866
public async Task ProjectionHandler_Handle_StateFoundInRepository_NoChanges_EventHandled()
5967
{
60-
Shared.Logger.Logger.Initialise(Shared.Logger.NullLogger.Instance);
61-
6268
Mock<IProjectionStateRepository<MerchantBalanceState>> repo = new Mock<IProjectionStateRepository<MerchantBalanceState>>();
6369
Mock<IProjection<MerchantBalanceState>> projection = new Mock<IProjection<MerchantBalanceState>>();
6470
Mock<IStateDispatcher<MerchantBalanceState>> stateDispatcher = new Mock<IStateDispatcher<MerchantBalanceState>>();
@@ -80,8 +86,6 @@ public async Task ProjectionHandler_Handle_StateFoundInRepository_NoChanges_Even
8086
[Fact]
8187
public async Task ProjectionHandler_Handle_StateFoundInRepository_ChangesMade_EventHandled()
8288
{
83-
Shared.Logger.Logger.Initialise(Shared.Logger.NullLogger.Instance);
84-
8589
Mock<IProjectionStateRepository<MerchantBalanceState>> repo = new Mock<IProjectionStateRepository<MerchantBalanceState>>();
8690
Mock<IProjection<MerchantBalanceState>> projection = new Mock<IProjection<MerchantBalanceState>>();
8791
Mock<IStateDispatcher<MerchantBalanceState>> stateDispatcher = new Mock<IStateDispatcher<MerchantBalanceState>>();

TransactionProcessor.ProjectionEngine.Tests/TestData.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public static TransactionHasStartedEvent GetTransactionHasStartedEvent(Decimal?
167167
public static IReadOnlyDictionary<String, String> DefaultAppSettings =>
168168
new Dictionary<String, String>
169169
{
170+
["AppSettings:ProjectionTraceThresholdInSeconds"] = "1",
170171
["AppSettings:ClientId"] = "clientId",
171172
["AppSettings:ClientSecret"] = "clientSecret",
172173
["AppSettings:UseConnectionStringConfig"] = "false",

TransactionProcessor.ProjectionEngine/ProjectionHandler/ProjectionHandler.cs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Projections;
88
using Repository;
99
using Shared.DomainDrivenDesign.EventSourcing;
10+
using Shared.General;
1011
using Shared.Logger;
1112
using State;
1213

@@ -25,12 +26,10 @@ public ProjectionHandler(IProjectionStateRepository<TState> projectionStateRepos
2526
this.StateDispatcher = stateDispatcher;
2627
}
2728

28-
public async Task Handle(IDomainEvent @event, CancellationToken cancellationToken)
29-
{
29+
public async Task Handle(IDomainEvent @event, CancellationToken cancellationToken){
3030
if (@event == null) return;
31-
32-
if (this.Projection.ShouldIHandleEvent(@event) == false)
33-
{
31+
32+
if (this.Projection.ShouldIHandleEvent(@event) == false){
3433
return;
3534
}
3635

@@ -40,8 +39,7 @@ public async Task Handle(IDomainEvent @event, CancellationToken cancellationToke
4039
//Load the state from persistence
4140
TState state = await this.ProjectionStateRepository.Load(@event, cancellationToken);
4241

43-
if (state == null)
44-
{
42+
if (state == null){
4543
return;
4644
}
4745

@@ -50,41 +48,40 @@ public async Task Handle(IDomainEvent @event, CancellationToken cancellationToke
5048
builder.Append($"{stopwatch.ElapsedMilliseconds}ms Handling {@event.EventType} Id [{@event.EventId}] for state {state.GetType().Name}|");
5149

5250
TState newState = await this.Projection.Handle(state, @event, cancellationToken);
53-
51+
5452
builder.Append($"{stopwatch.ElapsedMilliseconds}ms After Handle|");
5553

56-
if (newState != state)
57-
{
58-
newState = newState with
59-
{
60-
ChangesApplied = true
61-
};
62-
54+
if (newState != state){
55+
newState = newState with{
56+
ChangesApplied = true
57+
};
58+
6359
// save state
6460
newState = await this.ProjectionStateRepository.Save(newState, @event, cancellationToken);
6561

6662
//Repo might have detected a duplicate event
6763
builder.Append($"{stopwatch.ElapsedMilliseconds}ms After Save|");
6864

69-
if (this.StateDispatcher != null)
70-
{
65+
if (this.StateDispatcher != null){
7166
// Send to anyone else interested
7267
await this.StateDispatcher.Dispatch(newState, @event, cancellationToken);
7368

7469
builder.Append($"{stopwatch.ElapsedMilliseconds}ms After Dispatch|");
7570
}
76-
71+
7772
}
78-
else
79-
{
73+
else{
8074
builder.Append($"{stopwatch.ElapsedMilliseconds}ms No Save required|");
8175
}
8276

8377
stopwatch.Stop();
8478

8579
builder.Insert(0, $"Total time: {stopwatch.ElapsedMilliseconds}ms|");
86-
Logger.LogWarning(builder.ToString());
87-
Logger.LogInformation($"Event Type {@event.EventType} Id [{@event.EventId}] for state {state.GetType().Name} took {stopwatch.ElapsedMilliseconds}ms to process");
8880

81+
Int32 projectionTraceThresholdInSeconds = Int32.Parse(ConfigurationReader.GetValue("AppSettings", "ProjectionTraceThresholdInSeconds"));
82+
if (stopwatch.Elapsed.Seconds > projectionTraceThresholdInSeconds){
83+
Logger.LogWarning(builder.ToString());
84+
Logger.LogInformation($"Event Type {@event.EventType} Id [{@event.EventId}] for state {state.GetType().Name} took {stopwatch.ElapsedMilliseconds}ms to process");
85+
}
8986
}
9087
}

TransactionProcessor/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"ClientId": "serviceClient",
44
"ClientSecret": "d192cbc46d834d0da90e8a9d50ded543",
55
//"SecurityService": "https://127.0.0.1:5001",
6+
"ProjectionTraceThresholdInSeconds": 1,
67
"EventHandlerConfiguration": {
78
"TransactionHasBeenCompletedEvent": [
89
"TransactionProcessor.BusinessLogic.EventHandling.TransactionDomainEventHandler,TransactionProcessor.BusinessLogic"
@@ -105,7 +106,7 @@
105106
}
106107
]
107108
}
108-
},
109+
},
109110
"ConnectionStrings": {
110111
// SQL Server
111112
"TransactionProcessorReadModel": "server=192.168.1.167;user id=sa;password=Sc0tland;database=TransactionProcessorReadModel;Encrypt=false"

0 commit comments

Comments
 (0)