| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using Elsa.Diagnostics.StructuredLogs.Contracts; |
| | | 3 | | using Elsa.Diagnostics.StructuredLogs.Models; |
| | | 4 | | using Elsa.Diagnostics.StructuredLogs.Options; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Diagnostics.StructuredLogs.Services; |
| | | 8 | | |
| | | 9 | | public class StructuredLogSourceRegistry : IStructuredLogSourceRegistry |
| | | 10 | | { |
| | 17 | 11 | | private readonly ConcurrentDictionary<string, StructuredLogSource> _sources = new(); |
| | | 12 | | private readonly StructuredLogsOptions _options; |
| | | 13 | | |
| | 17 | 14 | | public StructuredLogSourceRegistry(IOptions<StructuredLogsOptions> options) |
| | | 15 | | { |
| | 17 | 16 | | _options = options.Value; |
| | 17 | 17 | | Current = CreateCurrentSource(); |
| | 17 | 18 | | _sources[Current.Id] = Current; |
| | 17 | 19 | | } |
| | | 20 | | |
| | | 21 | | public event Action<StructuredLogSource>? SourceChanged; |
| | | 22 | | |
| | 44 | 23 | | public StructuredLogSource Current { get; } |
| | | 24 | | |
| | | 25 | | public void MarkSeen(string sourceId, DateTimeOffset timestamp) |
| | | 26 | | { |
| | | 27 | | while (true) |
| | | 28 | | { |
| | 21 | 29 | | if (_sources.TryGetValue(sourceId, out var existing)) |
| | | 30 | | { |
| | 6 | 31 | | var updated = existing with { LastSeen = timestamp, Status = StructuredLogSourceStatus.Connected }; |
| | 6 | 32 | | if (!_sources.TryUpdate(sourceId, updated, existing)) |
| | | 33 | | continue; |
| | | 34 | | |
| | 6 | 35 | | if (existing.Status != updated.Status) |
| | 0 | 36 | | SourceChanged?.Invoke(updated); |
| | | 37 | | |
| | 6 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | 15 | 41 | | var source = new StructuredLogSource |
| | 15 | 42 | | { |
| | 15 | 43 | | Id = sourceId, |
| | 15 | 44 | | DisplayName = sourceId, |
| | 15 | 45 | | MachineName = sourceId, |
| | 15 | 46 | | ProcessId = 0, |
| | 15 | 47 | | LastSeen = timestamp, |
| | 15 | 48 | | Status = StructuredLogSourceStatus.Connected |
| | 15 | 49 | | }; |
| | | 50 | | |
| | 15 | 51 | | if (!_sources.TryAdd(sourceId, source)) |
| | | 52 | | continue; |
| | | 53 | | |
| | 15 | 54 | | SourceChanged?.Invoke(source); |
| | 1 | 55 | | return; |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public IReadOnlyCollection<StructuredLogSource> List() |
| | | 60 | | { |
| | 3 | 61 | | var staleBefore = DateTimeOffset.UtcNow.Subtract(_options.SourceHeartbeatTimeout); |
| | 3 | 62 | | return _sources.Values |
| | 6 | 63 | | .Select(source => source.LastSeen < staleBefore ? source with { Status = StructuredLogSourceStatus.Stale } : |
| | 6 | 64 | | .OrderBy(x => x.DisplayName, StringComparer.OrdinalIgnoreCase) |
| | 3 | 65 | | .ToList(); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private static StructuredLogSource CreateCurrentSource() |
| | | 69 | | { |
| | 17 | 70 | | var podName = Environment.GetEnvironmentVariable("HOSTNAME"); |
| | 17 | 71 | | var serviceName = Environment.GetEnvironmentVariable("OTEL_SERVICE_NAME") ?? AppDomain.CurrentDomain.FriendlyNam |
| | 17 | 72 | | var sourceId = $"{Environment.MachineName}-{Environment.ProcessId}"; |
| | 17 | 73 | | var displayName = !string.IsNullOrWhiteSpace(podName) ? podName : sourceId; |
| | | 74 | | |
| | 17 | 75 | | return new() |
| | 17 | 76 | | { |
| | 17 | 77 | | Id = sourceId, |
| | 17 | 78 | | DisplayName = displayName, |
| | 17 | 79 | | ServiceName = serviceName, |
| | 17 | 80 | | PodName = podName, |
| | 17 | 81 | | Namespace = Environment.GetEnvironmentVariable("POD_NAMESPACE"), |
| | 17 | 82 | | ContainerName = Environment.GetEnvironmentVariable("CONTAINER_NAME"), |
| | 17 | 83 | | NodeName = Environment.GetEnvironmentVariable("NODE_NAME"), |
| | 17 | 84 | | StartedAt = DateTimeOffset.UtcNow, |
| | 17 | 85 | | LastSeen = DateTimeOffset.UtcNow, |
| | 17 | 86 | | Status = StructuredLogSourceStatus.Connected |
| | 17 | 87 | | }; |
| | | 88 | | } |
| | | 89 | | } |