| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using Elsa.Diagnostics.OpenTelemetry.Contracts; |
| | | 3 | | using Elsa.Diagnostics.OpenTelemetry.Models; |
| | | 4 | | using Elsa.Diagnostics.OpenTelemetry.Options; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Diagnostics.OpenTelemetry.Services; |
| | | 8 | | |
| | 8 | 9 | | public class OpenTelemetryRedactor(IOptions<OpenTelemetryDiagnosticsOptions> options) : IOpenTelemetryRedactor |
| | | 10 | | { |
| | | 11 | | private const string Redacted = "[Redacted]"; |
| | 8 | 12 | | private readonly HashSet<string> _sensitiveNames = options.Value.SensitiveNames.ToHashSet(StringComparer.OrdinalIgno |
| | 8 | 13 | | private readonly IReadOnlyCollection<Regex> _sensitiveTextPatterns = options.Value.SensitiveTextPatterns |
| | 24 | 14 | | .Select(pattern => new Regex(pattern, RegexOptions.Compiled | RegexOptions.CultureInvariant, options.Value.Sensi |
| | 8 | 15 | | .ToList(); |
| | | 16 | | |
| | | 17 | | public OpenTelemetryBatch Redact(OpenTelemetryBatch batch) |
| | | 18 | | { |
| | 5 | 19 | | return batch with |
| | 5 | 20 | | { |
| | 5 | 21 | | Resources = batch.Resources.Select(RedactResource).ToList(), |
| | 5 | 22 | | Spans = batch.Spans.Select(RedactSpan).ToList(), |
| | 0 | 23 | | Instruments = batch.Instruments.Select(x => x with { Attributes = RedactDictionary(x.Attributes) }).ToList() |
| | 0 | 24 | | MetricPoints = batch.MetricPoints.Select(x => x with { Attributes = RedactDictionary(x.Attributes) }).ToList |
| | 5 | 25 | | Logs = batch.Logs.Select(RedactLog).ToList() |
| | 5 | 26 | | }; |
| | | 27 | | } |
| | | 28 | | |
| | 1 | 29 | | private TelemetryResource RedactResource(TelemetryResource resource) => resource with { Attributes = RedactDictionar |
| | | 30 | | |
| | | 31 | | private TelemetrySpan RedactSpan(TelemetrySpan span) |
| | | 32 | | { |
| | 2 | 33 | | return span with |
| | 2 | 34 | | { |
| | 2 | 35 | | Name = RedactValue("span.name", span.Name) ?? string.Empty, |
| | 2 | 36 | | StatusDescription = RedactValue("span.status.description", span.StatusDescription), |
| | 2 | 37 | | Attributes = RedactDictionary(span.Attributes), |
| | 0 | 38 | | Events = span.Events.Select(x => x with { Attributes = RedactDictionary(x.Attributes) }).ToList(), |
| | 0 | 39 | | Links = span.Links.Select(x => x with { Attributes = RedactDictionary(x.Attributes) }).ToList() |
| | 2 | 40 | | }; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | private OtlpLogRecord RedactLog(OtlpLogRecord log) |
| | | 44 | | { |
| | 2 | 45 | | return log with |
| | 2 | 46 | | { |
| | 2 | 47 | | Body = RedactValue("log.body", log.Body) ?? string.Empty, |
| | 2 | 48 | | Attributes = RedactDictionary(log.Attributes) |
| | 2 | 49 | | }; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | private Dictionary<string, string?> RedactDictionary(IDictionary<string, string?> values) |
| | | 53 | | { |
| | 5 | 54 | | var result = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase); |
| | | 55 | | |
| | 32 | 56 | | foreach (var (key, value) in values) |
| | 11 | 57 | | result[key] = RedactValue(key, value); |
| | | 58 | | |
| | 5 | 59 | | return result; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private string? RedactValue(string name, string? value) |
| | | 63 | | { |
| | 17 | 64 | | if (value == null) |
| | 2 | 65 | | return null; |
| | | 66 | | |
| | 132 | 67 | | if (_sensitiveNames.Any(sensitiveName => name.Contains(sensitiveName, StringComparison.OrdinalIgnoreCase))) |
| | 3 | 68 | | return Redacted; |
| | | 69 | | |
| | 12 | 70 | | return _sensitiveTextPatterns.Aggregate(value, RedactPattern); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private static string RedactPattern(string value, Regex pattern) |
| | | 74 | | { |
| | | 75 | | try |
| | | 76 | | { |
| | 36 | 77 | | return pattern.Replace(value, Redacted); |
| | | 78 | | } |
| | 0 | 79 | | catch (RegexMatchTimeoutException) |
| | | 80 | | { |
| | 0 | 81 | | return value; |
| | | 82 | | } |
| | 36 | 83 | | } |
| | | 84 | | } |