| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | // Copyright 2024 The Prometheus Authors
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package promql | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/model/histogram" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/model/value" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/tsdb/chunkenc" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-19 05:53:46 +08:00
										 |  |  | // HistogramStatsIterator is an iterator that returns histogram objects
 | 
					
						
							|  |  |  | // which have only their sum and count values populated. The iterator handles
 | 
					
						
							|  |  |  | // counter reset detection internally and sets the counter reset hint accordingly
 | 
					
						
							|  |  |  | // in each returned histogram object.
 | 
					
						
							|  |  |  | type HistogramStatsIterator struct { | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	chunkenc.Iterator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	currentH *histogram.Histogram | 
					
						
							|  |  |  | 	lastH    *histogram.Histogram | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	currentFH *histogram.FloatHistogram | 
					
						
							|  |  |  | 	lastFH    *histogram.FloatHistogram | 
					
						
							| 
									
										
										
										
											2025-06-19 05:53:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	currentSeriesRead bool | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-19 05:53:46 +08:00
										 |  |  | // NewHistogramStatsIterator creates a new HistogramStatsIterator.
 | 
					
						
							|  |  |  | func NewHistogramStatsIterator(it chunkenc.Iterator) *HistogramStatsIterator { | 
					
						
							|  |  |  | 	return &HistogramStatsIterator{ | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 		Iterator:  it, | 
					
						
							|  |  |  | 		currentH:  &histogram.Histogram{}, | 
					
						
							|  |  |  | 		currentFH: &histogram.FloatHistogram{}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-19 05:53:46 +08:00
										 |  |  | // Reset resets this iterator for use with a new underlying iterator, reusing
 | 
					
						
							|  |  |  | // objects already allocated where possible.
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | func (hsi *HistogramStatsIterator) Reset(it chunkenc.Iterator) { | 
					
						
							|  |  |  | 	hsi.Iterator = it | 
					
						
							|  |  |  | 	hsi.currentSeriesRead = false | 
					
						
							| 
									
										
										
										
											2025-06-19 05:53:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | // AtHistogram returns the next timestamp/histogram pair. The counter reset
 | 
					
						
							|  |  |  | // detection is guaranteed to be correct only when the caller does not switch
 | 
					
						
							|  |  |  | // between AtHistogram and AtFloatHistogram calls.
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | func (hsi *HistogramStatsIterator) AtHistogram(h *histogram.Histogram) (int64, *histogram.Histogram) { | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	var t int64 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 	t, hsi.currentH = hsi.Iterator.AtHistogram(hsi.currentH) | 
					
						
							|  |  |  | 	if value.IsStaleNaN(hsi.currentH.Sum) { | 
					
						
							|  |  |  | 		h = &histogram.Histogram{Sum: hsi.currentH.Sum} | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 		return t, h | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if h == nil { | 
					
						
							|  |  |  | 		h = &histogram.Histogram{ | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 			CounterResetHint: hsi.getResetHint(hsi.currentH), | 
					
						
							|  |  |  | 			Count:            hsi.currentH.Count, | 
					
						
							|  |  |  | 			Sum:              hsi.currentH.Sum, | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 		hsi.setLastH(hsi.currentH) | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 		return t, h | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 17:17:09 +08:00
										 |  |  | 	returnValue := histogram.Histogram{ | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 		CounterResetHint: hsi.getResetHint(hsi.currentH), | 
					
						
							|  |  |  | 		Count:            hsi.currentH.Count, | 
					
						
							|  |  |  | 		Sum:              hsi.currentH.Sum, | 
					
						
							| 
									
										
										
										
											2024-07-31 17:17:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	returnValue.CopyTo(h) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 	hsi.setLastH(hsi.currentH) | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	return t, h | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // AtFloatHistogram returns the next timestamp/float histogram pair. The counter
 | 
					
						
							|  |  |  | // reset detection is guaranteed to be correct only when the caller does not
 | 
					
						
							|  |  |  | // switch between AtHistogram and AtFloatHistogram calls.
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | func (hsi *HistogramStatsIterator) AtFloatHistogram(fh *histogram.FloatHistogram) (int64, *histogram.FloatHistogram) { | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	var t int64 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 	t, hsi.currentFH = hsi.Iterator.AtFloatHistogram(hsi.currentFH) | 
					
						
							|  |  |  | 	if value.IsStaleNaN(hsi.currentFH.Sum) { | 
					
						
							|  |  |  | 		return t, &histogram.FloatHistogram{Sum: hsi.currentFH.Sum} | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if fh == nil { | 
					
						
							|  |  |  | 		fh = &histogram.FloatHistogram{ | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 			CounterResetHint: hsi.getFloatResetHint(hsi.currentFH.CounterResetHint), | 
					
						
							|  |  |  | 			Count:            hsi.currentFH.Count, | 
					
						
							|  |  |  | 			Sum:              hsi.currentFH.Sum, | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 		hsi.setLastFH(hsi.currentFH) | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 		return t, fh | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 17:17:09 +08:00
										 |  |  | 	returnValue := histogram.FloatHistogram{ | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 		CounterResetHint: hsi.getFloatResetHint(hsi.currentFH.CounterResetHint), | 
					
						
							|  |  |  | 		Count:            hsi.currentFH.Count, | 
					
						
							|  |  |  | 		Sum:              hsi.currentFH.Sum, | 
					
						
							| 
									
										
										
										
											2024-07-31 17:17:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	returnValue.CopyTo(fh) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 	hsi.setLastFH(hsi.currentFH) | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	return t, fh | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | func (hsi *HistogramStatsIterator) setLastH(h *histogram.Histogram) { | 
					
						
							|  |  |  | 	hsi.lastFH = nil | 
					
						
							|  |  |  | 	if hsi.lastH == nil { | 
					
						
							|  |  |  | 		hsi.lastH = h.Copy() | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 		h.CopyTo(hsi.lastH) | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-06-19 05:53:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 	hsi.currentSeriesRead = true | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | func (hsi *HistogramStatsIterator) setLastFH(fh *histogram.FloatHistogram) { | 
					
						
							|  |  |  | 	hsi.lastH = nil | 
					
						
							|  |  |  | 	if hsi.lastFH == nil { | 
					
						
							|  |  |  | 		hsi.lastFH = fh.Copy() | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 		fh.CopyTo(hsi.lastFH) | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-06-19 05:53:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 	hsi.currentSeriesRead = true | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | func (hsi *HistogramStatsIterator) getFloatResetHint(hint histogram.CounterResetHint) histogram.CounterResetHint { | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	if hint != histogram.UnknownCounterReset { | 
					
						
							|  |  |  | 		return hint | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 	prevFH := hsi.lastFH | 
					
						
							|  |  |  | 	if prevFH == nil || !hsi.currentSeriesRead { | 
					
						
							|  |  |  | 		if hsi.lastH == nil || !hsi.currentSeriesRead { | 
					
						
							| 
									
										
										
										
											2025-06-04 16:24:50 +08:00
										 |  |  | 			// We don't know if there's a counter reset.
 | 
					
						
							|  |  |  | 			return histogram.UnknownCounterReset | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 		prevFH = hsi.lastH.ToFloat(nil) | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 	if hsi.currentFH.DetectReset(prevFH) { | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 		return histogram.CounterReset | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return histogram.NotCounterReset | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | func (hsi *HistogramStatsIterator) getResetHint(h *histogram.Histogram) histogram.CounterResetHint { | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	if h.CounterResetHint != histogram.UnknownCounterReset { | 
					
						
							|  |  |  | 		return h.CounterResetHint | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-06-04 16:24:50 +08:00
										 |  |  | 	var prevFH *histogram.FloatHistogram | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 	if hsi.lastH == nil || !hsi.currentSeriesRead { | 
					
						
							|  |  |  | 		if hsi.lastFH == nil || !hsi.currentSeriesRead { | 
					
						
							|  |  |  | 			// We don't know if there's a counter reset. Note that
 | 
					
						
							|  |  |  | 			// this generally will trigger an explicit counter reset
 | 
					
						
							|  |  |  | 			// detection by the PromQL engine, which in turn isn't
 | 
					
						
							|  |  |  | 			// as reliable in this case because the PromQL engine
 | 
					
						
							|  |  |  | 			// will not see the buckets. However, we can assume that
 | 
					
						
							|  |  |  | 			// in cases where the counter reset detection is
 | 
					
						
							|  |  |  | 			// relevant, an iteration through the series has
 | 
					
						
							|  |  |  | 			// happened, and therefore we do not end up here in the
 | 
					
						
							|  |  |  | 			// first place.
 | 
					
						
							| 
									
										
										
										
											2025-06-04 16:24:50 +08:00
										 |  |  | 			return histogram.UnknownCounterReset | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 		prevFH = hsi.lastFH | 
					
						
							| 
									
										
										
										
											2025-06-04 16:24:50 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2025-08-28 21:00:25 +08:00
										 |  |  | 		prevFH = hsi.lastH.ToFloat(nil) | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-06-04 16:24:50 +08:00
										 |  |  | 	fh := h.ToFloat(nil) | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	if fh.DetectReset(prevFH) { | 
					
						
							|  |  |  | 		return histogram.CounterReset | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return histogram.NotCounterReset | 
					
						
							|  |  |  | } |