Spanpruning Processor
contrib
Maintainers: @portertech
Source: opentelemetry-collector-contrib
Supported Telemetry
Overview
Overview
The Span Pruning Processor identifies duplicate or similar leaf spans within a single trace, groups them, and replaces each group with a single aggregated summary span. When leaf spans are aggregated, the processor also recursively aggregates their parent spans if all children of those parents are being aggregated. Leaf spans are spans that are not referenced as a parent by any other span in the trace. They typically represent the last actions in an execution call stack (e.g., individual database queries, HTTP calls to external services). Spans are grouped by:- Span name - spans must have the same name
- Span kind - spans must have the same kind (Internal, Server, Client, Producer, Consumer)
- Status code - spans must have the same status (OK, Error, or Unset)
- TraceState - spans must have identical TraceState values (for Consistent Probability Sampling compatibility)
- Configured attributes - spans must have matching values for attributes specified in
group_by_attributes - Parent span name - leaf spans must share the same parent span name to be grouped together
Use Cases
- Database query optimization: When an application makes many similar database queries (e.g., N+1 queries), aggregate them into a single summary span
- Batch operations: Consolidate many similar leaf operations into a single representative span
- Cost reduction: Reduce trace storage costs by eliminating redundant span data
Configuration
Configuration Options
| Field | Type | Default | Description |
|---|---|---|---|
group_by_attributes | []string | [] | Attribute patterns for grouping (supports glob patterns like db.*) |
min_spans_to_aggregate | int | 5 | Minimum group size before aggregation occurs |
max_parent_depth | int | 1 | Max depth of parent aggregation (0=none, -1=unlimited) |
aggregation_attribute_prefix | string | βaggregation.β | Prefix for aggregation statistics attributes |
Glob Pattern Support
Thegroup_by_attributes field supports glob patterns for matching attribute keys:
| Pattern | Matches |
|---|---|
db.* | db.operation, db.name, db.statement, etc. |
http.request.* | http.request.method, http.request.header.content-type, etc. |
rpc.* | rpc.method, rpc.service, rpc.system, etc. |
db.operation | Only the exact key db.operation |
Summary Span
When spans are aggregated, the summary span includes:Properties
- Name: Original span name (e.g.,
SELECT) - TraceID: Same as original spans
- SpanID: Newly generated unique ID
- ParentSpanID: Same as original spans (common parent)
- Kind: Same as template span (inherited from slowest span)
- StartTimestamp: Earliest start time of all spans in the group
- EndTimestamp: Latest end time of all spans in the group
- Status: Same as original spans (spans are grouped by status code)
- TraceState: Inherited from the template span (preserved for Consistent Probability Sampling compatibility)
- Attributes: Inherited from the slowest span in the group
Note: The summary spanβs duration (EndTimestamp - StartTimestamp) represents the total time window covered by all aggregated spans, which may exceedduration_max_ns. For example, if spans overlap or are staggered, the time range can be larger than any individual spanβs duration. Useduration_max_nsto find the slowest individual operation.
What Gets Aggregated Away
When spans are aggregated into a summary span, the following data from non-template spans is lost:| Data | Behavior |
|---|---|
| Span Events | Events from the template (slowest) span are preserved |
| Span Links | Links from the template span are preserved |
| Attributes | Non-matching attribute values are lost |
| Individual Timestamps | Original start/end times replaced by the groupβs time range |
| SpanIDs | Original SpanIDs are replaced by a single summary SpanID |
Aggregation Attributes
The following attributes are added to the summary span (shown with defaultaggregation_attribute_prefix: "aggregation."):
| Attribute | Type | Description |
|---|---|---|
<prefix>is_summary | bool | Always true to identify summary spans |
<prefix>span_count | int64 | Number of spans that were aggregated |
<prefix>duration_min_ns | int64 | Minimum duration in nanoseconds |
<prefix>duration_max_ns | int64 | Maximum duration in nanoseconds |
<prefix>duration_avg_ns | int64 | Average duration in nanoseconds |
<prefix>duration_total_ns | int64 | Total duration in nanoseconds |
Pipeline Placement
This processor is designed to work best when placed after processors that ensure complete traces are available:Example
Basic Example
A trace with repeated database queries (some failing): Before Processing:min_spans_to_aggregate: 2):
Recursive Parent Aggregation Example
When spans are aggregated, the processor also checks if their parent spans can be aggregated. Parent spans are eligible for aggregation when:- All of their children are being aggregated
- They share the same name, kind, and status code with other eligible parents
- They are not root spans (must have a parent)
- At least 2 parents meet the criteria
min_spans_to_aggregate: 2, group_by_attributes: ["db.op"]):
| Span | Result | Reason |
|---|---|---|
| 3x handler (OK) with SELECT children | Aggregated | All children aggregated, same name+kind+status |
| 3x SELECT (OK) under handler | Aggregated | Same name + kind + status + attributes + parent name |
| 2x handler (Error) with SELECT children | Aggregated | All children aggregated, same name+kind+status |
| 2x SELECT (Error) under handler | Aggregated | Same name + kind + status + attributes + parent name |
| handler (OK) with INSERT child | Unchanged | Child not aggregated (only 1 INSERT) |
| INSERT (OK) | Unchanged | Below threshold (only 1 span) |
| worker (OK) | Unchanged | Child not aggregated |
| SELECT (OK) under worker | Unchanged | Different parent name than other SELECTs |
Limitations
- Requires complete traces for accurate leaf detection
- Summary span inherits attributes from the slowest span in the group
- Parent spans are only aggregated when ALL their children are aggregated
Consistent Probability Sampling (CPS) Compatibility
The processor is designed to be compatible with Consistent Probability Sampling (CPS). CPS uses TraceState to carry sampling metadata (ot=th:...;rv:...) where:
th(threshold) indicates the sampling probability thresholdrv(randomness value) provides consistent randomness for sampling decisions
th value) because:
- The
rvvalue affects sampling decisions - Vendor-specific keys may have semantic meaning
- Key ordering may be significant
Telemetry
The processor emits the following metrics to help monitor its operation:Counters
| Metric | Description |
|---|---|
otelcol_processor_spanpruning_spans_received | Total number of spans received by the processor |
otelcol_processor_spanpruning_spans_pruned | Total number of spans removed by aggregation |
otelcol_processor_spanpruning_aggregations_created | Total number of aggregation summary spans created |
otelcol_processor_spanpruning_traces_processed | Total number of traces processed |
Histograms
| Metric | Description |
|---|---|
otelcol_processor_spanpruning_aggregation_group_size | Distribution of the number of spans per aggregation group |
otelcol_processor_spanpruning_processing_duration | Time taken to process each batch of traces (in seconds) |
- Monitor the effectiveness of span pruning (compare
spans_receivedvsspans_pruned) - Track the compression ratio achieved by aggregation
- Identify processing bottlenecks via
processing_duration - Understand aggregation patterns via
aggregation_group_size
Configuration
Example Configuration
Last generated: 2026-04-13