< 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: 46
Uncovered lines: 1
Coverable lines: 47
Total lines: 89
Line coverage: 97.8%
Branch coverage
80%
Covered branches: 16
Total branches: 20
Branch coverage: 80%
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(...)75%121294.44%
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{
 1711    private readonly ConcurrentDictionary<string, StructuredLogSource> _sources = new();
 12    private readonly StructuredLogsOptions _options;
 13
 1714    public StructuredLogSourceRegistry(IOptions<StructuredLogsOptions> options)
 15    {
 1716        _options = options.Value;
 1717        Current = CreateCurrentSource();
 1718        _sources[Current.Id] = Current;
 1719    }
 20
 21    public event Action<StructuredLogSource>? SourceChanged;
 22
 4423    public StructuredLogSource Current { get; }
 24
 25    public void MarkSeen(string sourceId, DateTimeOffset timestamp)
 26    {
 27        while (true)
 28        {
 2129            if (_sources.TryGetValue(sourceId, out var existing))
 30            {
 631                var updated = existing with { LastSeen = timestamp, Status = StructuredLogSourceStatus.Connected };
 632                if (!_sources.TryUpdate(sourceId, updated, existing))
 33                    continue;
 34
 635                if (existing.Status != updated.Status)
 036                    SourceChanged?.Invoke(updated);
 37
 638                return;
 39            }
 40
 1541            var source = new StructuredLogSource
 1542            {
 1543                Id = sourceId,
 1544                DisplayName = sourceId,
 1545                MachineName = sourceId,
 1546                ProcessId = 0,
 1547                LastSeen = timestamp,
 1548                Status = StructuredLogSourceStatus.Connected
 1549            };
 50
 1551            if (!_sources.TryAdd(sourceId, source))
 52                continue;
 53
 1554            SourceChanged?.Invoke(source);
 155            return;
 56        }
 57    }
 58
 59    public IReadOnlyCollection<StructuredLogSource> List()
 60    {
 361        var staleBefore = DateTimeOffset.UtcNow.Subtract(_options.SourceHeartbeatTimeout);
 362        return _sources.Values
 663            .Select(source => source.LastSeen < staleBefore ? source with { Status = StructuredLogSourceStatus.Stale } :
 664            .OrderBy(x => x.DisplayName, StringComparer.OrdinalIgnoreCase)
 365            .ToList();
 66    }
 67
 68    private static StructuredLogSource CreateCurrentSource()
 69    {
 1770        var podName = Environment.GetEnvironmentVariable("HOSTNAME");
 1771        var serviceName = Environment.GetEnvironmentVariable("OTEL_SERVICE_NAME") ?? AppDomain.CurrentDomain.FriendlyNam
 1772        var sourceId = $"{Environment.MachineName}-{Environment.ProcessId}";
 1773        var displayName = !string.IsNullOrWhiteSpace(podName) ? podName : sourceId;
 74
 1775        return new()
 1776        {
 1777            Id = sourceId,
 1778            DisplayName = displayName,
 1779            ServiceName = serviceName,
 1780            PodName = podName,
 1781            Namespace = Environment.GetEnvironmentVariable("POD_NAMESPACE"),
 1782            ContainerName = Environment.GetEnvironmentVariable("CONTAINER_NAME"),
 1783            NodeName = Environment.GetEnvironmentVariable("NODE_NAME"),
 1784            StartedAt = DateTimeOffset.UtcNow,
 1785            LastSeen = DateTimeOffset.UtcNow,
 1786            Status = StructuredLogSourceStatus.Connected
 1787        };
 88    }
 89}