| | | 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 | | |
| | | 11 | | public class InMemoryStructuredLogProvider : IStructuredLogStreamProvider |
| | | 12 | | { |
| | | 13 | | private readonly RingBuffer<StructuredLogEvent> _recentLogs; |
| | | 14 | | private readonly IStructuredLogSourceRegistry _sourceRegistry; |
| | | 15 | | private readonly StructuredLogsOptions _options; |
| | 6 | 16 | | private readonly object _subscribersLock = new(); |
| | 6 | 17 | | private readonly Dictionary<Guid, StructuredLogSubscriber> _subscribers = new(); |
| | | 18 | | |
| | 6 | 19 | | public InMemoryStructuredLogProvider(IOptions<StructuredLogsOptions> options, IStructuredLogSourceRegistry sourceReg |
| | | 20 | | { |
| | 6 | 21 | | _options = options.Value; |
| | 6 | 22 | | _sourceRegistry = sourceRegistry; |
| | 6 | 23 | | _recentLogs = new(_options.RecentLogCapacity); |
| | 6 | 24 | | } |
| | | 25 | | |
| | | 26 | | public ValueTask PublishAsync(StructuredLogEvent logEvent, CancellationToken cancellationToken = default) |
| | | 27 | | { |
| | 15 | 28 | | _recentLogs.Add(logEvent); |
| | 15 | 29 | | _sourceRegistry.MarkSeen(logEvent.SourceId, logEvent.ReceivedAt); |
| | | 30 | | |
| | | 31 | | List<StructuredLogSubscriber> subscribers; |
| | 15 | 32 | | lock (_subscribersLock) |
| | 15 | 33 | | subscribers = _subscribers.Values.ToList(); |
| | | 34 | | |
| | 38 | 35 | | foreach (var subscriber in subscribers) |
| | 4 | 36 | | subscriber.TryWrite(logEvent, _options.SubscriberChannelCapacity); |
| | | 37 | | |
| | 15 | 38 | | return ValueTask.CompletedTask; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public ValueTask<RecentStructuredLogsResult> GetRecentAsync(StructuredLogFilter filter, CancellationToken cancellati |
| | | 42 | | { |
| | 4 | 43 | | var take = Math.Clamp(filter.Take ?? _options.MaxRecentLogQuerySize, 0, _options.MaxRecentLogQuerySize); |
| | 4 | 44 | | var items = _recentLogs |
| | 4 | 45 | | .Snapshot() |
| | 10 | 46 | | .Where(x => StructuredLogFilterEvaluator.Matches(x, filter)) |
| | 8 | 47 | | .OrderBy(x => x.Timestamp) |
| | 8 | 48 | | .ThenBy(x => x.ReceivedAt) |
| | 8 | 49 | | .ThenBy(x => x.SourceId, StringComparer.OrdinalIgnoreCase) |
| | 8 | 50 | | .ThenBy(x => x.Sequence) |
| | 8 | 51 | | .ThenBy(x => x.Id, StringComparer.Ordinal) |
| | 4 | 52 | | .TakeLast(take) |
| | 4 | 53 | | .ToList(); |
| | | 54 | | |
| | 4 | 55 | | return ValueTask.FromResult(new RecentStructuredLogsResult(items, _recentLogs.DroppedCount)); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public async IAsyncEnumerable<StructuredLogEvent> SubscribeAsync(StructuredLogFilter filter, [EnumeratorCancellation |
| | | 59 | | { |
| | 3 | 60 | | await foreach (var item in SubscribeWithDroppedEventsAsync(filter, cancellationToken)) |
| | | 61 | | { |
| | 1 | 62 | | if (item.LogEvent != null) |
| | 1 | 63 | | yield return item.LogEvent; |
| | | 64 | | } |
| | 1 | 65 | | } |
| | | 66 | | |
| | | 67 | | public async IAsyncEnumerable<StructuredLogStreamItem> SubscribeWithDroppedEventsAsync(StructuredLogFilter filter, [ |
| | | 68 | | { |
| | 2 | 69 | | var subscriberId = Guid.NewGuid(); |
| | 2 | 70 | | var subscriber = new StructuredLogSubscriber(filter); |
| | | 71 | | |
| | 2 | 72 | | lock (_subscribersLock) |
| | 2 | 73 | | _subscribers[subscriberId] = subscriber; |
| | | 74 | | |
| | | 75 | | try |
| | | 76 | | { |
| | 8 | 77 | | await foreach (var item in subscriber.Channel.Reader.ReadAllAsync(cancellationToken)) |
| | | 78 | | { |
| | 3 | 79 | | subscriber.MarkConsumed(item); |
| | 3 | 80 | | yield return item; |
| | | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | finally |
| | | 84 | | { |
| | 2 | 85 | | lock (_subscribersLock) |
| | 2 | 86 | | _subscribers.Remove(subscriberId); |
| | | 87 | | } |
| | 2 | 88 | | } |
| | | 89 | | |
| | | 90 | | public ValueTask<IReadOnlyCollection<StructuredLogSource>> ListSourcesAsync(CancellationToken cancellationToken = de |
| | | 91 | | { |
| | 0 | 92 | | return ValueTask.FromResult(_sourceRegistry.List()); |
| | | 93 | | } |
| | | 94 | | |
| | 2 | 95 | | private sealed class StructuredLogSubscriber(StructuredLogFilter filter) |
| | | 96 | | { |
| | 2 | 97 | | private readonly object _lock = new(); |
| | | 98 | | private int _pendingItemCount; |
| | | 99 | | private long _droppedSinceLastSummary; |
| | | 100 | | private bool _summaryQueued; |
| | | 101 | | |
| | 7 | 102 | | public Channel<StructuredLogStreamItem> Channel { get; } = System.Threading.Channels.Channel.CreateUnbounded<Str |
| | 2 | 103 | | { |
| | 2 | 104 | | SingleReader = true, |
| | 2 | 105 | | SingleWriter = false |
| | 2 | 106 | | }); |
| | | 107 | | |
| | | 108 | | public void TryWrite(StructuredLogEvent logEvent, int capacity) |
| | | 109 | | { |
| | 4 | 110 | | if (!StructuredLogFilterEvaluator.Matches(logEvent, filter)) |
| | 1 | 111 | | return; |
| | | 112 | | |
| | 3 | 113 | | lock (_lock) |
| | | 114 | | { |
| | 3 | 115 | | if (_pendingItemCount >= capacity) |
| | | 116 | | { |
| | 1 | 117 | | _droppedSinceLastSummary++; |
| | 1 | 118 | | QueueDroppedSummaryIfNeeded(); |
| | 1 | 119 | | return; |
| | | 120 | | } |
| | | 121 | | |
| | 2 | 122 | | Channel.Writer.TryWrite(StructuredLogStreamItem.FromLogEvent(logEvent)); |
| | 2 | 123 | | _pendingItemCount++; |
| | 2 | 124 | | } |
| | 3 | 125 | | } |
| | | 126 | | |
| | | 127 | | public void MarkConsumed(StructuredLogStreamItem item) |
| | | 128 | | { |
| | 3 | 129 | | lock (_lock) |
| | | 130 | | { |
| | 3 | 131 | | _pendingItemCount = Math.Max(0, _pendingItemCount - 1); |
| | | 132 | | |
| | 3 | 133 | | if (item.DroppedEvents != null) |
| | | 134 | | { |
| | 1 | 135 | | _summaryQueued = false; |
| | 1 | 136 | | QueueDroppedSummaryIfNeeded(); |
| | | 137 | | } |
| | 3 | 138 | | } |
| | 3 | 139 | | } |
| | | 140 | | |
| | | 141 | | private void QueueDroppedSummaryIfNeeded() |
| | | 142 | | { |
| | 2 | 143 | | if (_summaryQueued || _droppedSinceLastSummary == 0) |
| | 1 | 144 | | return; |
| | | 145 | | |
| | 1 | 146 | | var summary = new StructuredLogDroppedEventSummary(null, _droppedSinceLastSummary, "SubscriberChannelFull"); |
| | 1 | 147 | | _droppedSinceLastSummary = 0; |
| | 1 | 148 | | _summaryQueued = true; |
| | 1 | 149 | | _pendingItemCount++; |
| | 1 | 150 | | Channel.Writer.TryWrite(StructuredLogStreamItem.FromDroppedEvents(summary)); |
| | 1 | 151 | | } |
| | | 152 | | } |
| | | 153 | | } |