| | | 1 | | using Elsa.Diagnostics.StructuredLogs.Contracts; |
| | | 2 | | using Elsa.Diagnostics.StructuredLogs.Models; |
| | | 3 | | using Elsa.Diagnostics.StructuredLogs.Options; |
| | | 4 | | using Elsa.Diagnostics.StructuredLogs.Services; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Diagnostics.StructuredLogs.Providers.InMemory; |
| | | 8 | | |
| | | 9 | | public class InMemoryStructuredLogStore : IStructuredLogStore |
| | | 10 | | { |
| | | 11 | | private readonly RingBuffer<StructuredLogEvent> _recentLogs; |
| | | 12 | | private readonly IStructuredLogSourceRegistry _sourceRegistry; |
| | | 13 | | private readonly StructuredLogsOptions _options; |
| | | 14 | | |
| | 8 | 15 | | public InMemoryStructuredLogStore(IOptions<StructuredLogsOptions> options, IStructuredLogSourceRegistry sourceRegist |
| | | 16 | | { |
| | 8 | 17 | | _options = options.Value; |
| | 8 | 18 | | _sourceRegistry = sourceRegistry; |
| | 8 | 19 | | _recentLogs = new(_options.RecentLogCapacity); |
| | 8 | 20 | | } |
| | | 21 | | |
| | | 22 | | public ValueTask WriteAsync(StructuredLogEvent logEvent, CancellationToken cancellationToken = default) |
| | | 23 | | { |
| | 15 | 24 | | _recentLogs.Add(logEvent); |
| | 15 | 25 | | _sourceRegistry.MarkSeen(logEvent.SourceId, logEvent.ReceivedAt); |
| | 15 | 26 | | return ValueTask.CompletedTask; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public ValueTask<RecentStructuredLogsResult> QueryAsync(StructuredLogFilter filter, CancellationToken cancellationTo |
| | | 30 | | { |
| | 4 | 31 | | var take = Math.Clamp(filter.Take ?? _options.MaxRecentLogQuerySize, 0, _options.MaxRecentLogQuerySize); |
| | 4 | 32 | | var items = _recentLogs |
| | 4 | 33 | | .Snapshot() |
| | 10 | 34 | | .Where(x => StructuredLogFilterEvaluator.Matches(x, filter)) |
| | 8 | 35 | | .OrderBy(x => x.Timestamp) |
| | 8 | 36 | | .ThenBy(x => x.ReceivedAt) |
| | 8 | 37 | | .ThenBy(x => x.SourceId, StringComparer.OrdinalIgnoreCase) |
| | 8 | 38 | | .ThenBy(x => x.Sequence) |
| | 8 | 39 | | .ThenBy(x => x.Id, StringComparer.Ordinal) |
| | 4 | 40 | | .TakeLast(take) |
| | 4 | 41 | | .ToList(); |
| | | 42 | | |
| | 4 | 43 | | return ValueTask.FromResult(new RecentStructuredLogsResult(items, _recentLogs.DroppedCount)); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public ValueTask<IReadOnlyCollection<StructuredLogSource>> ListSourcesAsync(CancellationToken cancellationToken = de |
| | | 47 | | { |
| | 0 | 48 | | return ValueTask.FromResult(_sourceRegistry.List()); |
| | | 49 | | } |
| | | 50 | | } |