From 7d9bd9e79cebbb47c79f83bc498472513de82f80 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Thu, 8 Dec 2022 19:11:09 +0100 Subject: [PATCH] Added ScrutR to make decorators easier --- Core.EventStoreDB/Repository/Config.cs | 28 ++++--- .../Repository/EventStoreDBRepository.cs | 75 +++++++------------ ...StoreDBRepositoryWithTelemetryDecorator.cs | 45 +++++++++++ Core.Marten/Repository/Config.cs | 14 ++-- Core/Core.csproj | 1 + 5 files changed, 97 insertions(+), 66 deletions(-) create mode 100644 Core.EventStoreDB/Repository/EventStoreDBRepositoryWithTelemetryDecorator.cs diff --git a/Core.EventStoreDB/Repository/Config.cs b/Core.EventStoreDB/Repository/Config.cs index 915eb885a..c6f83e4b7 100644 --- a/Core.EventStoreDB/Repository/Config.cs +++ b/Core.EventStoreDB/Repository/Config.cs @@ -1,4 +1,5 @@ using Core.Aggregates; +using Core.OpenTelemetry; using Core.OptimisticConcurrency; using Microsoft.Extensions.DependencyInjection; @@ -8,26 +9,33 @@ public static class Config { public static IServiceCollection AddEventStoreDBRepository( this IServiceCollection services, - bool withAppendScope = true + bool withAppendScope = true, + bool withTelemetry = true ) where T : class, IAggregate { - services.AddScoped, EventStoreDBRepository>(); + services.AddScoped, EventStoreDBRepository>(); - if (!withAppendScope) + if (withAppendScope) { - services.AddScoped, EventStoreDBRepository>(); - } - else - { - services.AddScoped, EventStoreDBRepositoryWithETagDecorator>( - sp => new EventStoreDBRepositoryWithETagDecorator( - sp.GetRequiredService>(), + services.Decorate>( + (inner, sp) => new EventStoreDBRepositoryWithETagDecorator( + inner, sp.GetRequiredService(), sp.GetRequiredService() ) ); } + if (withTelemetry) + { + services.Decorate>( + (inner, sp) => new EventStoreDBRepositoryWithTelemetryDecorator( + inner, + sp.GetRequiredService() + ) + ); + } + return services; } } diff --git a/Core.EventStoreDB/Repository/EventStoreDBRepository.cs b/Core.EventStoreDB/Repository/EventStoreDBRepository.cs index ee813ec9f..d967f76a2 100644 --- a/Core.EventStoreDB/Repository/EventStoreDBRepository.cs +++ b/Core.EventStoreDB/Repository/EventStoreDBRepository.cs @@ -36,63 +36,42 @@ IActivityScope activityScope cancellationToken ); - public Task Add(T aggregate, CancellationToken token = default) => - activityScope.Run($"{typeof(EventStoreDBRepository).Name}/{nameof(Add)}", - async (activity, ct) => - { - var result = await eventStore.AppendToStreamAsync( - StreamNameMapper.ToStreamId(aggregate.Id), - StreamState.NoStream, - GetEventsToStore(aggregate, TelemetryPropagator.GetPropagationContext(activity)), - cancellationToken: ct - ).ConfigureAwait(false); - return result.NextExpectedStreamRevision.ToUInt64(); - }, - token - ); + public async Task Add(T aggregate, CancellationToken ct = default) + { + var result = await eventStore.AppendToStreamAsync( + StreamNameMapper.ToStreamId(aggregate.Id), + StreamState.NoStream, + GetEventsToStore(aggregate), + cancellationToken: ct + ).ConfigureAwait(false); - public Task Update(T aggregate, ulong? expectedRevision = null, CancellationToken token = default) => - activityScope.Run($"{typeof(EventStoreDBRepository).Name}/{nameof(Update)}", - async (activity, ct) => - { - var eventsToAppend = GetEventsToStore(aggregate, TelemetryPropagator.GetPropagationContext(activity)); - var nextVersion = expectedRevision ?? (ulong)(aggregate.Version - eventsToAppend.Count); + return result.NextExpectedStreamRevision.ToUInt64(); + } - var result = await eventStore.AppendToStreamAsync( - StreamNameMapper.ToStreamId(aggregate.Id), - nextVersion, - eventsToAppend, - cancellationToken: ct - ).ConfigureAwait(false); - return result.NextExpectedStreamRevision.ToUInt64(); - }, - token - ); + public async Task Update(T aggregate, ulong? expectedRevision = null, CancellationToken ct = default) + { + var eventsToAppend = GetEventsToStore(aggregate); + var nextVersion = expectedRevision ?? (ulong)(aggregate.Version - eventsToAppend.Count); - public Task Delete(T aggregate, ulong? expectedRevision = null, CancellationToken token = default) => - activityScope.Run($"{typeof(EventStoreDBRepository).Name}/{nameof(Delete)}", - async (activity, ct) => - { - var eventsToAppend = GetEventsToStore(aggregate, TelemetryPropagator.GetPropagationContext(activity)); - var nextVersion = expectedRevision ?? (ulong)(aggregate.Version - eventsToAppend.Count); + var result = await eventStore.AppendToStreamAsync( + StreamNameMapper.ToStreamId(aggregate.Id), + nextVersion, + eventsToAppend, + cancellationToken: ct + ).ConfigureAwait(false); - var result = await eventStore.AppendToStreamAsync( - StreamNameMapper.ToStreamId(aggregate.Id), - nextVersion, - eventsToAppend, - cancellationToken: ct - ).ConfigureAwait(false); - return result.NextExpectedStreamRevision.ToUInt64(); - }, - token - ); + return result.NextExpectedStreamRevision.ToUInt64(); + } + + public Task Delete(T aggregate, ulong? expectedRevision = null, CancellationToken ct = default) => + Update(aggregate, expectedRevision, ct); - private static List GetEventsToStore(T aggregate, PropagationContext? propagationContext) + private static List GetEventsToStore(T aggregate) { var events = aggregate.DequeueUncommittedEvents(); return events - .Select(@event => @event.ToJsonEventData(propagationContext)) + .Select(@event => @event.ToJsonEventData(TelemetryPropagator.GetPropagationContext())) .ToList(); } } diff --git a/Core.EventStoreDB/Repository/EventStoreDBRepositoryWithTelemetryDecorator.cs b/Core.EventStoreDB/Repository/EventStoreDBRepositoryWithTelemetryDecorator.cs new file mode 100644 index 000000000..45e7d9b89 --- /dev/null +++ b/Core.EventStoreDB/Repository/EventStoreDBRepositoryWithTelemetryDecorator.cs @@ -0,0 +1,45 @@ +using Core.Aggregates; +using Core.OpenTelemetry; +using Microsoft.Extensions.Logging; + +namespace Core.EventStoreDB.Repository; + +public class EventStoreDBRepositoryWithTelemetryDecorator: IEventStoreDBRepository + where T : class, IAggregate +{ + private readonly IEventStoreDBRepository inner; + private readonly IActivityScope activityScope; + + public EventStoreDBRepositoryWithTelemetryDecorator( + IEventStoreDBRepository inner, + IActivityScope activityScope + ) + { + this.inner = inner; + this.activityScope = activityScope; + } + + public Task Find(Guid id, CancellationToken cancellationToken) => + inner.Find(id, cancellationToken); + + public Task Add(T aggregate, CancellationToken cancellationToken = default) => + activityScope.Run($"EventStoreDBRepository/{nameof(Add)}", + (_, ct) => inner.Add(aggregate, ct), + new StartActivityOptions { Tags = { { TelemetryTags.Logic.Entity, typeof(T).Name } } }, + cancellationToken + ); + + public Task Update(T aggregate, ulong? expectedVersion = null, CancellationToken token = default) => + activityScope.Run($"EventStoreDBRepository/{nameof(Update)}", + (_, ct) => inner.Update(aggregate, expectedVersion, ct), + new StartActivityOptions { Tags = { { TelemetryTags.Logic.Entity, typeof(T).Name } } }, + token + ); + + public Task Delete(T aggregate, ulong? expectedVersion = null, CancellationToken token = default) => + activityScope.Run($"EventStoreDBRepository/{nameof(Delete)}", + (_, ct) => inner.Delete(aggregate, expectedVersion, ct), + new StartActivityOptions { Tags = { { TelemetryTags.Logic.Entity, typeof(T).Name } } }, + token + ); +} diff --git a/Core.Marten/Repository/Config.cs b/Core.Marten/Repository/Config.cs index 07eb0fa9d..ea7b12c59 100644 --- a/Core.Marten/Repository/Config.cs +++ b/Core.Marten/Repository/Config.cs @@ -18,21 +18,19 @@ public static class Config services.AddScoped, MartenRepository>(); if (withAppendScope) - { - services.AddScoped, MartenRepositoryWithETagDecorator>( - sp => new MartenRepositoryWithETagDecorator( - sp.GetRequiredService>(), + services.Decorate>( + (inner, sp) => new MartenRepositoryWithETagDecorator( + inner, sp.GetRequiredService(), sp.GetRequiredService() ) ); - } if (withTelemetry) { - services.AddScoped, MartenRepositoryWithTracingDecorator>( - sp => new MartenRepositoryWithTracingDecorator( - sp.GetRequiredService>(), + services.Decorate>( + (inner, sp) => new MartenRepositoryWithTracingDecorator( + inner, sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService>>() diff --git a/Core/Core.csproj b/Core/Core.csproj index 483cdf8ed..d64e535e5 100644 --- a/Core/Core.csproj +++ b/Core/Core.csproj @@ -17,6 +17,7 @@ +