< Summary

Information
Class: Elsa.Diagnostics.StructuredLogs.Services.StructuredLogSourceRegistry
Assembly: Elsa.Diagnostics.StructuredLogs
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.StructuredLogs/Services/StructuredLogSourceRegistry.cs
Line coverage
97%
Covered lines: 48
Uncovered lines: 1
Coverable lines: 49
Total lines: 92
Line coverage: 97.9%
Branch coverage
83%
Covered branches: 20
Total branches: 24
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Current()100%11100%
MarkSeen(...)81.25%161695%
List()75%44100%
CreateCurrentSource()100%44100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.StructuredLogs/Services/StructuredLogSourceRegistry.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using Elsa.Diagnostics.StructuredLogs.Contracts;
 3using Elsa.Diagnostics.StructuredLogs.Models;
 4using Elsa.Diagnostics.StructuredLogs.Options;
 5using Microsoft.Extensions.Options;
 6
 7namespace Elsa.Diagnostics.StructuredLogs.Services;
 8
 9public class StructuredLogSourceRegistry : IStructuredLogSourceRegistry
 10{
 4411    private readonly ConcurrentDictionary<string, StructuredLogSource> _sources = new();
 12    private readonly StructuredLogsOptions _options;
 13
 4414    public StructuredLogSourceRegistry(IOptions<StructuredLogsOptions> options)
 15    {
 4416        _options = options.Value;
 4417        Current = CreateCurrentSource();
 4418        _sources[Current.Id] = Current;
 4419    }
 20
 21    public event Action<StructuredLogSource>? SourceChanged;
 22
 10023    public StructuredLogSource Current { get; }
 24
 25    public void MarkSeen(string sourceId, DateTimeOffset timestamp)
 26    {
 27        while (true)
 28        {
 10629            if (_sources.TryGetValue(sourceId, out var existing))
 30            {
 5831                if (existing.LastSeen is { } lastSeen && timestamp < lastSeen)
 1032                    return;
 33
 4834                var updated = existing with { LastSeen = timestamp, Status = StructuredLogSourceStatus.Connected };
 4835                if (!_sources.TryUpdate(sourceId, updated, existing))
 36                    continue;
 37
 4838                if (existing.Status != updated.Status)
 039                    SourceChanged?.Invoke(updated);
 40
 4841                return;
 42            }
 43
 4844            var source = new StructuredLogSource
 4845            {
 4846                Id = sourceId,
 4847                DisplayName = sourceId,
 4848                MachineName = sourceId,
 4849                ProcessId = 0,
 4850                LastSeen = timestamp,
 4851                Status = StructuredLogSourceStatus.Connected
 4852            };
 53
 4854            if (!_sources.TryAdd(sourceId, source))
 55                continue;
 56
 4857            SourceChanged?.Invoke(source);
 158            return;
 59        }
 60    }
 61
 62    public IReadOnlyCollection<StructuredLogSource> List()
 63    {
 764        var staleBefore = DateTimeOffset.UtcNow.Subtract(_options.SourceHeartbeatTimeout);
 765        return _sources.Values
 1666            .Select(source => source.LastSeen < staleBefore ? source with { Status = StructuredLogSourceStatus.Stale } :
 1667            .OrderBy(x => x.DisplayName, StringComparer.OrdinalIgnoreCase)
 768            .ToList();
 69    }
 70
 71    private static StructuredLogSource CreateCurrentSource()
 72    {
 4473        var podName = Environment.GetEnvironmentVariable("HOSTNAME");
 4474        var serviceName = Environment.GetEnvironmentVariable("OTEL_SERVICE_NAME") ?? AppDomain.CurrentDomain.FriendlyNam
 4475        var sourceId = $"{Environment.MachineName}-{Environment.ProcessId}";
 4476        var displayName = !string.IsNullOrWhiteSpace(podName) ? podName : sourceId;
 77
 4478        return new()
 4479        {
 4480            Id = sourceId,
 4481            DisplayName = displayName,
 4482            ServiceName = serviceName,
 4483            PodName = podName,
 4484            Namespace = Environment.GetEnvironmentVariable("POD_NAMESPACE"),
 4485            ContainerName = Environment.GetEnvironmentVariable("CONTAINER_NAME"),
 4486            NodeName = Environment.GetEnvironmentVariable("NODE_NAME"),
 4487            StartedAt = DateTimeOffset.UtcNow,
 4488            LastSeen = DateTimeOffset.UtcNow,
 4489            Status = StructuredLogSourceStatus.Connected
 4490        };
 91    }
 92}