|
| 1 | +using Cortex.Telemetry; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace Cortex.Streams.Operators |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// The FlatMapOperator takes each input element, applies a function to produce zero or more output elements, |
| 13 | + /// and emits each output element individually into the stream. |
| 14 | + /// </summary> |
| 15 | + /// <typeparam name="TInput">The type of the input element.</typeparam> |
| 16 | + /// <typeparam name="TOutput">The type of the output element(s) produced.</typeparam> |
| 17 | + public class FlatMapOperator<TInput, TOutput> : IOperator, IHasNextOperators, ITelemetryEnabled |
| 18 | + { |
| 19 | + private readonly Func<TInput, IEnumerable<TOutput>> _flatMapFunction; |
| 20 | + private IOperator _nextOperator; |
| 21 | + |
| 22 | + // Telemetry fields |
| 23 | + private ITelemetryProvider _telemetryProvider; |
| 24 | + private ICounter _processedCounter; |
| 25 | + private ICounter _emittedCounter; |
| 26 | + private IHistogram _processingTimeHistogram; |
| 27 | + private ITracer _tracer; |
| 28 | + private Action _incrementProcessedCounter; |
| 29 | + private Action _incrementEmittedCounter; |
| 30 | + private Action<double> _recordProcessingTime; |
| 31 | + |
| 32 | + public FlatMapOperator(Func<TInput, IEnumerable<TOutput>> flatMapFunction) |
| 33 | + { |
| 34 | + _flatMapFunction = flatMapFunction ?? throw new ArgumentNullException(nameof(flatMapFunction)); |
| 35 | + } |
| 36 | + |
| 37 | + public void SetTelemetryProvider(ITelemetryProvider telemetryProvider) |
| 38 | + { |
| 39 | + _telemetryProvider = telemetryProvider; |
| 40 | + |
| 41 | + if (_telemetryProvider != null) |
| 42 | + { |
| 43 | + var metrics = _telemetryProvider.GetMetricsProvider(); |
| 44 | + _processedCounter = metrics.CreateCounter($"flatmap_operator_processed_{typeof(TInput).Name}_to_{typeof(TOutput).Name}", "Number of items processed by FlatMapOperator"); |
| 45 | + _emittedCounter = metrics.CreateCounter($"flatmap_operator_emitted_{typeof(TInput).Name}_to_{typeof(TOutput).Name}", "Number of items emitted by FlatMapOperator"); |
| 46 | + _processingTimeHistogram = metrics.CreateHistogram($"flatmap_operator_processing_time_{typeof(TInput).Name}_to_{typeof(TOutput).Name}", "Processing time for FlatMapOperator"); |
| 47 | + _tracer = _telemetryProvider.GetTracingProvider().GetTracer($"FlatMapOperator_{typeof(TInput).Name}_to_{typeof(TOutput).Name}"); |
| 48 | + |
| 49 | + // Cache delegates |
| 50 | + _incrementProcessedCounter = () => _processedCounter.Increment(); |
| 51 | + _incrementEmittedCounter = () => _emittedCounter.Increment(); |
| 52 | + _recordProcessingTime = value => _processingTimeHistogram.Record(value); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + _incrementProcessedCounter = null; |
| 57 | + _incrementEmittedCounter = null; |
| 58 | + _recordProcessingTime = null; |
| 59 | + } |
| 60 | + |
| 61 | + // Propagate telemetry to next operator |
| 62 | + if (_nextOperator is ITelemetryEnabled telemetryEnabled) |
| 63 | + { |
| 64 | + telemetryEnabled.SetTelemetryProvider(telemetryProvider); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public void Process(object input) |
| 69 | + { |
| 70 | + if (input == null) |
| 71 | + throw new ArgumentNullException(nameof(input)); |
| 72 | + |
| 73 | + if (!(input is TInput typedInput)) |
| 74 | + throw new ArgumentException($"Expected input of type {typeof(TInput).Name}, but received {input.GetType().Name}", nameof(input)); |
| 75 | + |
| 76 | + IEnumerable<TOutput> outputs; |
| 77 | + |
| 78 | + if (_telemetryProvider != null) |
| 79 | + { |
| 80 | + var stopwatch = Stopwatch.StartNew(); |
| 81 | + using (var span = _tracer.StartSpan("FlatMapOperator.Process")) |
| 82 | + { |
| 83 | + try |
| 84 | + { |
| 85 | + outputs = _flatMapFunction(typedInput) ?? Array.Empty<TOutput>(); |
| 86 | + span.SetAttribute("status", "success"); |
| 87 | + span.SetAttribute("input_type", typeof(TInput).Name); |
| 88 | + span.SetAttribute("output_type", typeof(TOutput).Name); |
| 89 | + } |
| 90 | + catch (Exception ex) |
| 91 | + { |
| 92 | + span.SetAttribute("status", "error"); |
| 93 | + span.SetAttribute("exception", ex.ToString()); |
| 94 | + throw; |
| 95 | + } |
| 96 | + finally |
| 97 | + { |
| 98 | + stopwatch.Stop(); |
| 99 | + _recordProcessingTime?.Invoke(stopwatch.Elapsed.TotalMilliseconds); |
| 100 | + _incrementProcessedCounter?.Invoke(); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + outputs = _flatMapFunction(typedInput) ?? Array.Empty<TOutput>(); |
| 107 | + } |
| 108 | + |
| 109 | + // Emit each output element |
| 110 | + foreach (var output in outputs) |
| 111 | + { |
| 112 | + _incrementEmittedCounter?.Invoke(); |
| 113 | + _nextOperator?.Process(output); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public void SetNext(IOperator nextOperator) |
| 118 | + { |
| 119 | + _nextOperator = nextOperator; |
| 120 | + |
| 121 | + // Propagate telemetry |
| 122 | + if (_nextOperator is ITelemetryEnabled nextTelemetryEnabled && _telemetryProvider != null) |
| 123 | + { |
| 124 | + nextTelemetryEnabled.SetTelemetryProvider(_telemetryProvider); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public IEnumerable<IOperator> GetNextOperators() |
| 129 | + { |
| 130 | + if (_nextOperator != null) |
| 131 | + yield return _nextOperator; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments