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