Доменные события не стреляли с помощью Elastic Apm CaptureSpanC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Доменные события не стреляли с помощью Elastic Apm CaptureSpan

Сообщение Anonymous »

У нас была ошибка Prod, где доменные события не были отправлены, когда _unitofwork.commitasync () были вызваны внутри упругого APM CaptureSpan . Дело в том, что это обычно работает нормально - только этот раз в Prod они не стреляли. Никаких исключений тоже.await _transaction.CaptureSpan("SyncStagedBidSheetWithPublished", ApiConstants.ActionQuery,
async () =>
{
await SyncStagedBidSheetWithPublished(tenantId!.Value);
await _unitOfWork.CommitAsync();
});
< /code>
public class UnitOfWork
{
readonly AppDbContext _context;
readonly IMediator _mediator;
readonly BackgroundTaskChannel _taskChannel;

protected UnitOfWork() { }

public UnitOfWork(AppDbContext context, IMediator mediator, BackgroundTaskChannel taskChannel)
{
_context = context;
_mediator = mediator;
_taskChannel = taskChannel;
}

public async Task CommitAsync() => await CommitWithRetriesAsync();

async Task CommitWithRetriesAsync()
{
var domainEventEntities = _context.GetEntities();

await _mediator.DispatchDomainEventsPreAsync(domainEventEntities);
await _context.SaveChangesAsync();
if (await _mediator.DispatchDomainEventsPosAsync(domainEventEntities))
await _context.SaveChangesAsync();
domainEventEntities.ClearEvents();
}

public async Task CommitToBackgroundTaskQueueAsync()
{
var domainEventEntities = _context.GetEntities();
await _context.SaveChangesAsync();
await _mediator.DispatchDomainEventsPosWithBackgroundTaskQueueAsync(domainEventEntities, _taskChannel, _context);

domainEventEntities.ClearEvents();
}
}
< /code>
static class MediatorExtension
{
public static IList GetEntities(this TContext ctx) where TContext : DbContext, IDisposable
{
var domainEntities = ctx.ChangeTracker
.Entries()
.Where(x => x.Entity.DomainEvents != null && x.Entity.DomainEvents.Any());

return domainEntities.ToList();
}

public static async Task DispatchDomainEventsPreAsync(this IMediator mediator, IList domainEntities)
{
var domainEvents = domainEntities
.SelectMany(x => x.Entity.DomainEvents)
.Where(x => x is IEventPre)
.ToList();

var result = false;
foreach (var domainEvent in domainEvents.OfType())
{
await mediator.Publish(domainEvent);
result = true;
}

return result;
}

public static async Task DispatchDomainEventsPosAsync(this IMediator mediator, IList domainEntities)
{
var domainEvents = domainEntities
.SelectMany(x => x.Entity.DomainEvents)
.Where(x => x is IEventPos)
.ToList();

var result = false;
foreach (var domainEvent in domainEvents.OfType())
{
await mediator.Publish(domainEvent);
result = true;
}

return result;
}

public static async Task DispatchDomainEventsPosWithBackgroundTaskQueueAsync(this IMediator mediator, IEnumerable domainEntities, BackgroundTaskChannel taskChannel, AppDbContext context)
{
var domainEvents = domainEntities.SelectMany(x => x.Entity.DomainEvents).Where(x => x is IBackgroundTask).ToList();

if (domainEvents.Count == 0)
{
return false;
}

foreach (var domainEvent in domainEvents.OfType())
{
domainEvent.ApplicationTenant = context.TenancyContext.Tenant;
await taskChannel.EnqueueAsync(domainEvent);
}

return true;
}

public static void ClearEvents(this IList domainEntities) => domainEntities.ToList().ForEach(x => x.Entity.ClearDomainEvents());
}
< /code>

