-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added wrapper for Marten repository instead of the internal logic for…
… tracing
- Loading branch information
1 parent
c063d4d
commit 8c4f674
Showing
4 changed files
with
134 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,45 @@ | ||
using Core.Aggregates; | ||
using Core.OpenTelemetry; | ||
using Core.OptimisticConcurrency; | ||
using Marten; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Core.Marten.Repository; | ||
|
||
public static class Config | ||
{ | ||
public static IServiceCollection AddMartenRepository<T>( | ||
this IServiceCollection services, | ||
bool withAppendScope = true | ||
bool withAppendScope = true, | ||
bool withTelemetry = true | ||
) where T : class, IAggregate | ||
{ | ||
services.AddScoped<MartenRepository<T>, MartenRepository<T>>(); | ||
services.AddScoped<IMartenRepository<T>, MartenRepository<T>>(); | ||
|
||
if (!withAppendScope) | ||
{ | ||
services.AddScoped<IMartenRepository<T>, MartenRepository<T>>(); | ||
} | ||
else | ||
if (withAppendScope) | ||
{ | ||
services.AddScoped<IMartenRepository<T>, MartenRepositoryWithETagDecorator<T>>( | ||
sp => new MartenRepositoryWithETagDecorator<T>( | ||
sp.GetRequiredService<MartenRepository<T>>(), | ||
sp.GetRequiredService<IMartenRepository<T>>(), | ||
sp.GetRequiredService<IExpectedResourceVersionProvider>(), | ||
sp.GetRequiredService<INextResourceVersionProvider>() | ||
) | ||
); | ||
} | ||
|
||
if (withTelemetry) | ||
{ | ||
services.AddScoped<IMartenRepository<T>, MartenRepositoryWithTracingDecorator<T>>( | ||
sp => new MartenRepositoryWithTracingDecorator<T>( | ||
sp.GetRequiredService<IMartenRepository<T>>(), | ||
sp.GetRequiredService<IDocumentSession>(), | ||
sp.GetRequiredService<IActivityScope>(), | ||
sp.GetRequiredService<ILogger<MartenRepositoryWithTracingDecorator<T>>>() | ||
) | ||
); | ||
} | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
Core.Marten/Repository/MartenRepositoryWithTelemetryDecorator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System.Diagnostics; | ||
using Core.Aggregates; | ||
using Core.OpenTelemetry; | ||
using Marten; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Core.Marten.Repository; | ||
|
||
public class MartenRepositoryWithTracingDecorator<T>: IMartenRepository<T> | ||
where T : class, IAggregate | ||
{ | ||
private readonly IMartenRepository<T> inner; | ||
private readonly IDocumentSession documentSession; | ||
private readonly IActivityScope activityScope; | ||
private readonly ILogger<MartenRepositoryWithTracingDecorator<T>> logger; | ||
|
||
public MartenRepositoryWithTracingDecorator( | ||
IMartenRepository<T> inner, | ||
IDocumentSession documentSession, | ||
IActivityScope activityScope, | ||
ILogger<MartenRepositoryWithTracingDecorator<T>> logger | ||
) | ||
{ | ||
this.inner = inner; | ||
this.activityScope = activityScope; | ||
this.logger = logger; | ||
this.documentSession = documentSession; | ||
} | ||
|
||
public Task<T?> Find(Guid id, CancellationToken cancellationToken) => | ||
inner.Find(id, cancellationToken); | ||
|
||
public Task<long> Add(T aggregate, CancellationToken cancellationToken = default) => | ||
activityScope.Run($"MartenRepository/{nameof(Add)}", | ||
(activity, ct) => | ||
{ | ||
PropagateTelemetry(activity); | ||
|
||
return inner.Add(aggregate, ct); | ||
}, | ||
new StartActivityOptions { Tags = { { TelemetryTags.Logic.Entity, typeof(T).Name } } }, | ||
cancellationToken | ||
); | ||
|
||
public Task<long> Update(T aggregate, long? expectedVersion = null, CancellationToken token = default) => | ||
activityScope.Run($"MartenRepository/{nameof(Update)}", | ||
(activity, ct) => | ||
{ | ||
PropagateTelemetry(activity); | ||
|
||
return inner.Update(aggregate, expectedVersion, ct); | ||
}, | ||
new StartActivityOptions { Tags = { { TelemetryTags.Logic.Entity, typeof(T).Name } } }, | ||
token | ||
); | ||
|
||
public Task<long> Delete(T aggregate, long? expectedVersion = null, CancellationToken token = default) => | ||
activityScope.Run($"MartenRepository/{nameof(Delete)}", | ||
(activity, ct) => | ||
{ | ||
PropagateTelemetry(activity); | ||
|
||
return inner.Delete(aggregate, expectedVersion, ct); | ||
}, | ||
new StartActivityOptions { Tags = { { TelemetryTags.Logic.Entity, typeof(T).Name } } }, | ||
token | ||
); | ||
|
||
private void PropagateTelemetry(Activity? activity) | ||
{ | ||
var propagationContext = activity.Propagate(documentSession, InjectTelemetryIntoDocumentSession); | ||
|
||
if (!propagationContext.HasValue) return; | ||
|
||
documentSession.CorrelationId = propagationContext.Value.ActivityContext.TraceId.ToHexString(); | ||
documentSession.CausationId = propagationContext.Value.ActivityContext.SpanId.ToHexString(); | ||
} | ||
|
||
private void InjectTelemetryIntoDocumentSession(IDocumentSession session, string key, string value) | ||
{ | ||
try | ||
{ | ||
session.SetHeader(key, value); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "Failed to inject trace context"); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.