| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using Elsa.Diagnostics.OpenTelemetry.Contracts; |
| | | 3 | | using Elsa.Diagnostics.OpenTelemetry.Models; |
| | | 4 | | using Microsoft.AspNetCore.SignalR; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Diagnostics.OpenTelemetry.RealTime; |
| | | 8 | | |
| | 5 | 9 | | public sealed class OpenTelemetrySubscriptionManager( |
| | 5 | 10 | | IOpenTelemetryLiveFeed liveFeed, |
| | 5 | 11 | | IHubContext<OpenTelemetryHub, IOpenTelemetryClient> hubContext, |
| | 5 | 12 | | ILogger<OpenTelemetrySubscriptionManager> logger) : IDisposable |
| | | 13 | | { |
| | 5 | 14 | | private readonly ConcurrentDictionary<string, OpenTelemetrySubscription> _subscriptions = new(StringComparer.Ordinal |
| | | 15 | | |
| | | 16 | | public Task SubscribeAsync(string connectionId, OpenTelemetryTraceFilter filter, CancellationToken cancellationToken |
| | | 17 | | { |
| | 3 | 18 | | Unsubscribe(connectionId); |
| | 3 | 19 | | var subscriptionCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 3 | 20 | | var subscription = new OpenTelemetrySubscription(filter, subscriptionCancellation); |
| | 3 | 21 | | _subscriptions[connectionId] = subscription; |
| | | 22 | | |
| | 3 | 23 | | _ = StreamAsync(connectionId, filter, subscription, subscriptionCancellation.Token); |
| | 3 | 24 | | return Task.CompletedTask; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public Task UnsubscribeAsync(string connectionId) |
| | | 28 | | { |
| | 0 | 29 | | Unsubscribe(connectionId); |
| | 0 | 30 | | return Task.CompletedTask; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public void Dispose() |
| | | 34 | | { |
| | 0 | 35 | | foreach (var subscription in _subscriptions.Values) |
| | | 36 | | { |
| | 0 | 37 | | subscription.CancellationTokenSource.Cancel(); |
| | 0 | 38 | | subscription.CancellationTokenSource.Dispose(); |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | _subscriptions.Clear(); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | private async Task StreamAsync(string connectionId, OpenTelemetryTraceFilter filter, OpenTelemetrySubscription subsc |
| | | 45 | | { |
| | | 46 | | try |
| | | 47 | | { |
| | 12 | 48 | | await foreach (var item in liveFeed.SubscribeAsync(filter, cancellationToken).ConfigureAwait(false)) |
| | 3 | 49 | | await hubContext.Clients.Client(connectionId).ReceiveAsync(item).ConfigureAwait(false); |
| | 3 | 50 | | } |
| | 0 | 51 | | catch (OperationCanceledException e) |
| | | 52 | | { |
| | 0 | 53 | | logger.LogDebug(e, "OpenTelemetry subscription for connection {ConnectionId} was canceled", connectionId); |
| | 0 | 54 | | } |
| | 0 | 55 | | catch (Exception e) when (e is not OperationCanceledException) |
| | | 56 | | { |
| | 0 | 57 | | logger.LogWarning(e, "OpenTelemetry subscription for connection {ConnectionId} stopped unexpectedly", connec |
| | 0 | 58 | | } |
| | | 59 | | finally |
| | | 60 | | { |
| | 3 | 61 | | Remove(connectionId, subscription); |
| | | 62 | | } |
| | 3 | 63 | | } |
| | | 64 | | |
| | | 65 | | private void Unsubscribe(string connectionId) |
| | | 66 | | { |
| | 3 | 67 | | if (!_subscriptions.TryRemove(connectionId, out var subscription)) |
| | 3 | 68 | | return; |
| | | 69 | | |
| | 0 | 70 | | subscription.CancellationTokenSource.Cancel(); |
| | 0 | 71 | | subscription.CancellationTokenSource.Dispose(); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | private void Remove(string connectionId, OpenTelemetrySubscription subscription) |
| | | 75 | | { |
| | 3 | 76 | | var entry = new KeyValuePair<string, OpenTelemetrySubscription>(connectionId, subscription); |
| | 3 | 77 | | if (((ICollection<KeyValuePair<string, OpenTelemetrySubscription>>)_subscriptions).Remove(entry)) |
| | 3 | 78 | | subscription.CancellationTokenSource.Dispose(); |
| | 3 | 79 | | } |
| | | 80 | | |
| | 6 | 81 | | private sealed record OpenTelemetrySubscription(OpenTelemetryTraceFilter Filter, CancellationTokenSource Cancellatio |
| | | 82 | | } |