| 
									
										
										
										
											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-09-03 05:35:49 +08:00
										 |  |  | // HistogramStatsIterator is an iterator that returns histogram objects that
 | 
					
						
							|  |  |  | // 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. The Next and Seek methods of the iterator
 | 
					
						
							|  |  |  | // will never return ValHistogram, but ValFloatHistogram instead. Effectively,
 | 
					
						
							|  |  |  | // the iterator enforces conversion of (integer) Histogram to FloatHistogram.
 | 
					
						
							|  |  |  | // The AtHistogram method must not be called (and will panic).
 | 
					
						
							| 
									
										
										
										
											2025-06-19 05:53:46 +08:00
										 |  |  | type HistogramStatsIterator struct { | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	chunkenc.Iterator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	currentFH *histogram.FloatHistogram | 
					
						
							|  |  |  | 	lastFH    *histogram.FloatHistogram | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							|  |  |  | 		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 | 
					
						
							| 
									
										
										
										
											2025-09-03 05:35:49 +08:00
										 |  |  | 	hsi.lastFH = nil | 
					
						
							| 
									
										
										
										
											2025-06-19 05:53:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-03 05:35:49 +08:00
										 |  |  | // Next mostly relays to the underlying iterator, but changes a ValHistogram
 | 
					
						
							|  |  |  | // return into a ValFloatHistogram return.
 | 
					
						
							|  |  |  | func (hsi *HistogramStatsIterator) Next() chunkenc.ValueType { | 
					
						
							|  |  |  | 	vt := hsi.Iterator.Next() | 
					
						
							|  |  |  | 	if vt == chunkenc.ValHistogram { | 
					
						
							|  |  |  | 		return chunkenc.ValFloatHistogram | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-09-03 05:35:49 +08:00
										 |  |  | 	return vt | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-03 05:35:49 +08:00
										 |  |  | // Seek mostly relays to the underlying iterator, but changes a ValHistogram
 | 
					
						
							|  |  |  | // return into a ValFloatHistogram return.
 | 
					
						
							|  |  |  | func (hsi *HistogramStatsIterator) Seek(t int64) chunkenc.ValueType { | 
					
						
							|  |  |  | 	vt := hsi.Iterator.Seek(t) | 
					
						
							|  |  |  | 	if vt == chunkenc.ValHistogram { | 
					
						
							|  |  |  | 		return chunkenc.ValFloatHistogram | 
					
						
							| 
									
										
										
										
											2024-07-31 17:17:09 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-09-03 05:35:49 +08:00
										 |  |  | 	return vt | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-07-31 17:17:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-03 05:35:49 +08:00
										 |  |  | // AtHistogram must never be called.
 | 
					
						
							|  |  |  | func (*HistogramStatsIterator) AtHistogram(*histogram.Histogram) (int64, *histogram.Histogram) { | 
					
						
							|  |  |  | 	panic("HistogramStatsIterator.AtHistogram must never be called") | 
					
						
							| 
									
										
										
										
											2024-06-06 23:17:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-03 05:35:49 +08:00
										 |  |  | // AtFloatHistogram returns the next timestamp/float histogram pair. The method
 | 
					
						
							|  |  |  | // performs a counter reset detection on the fly. It will return an explicit
 | 
					
						
							|  |  |  | // hint (not UnknownCounterReset) if the previous sample has been accessed with
 | 
					
						
							|  |  |  | // the same iterator.
 | 
					
						
							| 
									
										
										
										
											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) setLastFH(fh *histogram.FloatHistogram) { | 
					
						
							|  |  |  | 	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-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 | 
					
						
							| 
									
										
										
										
											2025-09-03 05:35:49 +08:00
										 |  |  | 	if prevFH == nil { | 
					
						
							|  |  |  | 		// 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.
 | 
					
						
							|  |  |  | 		return histogram.UnknownCounterReset | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | } |