< Summary

Information
Class: Elsa.Diagnostics.OpenTelemetry.Services.OpenTelemetryRedactor
Assembly: Elsa.Diagnostics.OpenTelemetry
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.OpenTelemetry/Services/OpenTelemetryRedactor.cs
Line coverage
85%
Covered lines: 34
Uncovered lines: 6
Coverable lines: 40
Total lines: 84
Line coverage: 85%
Branch coverage
80%
Covered branches: 8
Total branches: 10
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%
Redact(...)100%1175%
RedactResource(...)100%11100%
RedactSpan(...)50%2275%
RedactLog(...)50%22100%
RedactDictionary(...)100%22100%
RedactValue(...)75%4480%
RedactPattern(...)100%1150%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.OpenTelemetry/Services/OpenTelemetryRedactor.cs

#LineLine coverage
 1using System.Text.RegularExpressions;
 2using Elsa.Diagnostics.OpenTelemetry.Contracts;
 3using Elsa.Diagnostics.OpenTelemetry.Models;
 4using Elsa.Diagnostics.OpenTelemetry.Options;
 5using Microsoft.Extensions.Options;
 6
 7namespace Elsa.Diagnostics.OpenTelemetry.Services;
 8
 89public class OpenTelemetryRedactor(IOptions<OpenTelemetryDiagnosticsOptions> options) : IOpenTelemetryRedactor
 10{
 11    private const string Redacted = "[Redacted]";
 812    private readonly HashSet<string> _sensitiveNames = options.Value.SensitiveNames.ToHashSet(StringComparer.OrdinalIgno
 813    private readonly IReadOnlyCollection<Regex> _sensitiveTextPatterns = options.Value.SensitiveTextPatterns
 2414        .Select(pattern => new Regex(pattern, RegexOptions.Compiled | RegexOptions.CultureInvariant, options.Value.Sensi
 815        .ToList();
 16
 17    public OpenTelemetryBatch Redact(OpenTelemetryBatch batch)
 18    {
 519        return batch with
 520        {
 521            Resources = batch.Resources.Select(RedactResource).ToList(),
 522            Spans = batch.Spans.Select(RedactSpan).ToList(),
 023            Instruments = batch.Instruments.Select(x => x with { Attributes = RedactDictionary(x.Attributes) }).ToList()
 024            MetricPoints = batch.MetricPoints.Select(x => x with { Attributes = RedactDictionary(x.Attributes) }).ToList
 525            Logs = batch.Logs.Select(RedactLog).ToList()
 526        };
 27    }
 28
 129    private TelemetryResource RedactResource(TelemetryResource resource) => resource with { Attributes = RedactDictionar
 30
 31    private TelemetrySpan RedactSpan(TelemetrySpan span)
 32    {
 233        return span with
 234        {
 235            Name = RedactValue("span.name", span.Name) ?? string.Empty,
 236            StatusDescription = RedactValue("span.status.description", span.StatusDescription),
 237            Attributes = RedactDictionary(span.Attributes),
 038            Events = span.Events.Select(x => x with { Attributes = RedactDictionary(x.Attributes) }).ToList(),
 039            Links = span.Links.Select(x => x with { Attributes = RedactDictionary(x.Attributes) }).ToList()
 240        };
 41    }
 42
 43    private OtlpLogRecord RedactLog(OtlpLogRecord log)
 44    {
 245        return log with
 246        {
 247            Body = RedactValue("log.body", log.Body) ?? string.Empty,
 248            Attributes = RedactDictionary(log.Attributes)
 249        };
 50    }
 51
 52    private Dictionary<string, string?> RedactDictionary(IDictionary<string, string?> values)
 53    {
 554        var result = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
 55
 3256        foreach (var (key, value) in values)
 1157            result[key] = RedactValue(key, value);
 58
 559        return result;
 60    }
 61
 62    private string? RedactValue(string name, string? value)
 63    {
 1764        if (value == null)
 265            return null;
 66
 13267        if (_sensitiveNames.Any(sensitiveName => name.Contains(sensitiveName, StringComparison.OrdinalIgnoreCase)))
 368            return Redacted;
 69
 1270        return _sensitiveTextPatterns.Aggregate(value, RedactPattern);
 71    }
 72
 73    private static string RedactPattern(string value, Regex pattern)
 74    {
 75        try
 76        {
 3677            return pattern.Replace(value, Redacted);
 78        }
 079        catch (RegexMatchTimeoutException)
 80        {
 081            return value;
 82        }
 3683    }
 84}