Files
am1r021andGitHub de674ca21d [fix] set schemaurl in batchprocessor (#14620)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
`batchprocessor` could drop resource/scope `SchemaURL` during partial
batch splits. This causes exported data to lose schema context. This
change fixes it by copying `SchemaURL` on resource and scope containers
in partial split logic for traces, metrics, and logs.

<!-- Issue number if applicable -->
#### Link to tracking issue
Fixes #
#12279 

<!--Describe what testing was performed and which tests were added.-->
#### Testing
A unit test has been added for partial splits on metrics, traces, and
logs to check if output still contains `schemaUrl`.

<!--Describe the documentation added.-->
#### Documentation
N/A
2026-02-24 16:47:10 +00:00

76 lines
2.1 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package batchprocessor // import "go.opentelemetry.io/collector/processor/batchprocessor"
import (
"go.opentelemetry.io/collector/pdata/ptrace"
)
// splitTraces removes spans from the input trace and returns a new trace of the specified size.
func splitTraces(size int, src ptrace.Traces) ptrace.Traces {
if src.SpanCount() <= size {
return src
}
totalCopiedSpans := 0
dest := ptrace.NewTraces()
src.ResourceSpans().RemoveIf(func(srcRs ptrace.ResourceSpans) bool {
// If we are done skip everything else.
if totalCopiedSpans == size {
return false
}
// If it fully fits
srcRsSC := resourceSC(srcRs)
if (totalCopiedSpans + srcRsSC) <= size {
totalCopiedSpans += srcRsSC
srcRs.MoveTo(dest.ResourceSpans().AppendEmpty())
return true
}
destRs := dest.ResourceSpans().AppendEmpty()
srcRs.Resource().CopyTo(destRs.Resource())
destRs.SetSchemaUrl(srcRs.SchemaUrl())
srcRs.ScopeSpans().RemoveIf(func(srcIls ptrace.ScopeSpans) bool {
// If we are done skip everything else.
if totalCopiedSpans == size {
return false
}
// If possible to move all metrics do that.
srcIlsSC := srcIls.Spans().Len()
if size-totalCopiedSpans >= srcIlsSC {
totalCopiedSpans += srcIlsSC
srcIls.MoveTo(destRs.ScopeSpans().AppendEmpty())
return true
}
destIls := destRs.ScopeSpans().AppendEmpty()
srcIls.Scope().CopyTo(destIls.Scope())
destIls.SetSchemaUrl(srcIls.SchemaUrl())
srcIls.Spans().RemoveIf(func(srcSpan ptrace.Span) bool {
// If we are done skip everything else.
if totalCopiedSpans == size {
return false
}
srcSpan.MoveTo(destIls.Spans().AppendEmpty())
totalCopiedSpans++
return true
})
return false
})
return srcRs.ScopeSpans().Len() == 0
})
return dest
}
// resourceSC calculates the total number of spans in the ptrace.ResourceSpans.
func resourceSC(rs ptrace.ResourceSpans) (count int) {
for k := 0; k < rs.ScopeSpans().Len(); k++ {
count += rs.ScopeSpans().At(k).Spans().Len()
}
return count
}