public async Task <int> AppendAsync(JaegerSpan span, CancellationToken cancellationToken) { if (!this.processByteSize.HasValue) { this.processByteSize = await this.GetSize(this.Process).ConfigureAwait(false); this.batchByteSize = this.processByteSize.Value; } int spanSize = await this.GetSize(span).ConfigureAwait(false); if (spanSize + this.processByteSize > this.maxPacketSize) { throw new JaegerExporterException($"ThriftSender received a span that was too large, size = {spanSize + this.processByteSize}, max = {this.maxPacketSize}", null); } var flushedSpanCount = 0; try { await this.flushLock.WaitAsync().ConfigureAwait(false); // flush if current batch size plus new span size equals or exceeds max batch size if (this.batchByteSize + spanSize >= this.maxPacketSize) { flushedSpanCount = await this.FlushAsync(cancellationToken).ConfigureAwait(false); } else { this.maxFlushIntervalTimer.Enabled = true; } // add span to batch and wait for more spans this.currentBatch.Add(span); this.batchByteSize += spanSize; } finally { this.flushLock.Release(); } return(flushedSpanCount); }
public async Task <int> AppendAsync(JaegerSpan span, CancellationToken cancellationToken) { int spanSize = this.GetSize(span); if (spanSize + this.processByteSize > this.maxPacketSize) { throw new JaegerExporterException($"ThriftSender received a span that was too large, size = {spanSize + this.processByteSize}, max = {this.maxPacketSize}", null); } var flushedSpanCount = 0; // flush if current batch size plus new span size equals or exceeds max batch size if (this.batchByteSize + spanSize >= this.maxPacketSize) { flushedSpanCount = await this.FlushAsync(cancellationToken).ConfigureAwait(false); } // add span to batch and wait for more spans this.currentBatch.Add(span); this.batchByteSize += spanSize; return(flushedSpanCount); }
public async Task <int> AppendAsync(JaegerSpan span, CancellationToken cancellationToken) { int spanSize = this.GetSize(span); if (spanSize > this.maxPacketSize) { throw new JaegerExporterException($"ThriftSender received a span that was too large, size = {spanSize}, max = {this.maxPacketSize}", null); } this.batchByteSize += spanSize; if (this.batchByteSize <= this.maxPacketSize) { this.currentBatch.Add(span); if (this.batchByteSize < this.maxPacketSize) { return(0); } return(await this.FlushAsync(cancellationToken).ConfigureAwait(false)); } int n; try { n = await this.FlushAsync(cancellationToken).ConfigureAwait(false); } catch (JaegerExporterException ex) { // +1 for the span not submitted in the buffer above throw new JaegerExporterException(ex.Message, ex); } this.currentBatch.Add(span); this.batchByteSize = this.processByteSize + spanSize; return(n); }
protected async ValueTask <int> AppendInternalAsync(JaegerSpan jaegerSpan, CancellationToken cancellationToken) { if (this.processCache == null) { this.Process.Message = this.BuildThriftMessage(this.Process).ToArray(); this.processCache = new Dictionary <string, Process> { [this.Process.ServiceName] = this.Process, }; } var spanServiceName = jaegerSpan.PeerServiceName ?? this.Process.ServiceName; if (!this.processCache.TryGetValue(spanServiceName, out var spanProcess)) { spanProcess = new Process(spanServiceName, this.Process.Tags); spanProcess.Message = this.BuildThriftMessage(spanProcess).ToArray(); this.processCache.Add(spanServiceName, spanProcess); } var spanMessage = this.BuildThriftMessage(jaegerSpan); jaegerSpan.Return(); if (spanMessage.Count + spanProcess.Message.Length > this.maxPacketSize) { throw new JaegerExporterException($"ThriftSender received a span that was too large, size = {spanMessage.Count + spanProcess.Message.Length}, max = {this.maxPacketSize}", null); } var spanTotalBytesNeeded = spanMessage.Count; var flushedSpanCount = 0; if (!this.CurrentBatches.TryGetValue(spanServiceName, out var spanBatch)) { spanBatch = new Batch(spanProcess) { SpanMessages = new List <BufferWriterMemory>(), }; this.CurrentBatches.Add(spanServiceName, spanBatch); spanTotalBytesNeeded += spanProcess.Message.Length; } // flush if current batch size plus new span size equals or exceeds max batch size if (this.batchByteSize + spanTotalBytesNeeded >= this.maxPacketSize) { flushedSpanCount = await this.FlushAsyncInternal(lockAlreadyHeld : true, cancellationToken).ConfigureAwait(false); // Flushing effectively erases the spanBatch we were working on, so we have to rebuild it. spanBatch.SpanMessages.Clear(); spanTotalBytesNeeded = spanMessage.Count + spanProcess.Message.Length; this.CurrentBatches.Add(spanServiceName, spanBatch); } else { this.maxFlushIntervalTimer.Enabled = true; } // add span to batch and wait for more spans spanBatch.SpanMessages.Add(spanMessage); this.batchByteSize += spanTotalBytesNeeded; return(flushedSpanCount); }
public async Task <int> AppendAsync(JaegerSpan span, CancellationToken cancellationToken) { if (this.processCache == null) { this.Process.ByteSize = await this.GetSize(this.Process).ConfigureAwait(false); this.processCache = new Dictionary <string, Process> { [this.Process.ServiceName] = this.Process, }; } string spanServiceName = span.JaegerTags?.FirstOrDefault(t => t.Key == "peer.service")?.VStr ?? this.Process.ServiceName; if (!this.processCache.TryGetValue(spanServiceName, out var spanProcess)) { spanProcess = new Process(spanServiceName, this.Process.Tags); spanProcess.ByteSize = await this.GetSize(spanProcess).ConfigureAwait(false); this.processCache.Add(spanServiceName, spanProcess); } int spanByteSize = await this.GetSize(span).ConfigureAwait(false); if (spanByteSize + spanProcess.ByteSize > this.maxPacketSize) { throw new JaegerExporterException($"ThriftSender received a span that was too large, size = {spanByteSize + spanProcess.ByteSize}, max = {this.maxPacketSize}", null); } int spanTotalBytesNeeded = spanByteSize; if (!this.CurrentBatches.TryGetValue(spanServiceName, out var spanBatch)) { spanBatch = new Batch(spanProcess); this.CurrentBatches.Add(spanServiceName, spanBatch); spanTotalBytesNeeded += spanProcess.ByteSize; } var flushedSpanCount = 0; await this.flushLock.WaitAsync().ConfigureAwait(false); try { // flush if current batch size plus new span size equals or exceeds max batch size if (this.batchByteSize + spanTotalBytesNeeded >= this.maxPacketSize) { flushedSpanCount = await this.FlushAsyncInternal(true, cancellationToken).ConfigureAwait(false); // Flushing effectively erases the spanBatch we were working on, so we have to rebuild it. spanBatch.Spans.Clear(); spanTotalBytesNeeded = spanByteSize + spanProcess.ByteSize; this.CurrentBatches.Add(spanServiceName, spanBatch); } else { this.maxFlushIntervalTimer.Enabled = true; } // add span to batch and wait for more spans spanBatch.Spans.Add(span); this.batchByteSize += spanTotalBytesNeeded; } finally { this.flushLock.Release(); } return(flushedSpanCount); }