| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Threading.Channels; |
| | | 3 | | using Elsa.Diagnostics.OpenTelemetry.Contracts; |
| | | 4 | | using Elsa.Diagnostics.OpenTelemetry.Models; |
| | | 5 | | using Elsa.Diagnostics.OpenTelemetry.Options; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Diagnostics.OpenTelemetry.Providers.InMemory; |
| | | 9 | | |
| | 10 | 10 | | public class InMemoryOpenTelemetryLiveFeed(IOptions<OpenTelemetryDiagnosticsOptions> options) : IOpenTelemetryLiveFeed |
| | | 11 | | { |
| | 10 | 12 | | private readonly OpenTelemetryDiagnosticsOptions _options = options.Value; |
| | 10 | 13 | | private readonly object _subscribersLock = new(); |
| | 10 | 14 | | private readonly List<OpenTelemetrySubscriber> _subscribers = []; |
| | | 15 | | |
| | | 16 | | public ValueTask PublishAsync(OpenTelemetryBatch batch, CancellationToken cancellationToken = default) |
| | | 17 | | { |
| | | 18 | | List<OpenTelemetrySubscriber> subscribers; |
| | 8 | 19 | | lock (_subscribersLock) |
| | 8 | 20 | | subscribers = _subscribers.ToList(); |
| | | 21 | | |
| | 26 | 22 | | foreach (var subscriber in subscribers) |
| | 5 | 23 | | subscriber.TryWrite(batch); |
| | | 24 | | |
| | 8 | 25 | | return ValueTask.CompletedTask; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public async IAsyncEnumerable<OpenTelemetryStreamItem> SubscribeAsync(OpenTelemetryTraceFilter filter, [EnumeratorCa |
| | | 29 | | { |
| | 4 | 30 | | var subscriber = new OpenTelemetrySubscriber(filter, _options.SubscriberChannelCapacity); |
| | | 31 | | |
| | 4 | 32 | | lock (_subscribersLock) |
| | 4 | 33 | | _subscribers.Add(subscriber); |
| | | 34 | | |
| | | 35 | | try |
| | | 36 | | { |
| | 18 | 37 | | await foreach (var item in subscriber.Channel.Reader.ReadAllAsync(cancellationToken)) |
| | | 38 | | { |
| | 7 | 39 | | subscriber.MarkRead(item); |
| | 7 | 40 | | yield return item; |
| | | 41 | | } |
| | 0 | 42 | | } |
| | | 43 | | finally |
| | | 44 | | { |
| | 4 | 45 | | lock (_subscribersLock) |
| | 4 | 46 | | _subscribers.Remove(subscriber); |
| | | 47 | | } |
| | 4 | 48 | | } |
| | | 49 | | |
| | 4 | 50 | | private sealed class OpenTelemetrySubscriber(OpenTelemetryTraceFilter filter, int channelCapacity) |
| | | 51 | | { |
| | 4 | 52 | | private readonly object _lock = new(); |
| | 4 | 53 | | private readonly int _capacity = Math.Max(1, channelCapacity); |
| | | 54 | | private int _pendingItemCount; |
| | 4 | 55 | | private readonly Dictionary<OpenTelemetrySignalType, long> _droppedSinceLastSummary = []; |
| | | 56 | | |
| | 19 | 57 | | public Channel<OpenTelemetryStreamItem> Channel { get; } = System.Threading.Channels.Channel.CreateBounded<OpenT |
| | 4 | 58 | | { |
| | 4 | 59 | | SingleReader = true, |
| | 4 | 60 | | SingleWriter = false, |
| | 4 | 61 | | FullMode = BoundedChannelFullMode.Wait |
| | 4 | 62 | | }); |
| | | 63 | | |
| | | 64 | | public void TryWrite(OpenTelemetryBatch batch) |
| | | 65 | | { |
| | 5 | 66 | | var serviceResourceIds = ResolveServiceResourceIds(batch.Resources); |
| | | 67 | | |
| | 12 | 68 | | foreach (var resource in batch.Resources.Where(MatchesResource)) |
| | 1 | 69 | | TryWrite(new OpenTelemetryStreamItem { Resource = resource }); |
| | | 70 | | |
| | 27 | 71 | | foreach (var trace in batch.Traces.Where(trace => MatchesTrace(trace, serviceResourceIds))) |
| | 5 | 72 | | TryWrite(new OpenTelemetryStreamItem { Trace = trace }); |
| | | 73 | | |
| | 14 | 74 | | foreach (var log in batch.Logs.Where(log => MatchesLog(log, serviceResourceIds))) |
| | 1 | 75 | | TryWrite(new OpenTelemetryStreamItem { Log = log }); |
| | | 76 | | |
| | 14 | 77 | | foreach (var point in batch.MetricPoints.Where(point => MatchesMetricPoint(point, serviceResourceIds))) |
| | 1 | 78 | | TryWrite(new OpenTelemetryStreamItem { MetricPoint = point }); |
| | 5 | 79 | | } |
| | | 80 | | |
| | | 81 | | private void TryWrite(OpenTelemetryStreamItem item) |
| | | 82 | | { |
| | 8 | 83 | | lock (_lock) |
| | | 84 | | { |
| | 8 | 85 | | EnsureCapacityForWrite(); |
| | 8 | 86 | | if (Channel.Writer.TryWrite(item)) |
| | 8 | 87 | | _pendingItemCount++; |
| | | 88 | | else |
| | 0 | 89 | | TrackDrop(item); |
| | | 90 | | |
| | 8 | 91 | | TryWriteDroppedSummary(); |
| | 8 | 92 | | } |
| | 8 | 93 | | } |
| | | 94 | | |
| | | 95 | | public void MarkRead(OpenTelemetryStreamItem item) |
| | | 96 | | { |
| | 7 | 97 | | lock (_lock) |
| | | 98 | | { |
| | 7 | 99 | | _pendingItemCount = Math.Max(0, _pendingItemCount - 1); |
| | | 100 | | |
| | 7 | 101 | | if (item.DroppedItems != null) |
| | 1 | 102 | | _droppedSinceLastSummary.Remove(item.DroppedItems.SignalType); |
| | | 103 | | |
| | 7 | 104 | | TryWriteDroppedSummary(); |
| | 7 | 105 | | } |
| | 7 | 106 | | } |
| | | 107 | | |
| | | 108 | | private void EnsureCapacityForWrite() |
| | | 109 | | { |
| | 8 | 110 | | if (_pendingItemCount < _capacity) |
| | 6 | 111 | | return; |
| | | 112 | | |
| | 2 | 113 | | if (!Channel.Reader.TryRead(out var dropped)) |
| | 0 | 114 | | return; |
| | | 115 | | |
| | 2 | 116 | | _pendingItemCount = Math.Max(0, _pendingItemCount - 1); |
| | 2 | 117 | | TrackDrop(dropped); |
| | 2 | 118 | | } |
| | | 119 | | |
| | | 120 | | private void TrackDrop(OpenTelemetryStreamItem item) |
| | | 121 | | { |
| | 2 | 122 | | var signalType = GetSignalType(item); |
| | 2 | 123 | | if (signalType == null) |
| | 0 | 124 | | return; |
| | | 125 | | |
| | 2 | 126 | | _droppedSinceLastSummary.TryGetValue(signalType.Value, out var count); |
| | 2 | 127 | | _droppedSinceLastSummary[signalType.Value] = count + (item.DroppedItems?.Count ?? 1); |
| | 2 | 128 | | } |
| | | 129 | | |
| | | 130 | | private void TryWriteDroppedSummary() |
| | | 131 | | { |
| | 15 | 132 | | if (_droppedSinceLastSummary.Count == 0) |
| | 12 | 133 | | return; |
| | | 134 | | |
| | 3 | 135 | | if (_pendingItemCount >= _capacity) |
| | 2 | 136 | | return; |
| | | 137 | | |
| | 2 | 138 | | var dropped = _droppedSinceLastSummary.OrderBy(x => x.Key).First(); |
| | 1 | 139 | | if (Channel.Writer.TryWrite(new OpenTelemetryStreamItem { DroppedItems = new(dropped.Key, dropped.Value, "Su |
| | 1 | 140 | | _pendingItemCount++; |
| | 1 | 141 | | } |
| | | 142 | | |
| | | 143 | | private static OpenTelemetrySignalType? GetSignalType(OpenTelemetryStreamItem item) |
| | | 144 | | { |
| | 2 | 145 | | if (item.Trace != null) |
| | 2 | 146 | | return OpenTelemetrySignalType.Trace; |
| | | 147 | | |
| | 0 | 148 | | if (item.MetricPoint != null) |
| | 0 | 149 | | return OpenTelemetrySignalType.Metric; |
| | | 150 | | |
| | 0 | 151 | | if (item.Log != null) |
| | 0 | 152 | | return OpenTelemetrySignalType.Log; |
| | | 153 | | |
| | 0 | 154 | | return item.DroppedItems?.SignalType; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | private bool MatchesResource(TelemetryResource resource) |
| | | 158 | | { |
| | 2 | 159 | | if (!string.IsNullOrWhiteSpace(filter.TraceId)) |
| | 0 | 160 | | return false; |
| | | 161 | | |
| | 2 | 162 | | if (!string.IsNullOrWhiteSpace(filter.WorkflowInstanceId)) |
| | 0 | 163 | | return false; |
| | | 164 | | |
| | 2 | 165 | | if (filter.Status != null) |
| | 0 | 166 | | return false; |
| | | 167 | | |
| | 2 | 168 | | if (!string.IsNullOrWhiteSpace(filter.ResourceId) && !EqualsIgnoreCase(resource.Id, filter.ResourceId)) |
| | 0 | 169 | | return false; |
| | | 170 | | |
| | 2 | 171 | | if (!string.IsNullOrWhiteSpace(filter.ServiceName) && !EqualsIgnoreCase(resource.ServiceName, filter.Service |
| | 1 | 172 | | return false; |
| | | 173 | | |
| | 1 | 174 | | if (filter.From != null && resource.LastSeen < filter.From) |
| | 0 | 175 | | return false; |
| | | 176 | | |
| | 1 | 177 | | if (filter.To != null && resource.LastSeen > filter.To) |
| | 0 | 178 | | return false; |
| | | 179 | | |
| | 1 | 180 | | if (!string.IsNullOrWhiteSpace(filter.Search) && !Matches(resource.Id, filter.Search) && !Matches(resource.S |
| | 0 | 181 | | return false; |
| | | 182 | | |
| | 1 | 183 | | return true; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | private bool MatchesTrace(TelemetryTrace trace, HashSet<string>? serviceResourceIds) |
| | | 187 | | { |
| | 7 | 188 | | if (!string.IsNullOrWhiteSpace(filter.TraceId) && !EqualsIgnoreCase(trace.TraceId, filter.TraceId)) |
| | 1 | 189 | | return false; |
| | | 190 | | |
| | 6 | 191 | | if (filter.Status != null && trace.Status != filter.Status) |
| | 0 | 192 | | return false; |
| | | 193 | | |
| | 6 | 194 | | if (filter.From != null && trace.StartTime < filter.From) |
| | 0 | 195 | | return false; |
| | | 196 | | |
| | 6 | 197 | | if (filter.To != null && trace.StartTime > filter.To) |
| | 0 | 198 | | return false; |
| | | 199 | | |
| | 6 | 200 | | if (!string.IsNullOrWhiteSpace(filter.WorkflowInstanceId) && !trace.WorkflowInstanceIds.Any(id => Matches(id |
| | 0 | 201 | | return false; |
| | | 202 | | |
| | 6 | 203 | | if (!string.IsNullOrWhiteSpace(filter.ResourceId) && !trace.ResourceIds.Contains(filter.ResourceId, StringCo |
| | 0 | 204 | | return false; |
| | | 205 | | |
| | 6 | 206 | | if (serviceResourceIds != null && !trace.ResourceIds.Any(serviceResourceIds.Contains)) |
| | 1 | 207 | | return false; |
| | | 208 | | |
| | 5 | 209 | | if (!string.IsNullOrWhiteSpace(filter.Search) && !Matches(trace.TraceId, filter.Search) && !Matches(trace.Na |
| | 0 | 210 | | return false; |
| | | 211 | | |
| | 5 | 212 | | return true; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | private bool MatchesLog(OtlpLogRecord log, HashSet<string>? serviceResourceIds) |
| | | 216 | | { |
| | 2 | 217 | | if (filter.Status != null) |
| | 0 | 218 | | return false; |
| | | 219 | | |
| | 2 | 220 | | if (!string.IsNullOrWhiteSpace(filter.ResourceId) && !EqualsIgnoreCase(log.ResourceId, filter.ResourceId)) |
| | 1 | 221 | | return false; |
| | | 222 | | |
| | 1 | 223 | | if (serviceResourceIds != null && !serviceResourceIds.Contains(log.ResourceId)) |
| | 0 | 224 | | return false; |
| | | 225 | | |
| | 1 | 226 | | if (!string.IsNullOrWhiteSpace(filter.TraceId) && !EqualsIgnoreCase(log.TraceId, filter.TraceId)) |
| | 0 | 227 | | return false; |
| | | 228 | | |
| | 1 | 229 | | if (!string.IsNullOrWhiteSpace(filter.WorkflowInstanceId) && !AttributeMatches(log.Attributes, "workflow.ins |
| | 0 | 230 | | return false; |
| | | 231 | | |
| | 1 | 232 | | if (filter.From != null && log.Timestamp < filter.From) |
| | 0 | 233 | | return false; |
| | | 234 | | |
| | 1 | 235 | | if (filter.To != null && log.Timestamp > filter.To) |
| | 0 | 236 | | return false; |
| | | 237 | | |
| | 1 | 238 | | if (!string.IsNullOrWhiteSpace(filter.Search) && !Matches(log.Body, filter.Search)) |
| | 0 | 239 | | return false; |
| | | 240 | | |
| | 1 | 241 | | return true; |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | private bool MatchesMetricPoint(MetricPoint point, HashSet<string>? serviceResourceIds) |
| | | 245 | | { |
| | 2 | 246 | | if (filter.Status != null) |
| | 0 | 247 | | return false; |
| | | 248 | | |
| | 2 | 249 | | if (!string.IsNullOrWhiteSpace(filter.ResourceId) && !EqualsIgnoreCase(point.ResourceId, filter.ResourceId)) |
| | 1 | 250 | | return false; |
| | | 251 | | |
| | 1 | 252 | | if (serviceResourceIds != null && !serviceResourceIds.Contains(point.ResourceId)) |
| | 0 | 253 | | return false; |
| | | 254 | | |
| | 1 | 255 | | if (!string.IsNullOrWhiteSpace(filter.TraceId) && !EqualsIgnoreCase(point.TraceId, filter.TraceId)) |
| | 0 | 256 | | return false; |
| | | 257 | | |
| | 1 | 258 | | if (!string.IsNullOrWhiteSpace(filter.WorkflowInstanceId) && !AttributeMatches(point.Attributes, "workflow.i |
| | 0 | 259 | | return false; |
| | | 260 | | |
| | 1 | 261 | | if (filter.From != null && point.Timestamp < filter.From) |
| | 0 | 262 | | return false; |
| | | 263 | | |
| | 1 | 264 | | if (filter.To != null && point.Timestamp > filter.To) |
| | 0 | 265 | | return false; |
| | | 266 | | |
| | 1 | 267 | | if (!string.IsNullOrWhiteSpace(filter.Search) && !Matches(point.InstrumentName, filter.Search) && !Matches(p |
| | 0 | 268 | | return false; |
| | | 269 | | |
| | 1 | 270 | | return true; |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | private HashSet<string>? ResolveServiceResourceIds(IEnumerable<TelemetryResource> resources) |
| | | 274 | | { |
| | 5 | 275 | | if (string.IsNullOrWhiteSpace(filter.ServiceName)) |
| | 3 | 276 | | return null; |
| | | 277 | | |
| | 2 | 278 | | return resources |
| | 2 | 279 | | .Where(x => EqualsIgnoreCase(x.ServiceName, filter.ServiceName)) |
| | 1 | 280 | | .Select(x => x.Id) |
| | 2 | 281 | | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | private static bool AttributeMatches(IDictionary<string, string?> attributes, string key, string? search) |
| | | 285 | | { |
| | 0 | 286 | | return attributes.TryGetValue(key, out var value) && Matches(value, search); |
| | | 287 | | } |
| | | 288 | | |
| | 10 | 289 | | private static bool EqualsIgnoreCase(string? candidate, string? search) => string.Equals(candidate, search, Stri |
| | | 290 | | |
| | 0 | 291 | | private static bool Matches(string? candidate, string? search) => !string.IsNullOrEmpty(candidate) && !string.Is |
| | | 292 | | } |
| | | 293 | | } |