stack trace < /h2>
Просмотр трассировки стека, происходит много переключения потоков: < / p > < b r / > < c o d e > E l a s t i c . A p m . M o d e l . S p a n i n E n d i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . C o n t i n u a t i o n T a s k F r o m T a s k i n I n n e r I n v o k e i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . T h r e a d P o o l T a s k S c h e d u l e r i n T r y E x e c u t e T a s k I n l i n e i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . T a s k C o n t i n u a t i o n i n I n l i n e I f P o s s i b l e O r E l s e Q u e u e i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . C o n t i n u e W i t h T a s k C o n t i n u a t i o n i n R u n i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . T a s k i n R u n C o n t i n u a t i o n s i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > R J O . O r d e r S e r v i c e . S e r v i c e s . H a n d l e r s . B i d s h e e t . P u b l i s h C o m m a n d H a n d l e r + & l t ; & g t ; c _ _ D i s p l a y C l a s s 1 2 _ 0 + & l t ; & l t ; H a n d l e & g t ; b _ _ 5 & g t ; d i n & l t ; H a n d l e & g t ; b _ _ 5 i n / h o m e / v s t s / w o r k / 1 / s / s r c / S e r v i c e s / O r d e r S e r v i c e / R J O . O r d e r S e r v i c e . S e r v i c e s / H a n d l e r s / B i d s h e e t / P u b l i s h C o m m a n d H a n d l e r . c s a t l i n e 1 5 2 < b r / > S y s t e m . R u n t i m e . C o m p i l e r S e r v i c e s . A s y n c T a s k M e t h o d B u i l d e r ` 1 + A s y n c S t a t e M a c h i n e B o x ` 1 i n E x e c u t i o n C o n t e x t C a l l b a c k i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . R u n t i m e . C o m p i l e r S e r v i c e s . A s y n c T a s k M e t h o d B u i l d e r ` 1 + A s y n c S t a t e M a c h i n e B o x ` 1 i n M o v e N e x t x 1 i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . R u n t i m e . C o m p i l e r S e r v i c e s . T a s k A w a i t e r + & l t ; & g t ; c i n & l t ; O u t p u t W a i t E t w E v e n t s & g t ; b _ _ 1 2 _ 0 i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . A w a i t T a s k C o n t i n u a t i o n i n R u n O r S c h e d u l e A c t i o n i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . T a s k i n R u n C o n t i n u a t i o n s i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > R J O . O r d e r S e r v i c e . P e r s i s t e n c e . U n i t O f W o r k + & l t ; C o m m i t A s y n c & g t ; d _ _ 5 i n C o m m i t A s y n c i n / h o m e / v s t s / w o r k / 1 / s / s r c / S e r v i c e s / O r d e r S e r v i c e / R J O . O r d e r S e r v i c e . P e r s i s t e n c e / U n i t O f W o r k . c s a t l i n e 2 1 < b r / > S y s t e m . R u n t i m e . C o m p i l e r S e r v i c e s . A s y n c T a s k M e t h o d B u i l d e r ` 1 + A s y n c S t a t e M a c h i n e B o x ` 1 i n M o v e N e x t i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . R u n t i m e . C o m p i l e r S e r v i c e s . T a s k A w a i t e r + & l t ; & g t ; c i n & l t ; O u t p u t W a i t E t w E v e n t s & g t ; b _ _ 1 2 _ 0 i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . A w a i t T a s k C o n t i n u a t i o n i n R u n O r S c h e d u l e A c t i o n i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . T a s k i n R u n C o n t i n u a t i o n s i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > R J O . O r d e r S e r v i c e . P e r s i s t e n c e . U n i t O f W o r k + & l t ; C o m m i t W i t h R e t r i e s A s y n c & g t ; d _ _ 6 i n C o m m i t W i t h R e t r i e s A s y n c i n / h o m e / v s t s / w o r k / 1 / s / s r c / S e r v i c e s / O r d e r S e r v i c e / R J O . O r d e r S e r v i c e . P e r s i s t e n c e / U n i t O f W o r k . c s a t l i n e 3 2 < b r / > S y s t e m . R u n t i m e . C o m p i l e r S e r v i c e s . A s y n c T a s k M e t h o d B u i l d e r ` 1 + A s y n c S t a t e M a c h i n e B o x ` 1 i n M o v e N e x t i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . R u n t i m e . C o m p i l e r S e r v i c e s . T a s k A w a i t e r + & l t ; & g t ; c i n & l t ; O u t p u t W a i t E t w E v e n t s & g t ; b _ _ 1 2 _ 0 i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . A w a i t T a s k C o n t i n u a t i o n i n R u n O r S c h e d u l e A c t i o n i n / u s r / s h a r e / d o t n e t / s h a r e d / M i c r o s o f t . N E T C o r e . A p p / 8 . 0 . 1 8 / S y s t e m . P r i v a t e . C o r e L i b . d l l < b r / > S y s t e m . T h r e a d i n g . T a s k s . T a s k in RunContinuations in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
Microsoft.EntityFrameworkCore.DbContext+d__63 in SaveChangesAsync in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.TaskAwaiter+c in b__12_0 in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.Task in RunContinuations in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy+d__7`2 in ExecuteAsync in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.TaskAwaiter+c in b__12_0 in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.Task in RunContinuations in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager+d__115 in SaveChangesAsync in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.TaskAwaiter+c in b__12_0 in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.Task in RunContinuations in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager+d__111 in SaveChangesAsync in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.TaskAwaiter+c in b__12_0 in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.Task in RunContinuations in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor+d__9 in ExecuteAsync in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.TaskAwaiter+c in b__12_0 in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Threading.Tasks.Task in RunContinuations in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch+d__15 in ExecuteAsync in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll
System.Runtime.CompilerServices.TaskAwaiter+c in b__12_0 in /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.18/System.Private.CoreLib.dll

Мне интересно, возможно ли это:
ratemetracker и события домены не защищают тему . При захвате Span вызывает асинхродическое переключение контекста:
  • События доменов.CaptureSpan переключает выполнение на поток B
  • UnitOfWork.CommitAsync() работает на потоке b , но не может получить доступ к событиям домена из потока A
  • Обработка доменных событий молча не проходит
  • Сохранить базу данных, но бизнес -логик будет пропущенным
. Условие во время переключения потока).
Здесь есть упругие документы APM:





APM CaptureSpan приводит к переключению потоков, который связывается с EF Changetracker?var span = _transaction.StartSpan("SyncStagedBidSheetWithPublished", ApiConstants.ActionQuery);
try
{
await SyncStagedBidSheetWithPublished(tenantId!.Value);
await _unitOfWork.CommitAsync();
}
catch (Exception ex)
{
span.CaptureException(ex);
throw;
}
finally
{
span.End();
}


Подробнее здесь: https://stackoverflow.com/questions/797 ... apturespan
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Io.opentelemetry.api.internal.AutoValue_ImmutableSpanContext невозможно привести к классу co.elastic.apm.agent.opentelem
    Anonymous » » в форуме JAVA
    0 Ответы
    29 Просмотры
    Последнее сообщение Anonymous
  • Настройте название SPAN или подтип для DB по сравнению с Elastic APM -метриками
    Anonymous » » в форуме JAVA
    0 Ответы
    9 Просмотры
    Последнее сообщение Anonymous
  • Как я могу обрабатывать Elastic.CommonSchema.NLog с помощью Elastic.NLog.Targets
    Anonymous » » в форуме C#
    0 Ответы
    81 Просмотры
    Последнее сообщение Anonymous
  • Как я могу обрабатывать Elastic.CommonSchema.NLog с помощью Elastic.NLog.Targets
    Anonymous » » в форуме C#
    0 Ответы
    97 Просмотры
    Последнее сообщение Anonymous
  • Как я могу обрабатывать Elastic.CommonSchema.NLog с помощью Elastic.NLog.Targets
    Anonymous » » в форуме C#
    0 Ответы
    122 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C#»