/// <summary> /// Initializes a new instance of the <see cref="BatchingActivityProcessor"/> class with custom settings. /// </summary> /// <param name="exporter">Exporter instance.</param> /// <param name="maxQueueSize">Maximum queue size. After the size is reached activities are dropped by processor.</param> /// <param name="scheduledDelay">The delay between two consecutive exports.</param> /// <param name="exporterTimeout">Maximum allowed time to export data.</param> /// <param name="maxExportBatchSize">The maximum batch size of every export. It must be smaller or equal to maxQueueSize.</param> public BatchingActivityProcessor(ActivityExporter exporter, int maxQueueSize, TimeSpan scheduledDelay, TimeSpan exporterTimeout, int maxExportBatchSize) { if (maxQueueSize <= 0) { throw new ArgumentOutOfRangeException(nameof(maxQueueSize)); } if (maxExportBatchSize <= 0 || maxExportBatchSize > maxQueueSize) { throw new ArgumentOutOfRangeException(nameof(maxExportBatchSize)); } this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter)); this.maxQueueSize = maxQueueSize; this.scheduledDelay = scheduledDelay; this.exporterTimeout = exporterTimeout; this.maxExportBatchSize = maxExportBatchSize; this.cts = new CancellationTokenSource(); this.exportQueue = new ConcurrentQueue <Activity>(); // worker task that will last for lifetime of processor. // Threads are also useless as exporter tasks run in thread pool threads. Task.Run(() => this.Worker(this.cts.Token), this.cts.Token); }
/// <summary> /// Initializes a new instance of the <see cref="BatchingActivityProcessor"/> class with custom settings. /// </summary> /// <param name="exporter">Exporter instance.</param> /// <param name="maxQueueSize">Maximum queue size. After the size is reached activities are dropped by processor.</param> /// <param name="scheduledDelay">The delay between two consecutive exports.</param> /// <param name="exporterTimeout">Maximum allowed time to export data.</param> /// <param name="maxExportBatchSize">The maximum batch size of every export. It must be smaller or equal to maxQueueSize.</param> public BatchingActivityProcessor(ActivityExporter exporter, int maxQueueSize, TimeSpan scheduledDelay, TimeSpan exporterTimeout, int maxExportBatchSize) { if (maxQueueSize <= 0) { throw new ArgumentOutOfRangeException(nameof(maxQueueSize)); } if (maxExportBatchSize <= 0 || maxExportBatchSize > maxQueueSize) { throw new ArgumentOutOfRangeException(nameof(maxExportBatchSize)); } if (scheduledDelay <= TimeSpan.FromMilliseconds(0)) { throw new ArgumentOutOfRangeException(nameof(scheduledDelay)); } this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter)); this.maxQueueSize = maxQueueSize; this.scheduledDelay = scheduledDelay; this.exporterTimeout = exporterTimeout; this.maxExportBatchSize = maxExportBatchSize; this.exportQueue = new ConcurrentQueue <Activity>(); this.flushTimer = new System.Timers.Timer { AutoReset = false, Enabled = true, Interval = this.scheduledDelay.TotalMilliseconds, }; this.flushTimer.Elapsed += async(sender, args) => { bool lockTaken = this.flushLock.Wait(0); try { if (!lockTaken) { // If the lock was already held, it means a flush is already executing. return; } await this.FlushAsyncInternal(drain : false, lockAlreadyHeld : true, CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) { OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(System.Timers.Timer.Elapsed), ex); } finally { if (lockTaken) { this.flushLock.Release(); } } }; }
/// <summary> /// Initializes a new instance of the <see cref="BatchingActivityProcessor"/> class with default parameters: /// <list type="bullet"> /// <item> /// <description>maxQueueSize = 2048,</description> /// </item> /// <item> /// <description>scheduledDelay = 5 sec,</description> /// </item> /// <item> /// <description>exporterTimeout = 30 sec,</description> /// </item> /// <item> /// <description>maxExportBatchSize = 512</description> /// </item> /// </list> /// </summary> /// <param name="exporter">Exporter instance.</param> public BatchingActivityProcessor(ActivityExporter exporter) : this(exporter, DefaultMaxQueueSize, DefaultScheduledDelay, DefaultExporterTimeout, DefaultMaxExportBatchSize) { }
/// <summary> /// Initializes a new instance of the <see cref="SimpleActivityProcessor"/> class. /// </summary> /// <param name="exporter">Activity exporter instance.</param> public SimpleActivityProcessor(ActivityExporter exporter) { this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter)); }