segmentSearchResponse splits response into one or more SearchResponse values, each within maxSize bytes (by proto Size()). Metrics are included in every segment.
(response *tempopb.SearchResponse, maxSize int)
| 160 | // segmentSearchResponse splits response into one or more SearchResponse values, each within |
| 161 | // maxSize bytes (by proto Size()). Metrics are included in every segment. |
| 162 | func segmentSearchResponse(response *tempopb.SearchResponse, maxSize int) []*tempopb.SearchResponse { |
| 163 | if maxSize <= 0 { |
| 164 | return []*tempopb.SearchResponse{response} |
| 165 | } |
| 166 | |
| 167 | var out []*tempopb.SearchResponse |
| 168 | var current *tempopb.SearchResponse |
| 169 | var currentSz int |
| 170 | |
| 171 | startNextPacket := func() { |
| 172 | current = &tempopb.SearchResponse{ |
| 173 | Metrics: response.Metrics, |
| 174 | } |
| 175 | currentSz = current.Size() |
| 176 | out = append(out, current) |
| 177 | } |
| 178 | |
| 179 | startNextPacket() |
| 180 | |
| 181 | for _, t := range response.Traces { |
| 182 | traceSz := protoSizeMath(t) |
| 183 | |
| 184 | // Start a new packet if there isn't room for this entry, |
| 185 | // unless it's the first one, that way we always try to fit at least one. |
| 186 | if len(current.Traces) > 0 && currentSz+traceSz > maxSize { |
| 187 | startNextPacket() |
| 188 | } |
| 189 | |
| 190 | current.Traces = append(current.Traces, t) |
| 191 | currentSz += traceSz |
| 192 | } |
| 193 | |
| 194 | return out |
| 195 | } |