Skip to content

Commit 071147c

Browse files
committed
fix: put an activity with the service handler context for better visibility into service behaviour
1 parent 7300630 commit 071147c

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

src/Whaally.Domain/Abstractions/Service/IServiceHandlerContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Whaally.Domain.Abstractions;
44

5-
public interface IServiceHandlerContext : IContext
5+
public interface IServiceHandlerContext : IContext, IDisposable
66
{
77
/// <summary>
88
/// The optimistic result of the evaluation of this service.

src/Whaally.Domain/DefaultEvaluationAgent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task<IResult<ICommandEnvelope[]>> Evaluate(IServiceEnvelope service
3434
if (serviceEnvelope.Messages.Count() != 1)
3535
throw new ArgumentException($"Expected {nameof(serviceEnvelope)} to contain one message");
3636

37-
var serviceContext = _contextFactory.CreateServiceHandlerContext(serviceEnvelope.Metadata);
37+
using var serviceContext = _contextFactory.CreateServiceHandlerContext(serviceEnvelope.Metadata);
3838

3939
var result = await _domainContext
4040
.GetServiceHandler(serviceEnvelope.Messages.Single().GetType())

src/Whaally.Domain/DomainContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ public virtual async Task<IResult<IEventEnvelope[]>> Trigger(
209209
metadata ?? new ServiceMetadata
210210
{
211211
CreatedAt = DateTimeOffset.UtcNow,
212+
ServiceType = service.GetType(),
212213
ParentContext = activity?.Context
213214
}, service));
214215
}

src/Whaally.Domain/Saga/SagaContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public virtual async Task<IResultBase> EvaluateService(IService service)
6868
new ServiceEnvelope(
6969
new ServiceMetadata
7070
{
71-
CreatedAt = DateTimeOffset.UtcNow
71+
CreatedAt = DateTimeOffset.UtcNow,
72+
ServiceType = service.GetType()
7273
}, service));
7374

7475
if (!result.IsSuccess) return result.ToResult();

src/Whaally.Domain/Service/ServiceHandlerContext.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class ServiceHandlerContext : IServiceHandlerContext
1313
private readonly DomainContext _domainContext;
1414

1515
private readonly Activity? _activity;
16-
// ToDo: Add an activity here to track service evaluation
1716

1817
public ServiceHandlerContext(IServiceProvider services, IServiceMetadata metadata)
1918
{
@@ -57,6 +56,7 @@ public virtual async Task<IResultBase> EvaluateService<TService>(TService servic
5756
new ServiceMetadata
5857
{
5958
CreatedAt = DateTimeOffset.UtcNow,
59+
ServiceType = service.GetType(),
6060
Attributes = new Dictionary<string, object>(Attributes),
6161
ParentContext = ParentContext
6262
}, service));
@@ -79,6 +79,8 @@ public virtual async Task<IResultBase> EvaluateService<TService>(TService servic
7979
/// <param name="command">The command to add to the current commands basket</param>
8080
public virtual void StageCommands(string aggregateId, params ICommand[] commands)
8181
{
82+
foreach (var command in commands) _activity?.AddEvent(new ActivityEvent($"Stage {command.GetType().Name}"));
83+
8284
var aggregateType = _domainContext.GetCommonAggregateType(commands);
8385

8486
if (!_envelopes.TryGetValue(aggregateId, out var envelope))
@@ -102,4 +104,10 @@ public virtual void StageCommands(string aggregateId, params ICommand[] commands
102104
_envelopes.Remove(aggregateId);
103105
_envelopes.Add(aggregateId, envelope);
104106
}
107+
108+
public void Dispose()
109+
{
110+
_evaluationAgent.Dispose();
111+
_activity?.Dispose();
112+
}
105113
}

0 commit comments

Comments
 (0)