Skip to content

Commit 2a0dcd1

Browse files
committed
perf(native): reduce processor flush overhead
Avoid allocating the active-span list until a native trace is actually being flushed. Complete-trace flushes can also reuse trace.started as the finished export list because all started spans are finished and the processor erases the trace by reassignment after export. Partial flushes still build a finished-only array for the active-span case.
1 parent 1827feb commit 2a0dcd1

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

packages/dd-trace/src/span_processor.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,18 @@ class SpanProcessor {
244244

245245
process (span) {
246246
const spanContext = span.context()
247-
const active = []
248247
const trace = spanContext._trace
249248
const { flushMinSpans, DD_TRACE_ENABLED } = this._config
250249
const { started, finished } = trace
251250

252251
if (trace.record === false) return
253252
if (DD_TRACE_ENABLED === false) {
254-
this._erase(trace, active)
253+
this._erase(trace, [])
255254
return
256255
}
257-
if (started.length === finished.length || finished.length >= flushMinSpans) {
256+
const allStartedFinished = started.length === finished.length
257+
if (allStartedFinished || finished.length >= flushMinSpans) {
258+
const active = []
258259
this.sample(span)
259260
this._gitMetadataTagger.tagGitMetadata(spanContext)
260261

@@ -269,15 +270,15 @@ class SpanProcessor {
269270
// Pass raw spans to the native exporter; the WASM pipeline serializes
270271
// them. When native stats are enabled the concentrator handles stats
271272
// aggregation during flush_chunk.
272-
const finishedSpansToExport = []
273+
const finishedSpansToExport = allStartedFinished ? started : []
273274
const otelSemantics = this._config.DD_TRACE_OTEL_SEMANTICS_ENABLED
274275
let isFirstSpanInChunk = true
275276

276277
for (const span of started) {
277278
if (span._duration === undefined) {
278279
active.push(span)
279280
} else {
280-
finishedSpansToExport.push(span)
281+
if (!allStartedFinished) finishedSpansToExport.push(span)
281282
const context = span.context()
282283

283284
// OTLP trace metrics remain a JS-side stats feature. Build the same

0 commit comments

Comments
 (0)