| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Threading.Channels; |
| | | 3 | | using Elsa.Diagnostics.StructuredLogs.Contracts; |
| | | 4 | | using Elsa.Diagnostics.StructuredLogs.Models; |
| | | 5 | | using Elsa.Diagnostics.StructuredLogs.Options; |
| | | 6 | | using Elsa.Diagnostics.StructuredLogs.Services; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Diagnostics.StructuredLogs.Providers.InMemory; |
| | | 10 | | |
| | 8 | 11 | | public class InMemoryStructuredLogLiveFeed(IOptions<StructuredLogsOptions> options) : IStructuredLogLiveFeed |
| | | 12 | | { |
| | 8 | 13 | | private readonly StructuredLogsOptions _options = options.Value; |
| | 8 | 14 | | private readonly object _subscribersLock = new(); |
| | 8 | 15 | | private readonly Dictionary<Guid, StructuredLogSubscriber> _subscribers = new(); |
| | | 16 | | |
| | | 17 | | public ValueTask PublishAsync(StructuredLogEvent logEvent, CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | | 19 | | List<StructuredLogSubscriber> subscribers; |
| | 15 | 20 | | lock (_subscribersLock) |
| | 15 | 21 | | subscribers = _subscribers.Values.ToList(); |
| | | 22 | | |
| | 38 | 23 | | foreach (var subscriber in subscribers) |
| | 4 | 24 | | subscriber.TryWrite(logEvent, _options.SubscriberChannelCapacity); |
| | | 25 | | |
| | 15 | 26 | | return ValueTask.CompletedTask; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public async IAsyncEnumerable<StructuredLogStreamItem> SubscribeAsync(StructuredLogFilter filter, [EnumeratorCancell |
| | | 30 | | { |
| | 2 | 31 | | var subscriberId = Guid.NewGuid(); |
| | 2 | 32 | | var subscriber = new StructuredLogSubscriber(filter); |
| | | 33 | | |
| | 2 | 34 | | lock (_subscribersLock) |
| | 2 | 35 | | _subscribers[subscriberId] = subscriber; |
| | | 36 | | |
| | | 37 | | try |
| | | 38 | | { |
| | 8 | 39 | | await foreach (var item in subscriber.Channel.Reader.ReadAllAsync(cancellationToken)) |
| | | 40 | | { |
| | 3 | 41 | | subscriber.MarkConsumed(item); |
| | 3 | 42 | | yield return item; |
| | | 43 | | } |
| | 0 | 44 | | } |
| | | 45 | | finally |
| | | 46 | | { |
| | 2 | 47 | | lock (_subscribersLock) |
| | 2 | 48 | | _subscribers.Remove(subscriberId); |
| | | 49 | | } |
| | 2 | 50 | | } |
| | | 51 | | |
| | 2 | 52 | | private sealed class StructuredLogSubscriber(StructuredLogFilter filter) |
| | | 53 | | { |
| | 2 | 54 | | private readonly object _lock = new(); |
| | | 55 | | private int _pendingItemCount; |
| | | 56 | | private long _droppedSinceLastSummary; |
| | | 57 | | private bool _summaryQueued; |
| | | 58 | | |
| | 7 | 59 | | public Channel<StructuredLogStreamItem> Channel { get; } = System.Threading.Channels.Channel.CreateUnbounded<Str |
| | 2 | 60 | | { |
| | 2 | 61 | | SingleReader = true, |
| | 2 | 62 | | SingleWriter = false |
| | 2 | 63 | | }); |
| | | 64 | | |
| | | 65 | | public void TryWrite(StructuredLogEvent logEvent, int capacity) |
| | | 66 | | { |
| | 4 | 67 | | if (!StructuredLogFilterEvaluator.Matches(logEvent, filter)) |
| | 1 | 68 | | return; |
| | | 69 | | |
| | 3 | 70 | | lock (_lock) |
| | | 71 | | { |
| | 3 | 72 | | if (_pendingItemCount >= capacity) |
| | | 73 | | { |
| | 1 | 74 | | _droppedSinceLastSummary++; |
| | 1 | 75 | | QueueDroppedSummaryIfNeeded(); |
| | 1 | 76 | | return; |
| | | 77 | | } |
| | | 78 | | |
| | 2 | 79 | | Channel.Writer.TryWrite(StructuredLogStreamItem.FromLogEvent(logEvent)); |
| | 2 | 80 | | _pendingItemCount++; |
| | 2 | 81 | | } |
| | 3 | 82 | | } |
| | | 83 | | |
| | | 84 | | public void MarkConsumed(StructuredLogStreamItem item) |
| | | 85 | | { |
| | 3 | 86 | | lock (_lock) |
| | | 87 | | { |
| | 3 | 88 | | _pendingItemCount = Math.Max(0, _pendingItemCount - 1); |
| | | 89 | | |
| | 3 | 90 | | if (item.DroppedEvents != null) |
| | | 91 | | { |
| | 1 | 92 | | _summaryQueued = false; |
| | 1 | 93 | | QueueDroppedSummaryIfNeeded(); |
| | | 94 | | } |
| | 3 | 95 | | } |
| | 3 | 96 | | } |
| | | 97 | | |
| | | 98 | | private void QueueDroppedSummaryIfNeeded() |
| | | 99 | | { |
| | 2 | 100 | | if (_summaryQueued || _droppedSinceLastSummary == 0) |
| | 1 | 101 | | return; |
| | | 102 | | |
| | 1 | 103 | | var summary = new StructuredLogDroppedEventSummary(null, _droppedSinceLastSummary, "SubscriberChannelFull"); |
| | 1 | 104 | | _droppedSinceLastSummary = 0; |
| | 1 | 105 | | _summaryQueued = true; |
| | 1 | 106 | | _pendingItemCount++; |
| | 1 | 107 | | Channel.Writer.TryWrite(StructuredLogStreamItem.FromDroppedEvents(summary)); |
| | 1 | 108 | | } |
| | | 109 | | } |
| | | 110 | | } |