sdk/trace: NewBatchSpanProcessor panics on negative OTEL_BSP_MAX_QUEUE_SIZE or OTEL_BSP_MAX_EXPORT_BATCH_SIZE
Description
sdktrace.NewBatchSpanProcessor panics when OTEL_BSP_MAX_QUEUE_SIZE or OTEL_BSP_MAX_EXPORT_BATCH_SIZE is set to a negative integer.
The environment values are only parsed with strconv.Atoi (sdk/trace/internal/env/env.go) and are not range-checked. They are then used directly as the capacity in make([]ReadOnlySpan, 0, o.MaxExportBatchSize) in NewBatchSpanProcessor:
OTEL_BSP_MAX_EXPORT_BATCH_SIZE=-1gives a batch capacity of-1.OTEL_BSP_MAX_QUEUE_SIZE=-1givesmaxExportBatchSize > maxQueueSize, so the batch size becomesmin(DefaultMaxExportBatchSize, -1) = -1.
A misconfigured environment variable can therefore crash the host application during SDK setup.
The spec says valid values are positive for both variables. The log SDK's BatchProcessor already ignores values below 1 for the equivalent OTEL_BLRP_* variables (clearLessThanOne in sdk/log/batch.go).
Environment
- OS: Windows
- Architecture: amd64
- Go Version: 1.27.0
- opentelemetry-go version: 29413a626 (
sdk/trace/batch_span_processor.goandsdk/trace/internal/env/env.goare unchanged on currentmain)
Steps To Reproduce
package main
import (
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
func main() {
sdktrace.NewBatchSpanProcessor(tracetest.NewInMemoryExporter())
}$ OTEL_BSP_MAX_QUEUE_SIZE=-1 go run .
panic: runtime error: makeslice: cap out of range
$ OTEL_BSP_MAX_EXPORT_BATCH_SIZE=-1 go run .
panic: runtime error: makeslice: cap out of rangeExpected behavior
Invalid (non-positive) OTEL_BSP_* values should not panic. They should be ignored and the defaults used, as the log SDK's BatchProcessor does for OTEL_BLRP_*, ideally with a warning as the spec's numeric guidance suggests.
Source: open-telemetry/opentelemetry-go