| | | 1 | | using Elsa.Diagnostics.OpenTelemetry.Contracts; |
| | | 2 | | using Elsa.Diagnostics.OpenTelemetry.Models; |
| | | 3 | | using Elsa.Diagnostics.OpenTelemetry.Options; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Diagnostics.OpenTelemetry.Providers.InMemory; |
| | | 7 | | |
| | | 8 | | public class InMemoryOpenTelemetryStore : IOpenTelemetryStore |
| | | 9 | | { |
| | | 10 | | private readonly IOpenTelemetrySourceRegistry _sourceRegistry; |
| | | 11 | | private readonly OpenTelemetryDiagnosticsOptions _options; |
| | | 12 | | private readonly int _traceCapacity; |
| | | 13 | | private readonly int _spanCapacity; |
| | | 14 | | private readonly int _metricPointCapacity; |
| | | 15 | | private readonly int _logRecordCapacity; |
| | | 16 | | private readonly RingBuffer<TelemetryTrace> _traces; |
| | | 17 | | private readonly RingBuffer<TelemetrySpan> _spans; |
| | | 18 | | private readonly RingBuffer<MetricPoint> _metricPoints; |
| | | 19 | | private readonly RingBuffer<OtlpLogRecord> _logs; |
| | 16 | 20 | | private readonly object _instrumentLock = new(); |
| | 16 | 21 | | private readonly Dictionary<string, MetricInstrument> _instruments = new(StringComparer.OrdinalIgnoreCase); |
| | | 22 | | |
| | 16 | 23 | | public InMemoryOpenTelemetryStore(IOptions<OpenTelemetryDiagnosticsOptions> options, IOpenTelemetrySourceRegistry so |
| | | 24 | | { |
| | 16 | 25 | | _options = options.Value; |
| | 16 | 26 | | _sourceRegistry = sourceRegistry; |
| | 16 | 27 | | _traceCapacity = ClampCapacity(_options.TraceCapacity); |
| | 16 | 28 | | _spanCapacity = ClampCapacity(_options.SpanCapacity); |
| | 16 | 29 | | _metricPointCapacity = ClampCapacity(_options.MetricPointCapacity); |
| | 16 | 30 | | _logRecordCapacity = ClampCapacity(_options.LogRecordCapacity); |
| | 16 | 31 | | _traces = new(_traceCapacity); |
| | 16 | 32 | | _spans = new(_spanCapacity); |
| | 16 | 33 | | _metricPoints = new(_metricPointCapacity); |
| | 16 | 34 | | _logs = new(_logRecordCapacity); |
| | 16 | 35 | | } |
| | | 36 | | |
| | | 37 | | public ValueTask WriteAsync(OpenTelemetryBatch batch, CancellationToken cancellationToken = default) |
| | | 38 | | { |
| | 62 | 39 | | foreach (var resource in batch.Resources) |
| | 15 | 40 | | _sourceRegistry.MarkSeen(resource); |
| | | 41 | | |
| | 52 | 42 | | foreach (var trace in batch.Traces) |
| | 10 | 43 | | _traces.Add(trace); |
| | | 44 | | |
| | 42 | 45 | | foreach (var span in batch.Spans) |
| | 5 | 46 | | _spans.Add(span); |
| | | 47 | | |
| | 16 | 48 | | lock (_instrumentLock) |
| | | 49 | | { |
| | 42 | 50 | | foreach (var instrument in batch.Instruments) |
| | 5 | 51 | | _instruments[instrument.Id] = instrument; |
| | | 52 | | } |
| | | 53 | | |
| | 42 | 54 | | foreach (var point in batch.MetricPoints) |
| | 5 | 55 | | _metricPoints.Add(point); |
| | | 56 | | |
| | 42 | 57 | | foreach (var log in batch.Logs) |
| | 5 | 58 | | _logs.Add(log); |
| | | 59 | | |
| | 16 | 60 | | return ValueTask.CompletedTask; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public ValueTask<OpenTelemetryResourceResult> QueryResourcesAsync(OpenTelemetryResourceFilter filter, CancellationTo |
| | | 64 | | { |
| | 2 | 65 | | var take = ClampTake(filter.Take); |
| | 2 | 66 | | var items = _sourceRegistry.List() |
| | 4 | 67 | | .Where(x => string.IsNullOrWhiteSpace(filter.ServiceName) || string.Equals(x.ServiceName, filter.ServiceName |
| | 4 | 68 | | .Where(x => filter.Status == null || x.Status == filter.Status) |
| | 4 | 69 | | .Where(x => string.IsNullOrWhiteSpace(filter.Search) || Matches(x.ServiceName, filter.Search) || Matches(x.I |
| | 4 | 70 | | .OrderByDescending(x => x.LastSeen) |
| | 4 | 71 | | .ThenBy(x => x.ServiceName, StringComparer.OrdinalIgnoreCase) |
| | 2 | 72 | | .Take(take) |
| | 2 | 73 | | .ToList(); |
| | | 74 | | |
| | 2 | 75 | | return ValueTask.FromResult(new OpenTelemetryResourceResult(items, _sourceRegistry.DroppedCount)); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | public ValueTask<OpenTelemetryTraceResult> QueryTracesAsync(OpenTelemetryTraceFilter filter, CancellationToken cance |
| | | 79 | | { |
| | 4 | 80 | | var take = ClampTake(filter.Take); |
| | 4 | 81 | | var serviceResourceIds = ResolveResourceIds(filter.ServiceName); |
| | 4 | 82 | | var items = _traces.Snapshot() |
| | 7 | 83 | | .Where(x => string.IsNullOrWhiteSpace(filter.TraceId) || Matches(x.TraceId, filter.TraceId)) |
| | 7 | 84 | | .Where(x => filter.Status == null || x.Status == filter.Status) |
| | 7 | 85 | | .Where(x => filter.From == null || x.StartTime >= filter.From) |
| | 7 | 86 | | .Where(x => filter.To == null || x.StartTime <= filter.To) |
| | 7 | 87 | | .Where(x => string.IsNullOrWhiteSpace(filter.WorkflowInstanceId) || x.WorkflowInstanceIds.Any(id => Matches( |
| | 7 | 88 | | .Where(x => string.IsNullOrWhiteSpace(filter.ResourceId) || x.ResourceIds.Contains(filter.ResourceId, String |
| | 7 | 89 | | .Where(x => serviceResourceIds == null || x.ResourceIds.Any(serviceResourceIds.Contains)) |
| | 6 | 90 | | .Where(x => string.IsNullOrWhiteSpace(filter.Search) || Matches(x.TraceId, filter.Search) || Matches(x.Name, |
| | 6 | 91 | | .OrderBy(x => x.StartTime) |
| | 6 | 92 | | .GroupBy(x => x.TraceId, StringComparer.OrdinalIgnoreCase) |
| | 5 | 93 | | .Select(x => x.Last()) |
| | 4 | 94 | | .TakeLast(take) |
| | 4 | 95 | | .ToList(); |
| | | 96 | | |
| | 4 | 97 | | return ValueTask.FromResult(new OpenTelemetryTraceResult(items, _traces.DroppedCount)); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | public ValueTask<OpenTelemetryTraceDetail?> GetTraceAsync(string traceId, CancellationToken cancellationToken = defa |
| | | 101 | | { |
| | 4 | 102 | | var trace = _traces.Snapshot().LastOrDefault(x => string.Equals(x.TraceId, traceId, StringComparison.OrdinalIgno |
| | 2 | 103 | | if (trace == null) |
| | 0 | 104 | | return ValueTask.FromResult<OpenTelemetryTraceDetail?>(null); |
| | | 105 | | |
| | 2 | 106 | | var spans = _spans.Snapshot() |
| | 4 | 107 | | .Where(x => string.Equals(x.TraceId, traceId, StringComparison.OrdinalIgnoreCase)) |
| | 4 | 108 | | .OrderBy(x => x.StartTime) |
| | 4 | 109 | | .ThenBy(x => x.SpanId, StringComparer.OrdinalIgnoreCase) |
| | 2 | 110 | | .ToList(); |
| | 2 | 111 | | var resourceIds = trace.ResourceIds.ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | 2 | 112 | | var resources = _sourceRegistry.List() |
| | 2 | 113 | | .Where(x => resourceIds.Contains(x.Id)) |
| | 0 | 114 | | .OrderBy(x => x.ServiceName, StringComparer.OrdinalIgnoreCase) |
| | 0 | 115 | | .ThenBy(x => x.Id, StringComparer.OrdinalIgnoreCase) |
| | 2 | 116 | | .ToList(); |
| | 2 | 117 | | var logs = _logs.Snapshot() |
| | 1 | 118 | | .Where(x => string.Equals(x.TraceId, traceId, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 119 | | .OrderBy(x => x.Timestamp) |
| | 0 | 120 | | .ThenBy(x => x.Id, StringComparer.OrdinalIgnoreCase) |
| | 2 | 121 | | .ToList(); |
| | | 122 | | |
| | 2 | 123 | | return ValueTask.FromResult<OpenTelemetryTraceDetail?>(new OpenTelemetryTraceDetail(trace, spans, resources, log |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | public ValueTask<OpenTelemetryMetricResult> QueryMetricsAsync(OpenTelemetryMetricFilter filter, CancellationToken ca |
| | | 127 | | { |
| | 2 | 128 | | var take = ClampTake(filter.Take); |
| | 2 | 129 | | var serviceResourceIds = ResolveResourceIds(filter.ServiceName); |
| | | 130 | | |
| | | 131 | | Dictionary<string, MetricInstrument> instruments; |
| | 2 | 132 | | lock (_instrumentLock) |
| | 2 | 133 | | instruments = new(_instruments, StringComparer.OrdinalIgnoreCase); |
| | | 134 | | |
| | 2 | 135 | | var instrumentFilterIds = string.IsNullOrWhiteSpace(filter.InstrumentName) |
| | 2 | 136 | | ? null |
| | 2 | 137 | | : instruments.Values |
| | 4 | 138 | | .Where(x => Matches(x.Name, filter.InstrumentName)) |
| | 2 | 139 | | .Select(x => x.Id) |
| | 2 | 140 | | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 141 | | |
| | 2 | 142 | | var points = _metricPoints.Snapshot() |
| | 4 | 143 | | .Where(x => string.IsNullOrWhiteSpace(filter.ResourceId) || string.Equals(x.ResourceId, filter.ResourceId, S |
| | 4 | 144 | | .Where(x => serviceResourceIds == null || serviceResourceIds.Contains(x.ResourceId)) |
| | 4 | 145 | | .Where(x => filter.From == null || x.Timestamp >= filter.From) |
| | 4 | 146 | | .Where(x => filter.To == null || x.Timestamp <= filter.To) |
| | 4 | 147 | | .Where(x => instrumentFilterIds == null || instrumentFilterIds.Contains(x.InstrumentId)) |
| | 2 | 148 | | .OrderBy(x => x.Timestamp) |
| | 2 | 149 | | .TakeLast(take) |
| | 2 | 150 | | .ToList(); |
| | | 151 | | |
| | 2 | 152 | | var selectedInstruments = points |
| | 2 | 153 | | .Select(x => x.InstrumentId) |
| | 2 | 154 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 2 | 155 | | .Where(instruments.ContainsKey) |
| | 2 | 156 | | .Select(x => instruments[x]) |
| | 2 | 157 | | .ToList(); |
| | | 158 | | |
| | 2 | 159 | | return ValueTask.FromResult(new OpenTelemetryMetricResult(selectedInstruments, points, _metricPoints.DroppedCoun |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | public ValueTask<OpenTelemetryLogResult> QueryLogsAsync(OpenTelemetryLogFilter filter, CancellationToken cancellatio |
| | | 163 | | { |
| | 1 | 164 | | var take = ClampTake(filter.Take); |
| | 1 | 165 | | var serviceResourceIds = ResolveResourceIds(filter.ServiceName); |
| | 1 | 166 | | var logs = _logs.Snapshot() |
| | 3 | 167 | | .Where(x => string.IsNullOrWhiteSpace(filter.ResourceId) || string.Equals(x.ResourceId, filter.ResourceId, S |
| | 3 | 168 | | .Where(x => serviceResourceIds == null || serviceResourceIds.Contains(x.ResourceId)) |
| | 3 | 169 | | .Where(x => string.IsNullOrWhiteSpace(filter.TraceId) || Matches(x.TraceId, filter.TraceId)) |
| | 2 | 170 | | .Where(x => string.IsNullOrWhiteSpace(filter.SpanId) || Matches(x.SpanId, filter.SpanId)) |
| | 2 | 171 | | .Where(x => string.IsNullOrWhiteSpace(filter.Severity) || Matches(x.SeverityText, filter.Severity)) |
| | 1 | 172 | | .Where(x => filter.From == null || x.Timestamp >= filter.From) |
| | 1 | 173 | | .Where(x => filter.To == null || x.Timestamp <= filter.To) |
| | 1 | 174 | | .Where(x => string.IsNullOrWhiteSpace(filter.Search) || Matches(x.Body, filter.Search)) |
| | 1 | 175 | | .OrderBy(x => x.Timestamp) |
| | 1 | 176 | | .TakeLast(take) |
| | 1 | 177 | | .ToList(); |
| | | 178 | | |
| | 1 | 179 | | return ValueTask.FromResult(new OpenTelemetryLogResult(logs, _logs.DroppedCount)); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | public ValueTask<OpenTelemetryStorageDiagnostics> GetDiagnosticsAsync(CancellationToken cancellationToken = default) |
| | | 183 | | { |
| | | 184 | | Dictionary<string, MetricInstrument> instruments; |
| | 2 | 185 | | lock (_instrumentLock) |
| | 2 | 186 | | instruments = new(_instruments, StringComparer.OrdinalIgnoreCase); |
| | | 187 | | |
| | 2 | 188 | | var traceSnapshot = _traces.Snapshot(); |
| | 2 | 189 | | var spanSnapshot = _spans.Snapshot(); |
| | 2 | 190 | | var metricPointSnapshot = _metricPoints.Snapshot(); |
| | 2 | 191 | | var logSnapshot = _logs.Snapshot(); |
| | 2 | 192 | | var diagnostics = new OpenTelemetryStorageDiagnostics( |
| | 2 | 193 | | _traceCapacity, |
| | 2 | 194 | | _spanCapacity, |
| | 2 | 195 | | _metricPointCapacity, |
| | 2 | 196 | | _logRecordCapacity, |
| | 2 | 197 | | _sourceRegistry.List().Count, |
| | 2 | 198 | | traceSnapshot.Count, |
| | 2 | 199 | | spanSnapshot.Count, |
| | 2 | 200 | | instruments.Count, |
| | 2 | 201 | | metricPointSnapshot.Count, |
| | 2 | 202 | | logSnapshot.Count, |
| | 2 | 203 | | _traces.DroppedCount, |
| | 2 | 204 | | _spans.DroppedCount, |
| | 2 | 205 | | _metricPoints.DroppedCount, |
| | 2 | 206 | | _logs.DroppedCount); |
| | | 207 | | |
| | 2 | 208 | | return ValueTask.FromResult(diagnostics); |
| | | 209 | | } |
| | | 210 | | |
| | 9 | 211 | | private int ClampTake(int? take) => Math.Clamp(take ?? _options.MaxQuerySize, 0, _options.MaxQuerySize); |
| | | 212 | | |
| | 64 | 213 | | private static int ClampCapacity(int capacity) => Math.Max(1, capacity); |
| | | 214 | | |
| | | 215 | | private HashSet<string>? ResolveResourceIds(string? serviceName) |
| | | 216 | | { |
| | 7 | 217 | | if (string.IsNullOrWhiteSpace(serviceName)) |
| | 6 | 218 | | return null; |
| | | 219 | | |
| | 1 | 220 | | return _sourceRegistry.List() |
| | 2 | 221 | | .Where(x => string.Equals(x.ServiceName, serviceName, StringComparison.OrdinalIgnoreCase)) |
| | 1 | 222 | | .Select(x => x.Id) |
| | 1 | 223 | | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 224 | | } |
| | | 225 | | |
| | 10 | 226 | | private static bool Matches(string? candidate, string? search) => !string.IsNullOrEmpty(candidate) && !string.IsNull |
| | | 227 | | } |