@@ -382,19 +382,20 @@ type HistogramOpts struct {
382
382
Buckets []float64
383
383
384
384
// If SparseBucketsFactor is greater than one, sparse buckets are used
385
- // (in addition to the regular buckets, if defined above). Sparse
386
- // buckets are exponential buckets covering the whole float64 range
387
- // (with the exception of the “zero” bucket, see
388
- // SparseBucketsZeroThreshold below). From any one bucket to the next,
389
- // the width of the bucket grows by a constant factor.
390
- // SparseBucketsFactor provides an upper bound for this factor
391
- // (exception see below). The smaller SparseBucketsFactor, the more
392
- // buckets will be used and thus the more costly the histogram will
393
- // become. A generally good trade-off between cost and accuracy is a
394
- // value of 1.1 (each bucket is at most 10% wider than the previous
395
- // one), which will result in each power of two divided into 8 buckets
396
- // (e.g. there will be 8 buckets between 1 and 2, same as between 2 and
397
- // 4, and 4 and 8, etc.).
385
+ // (in addition to the regular buckets, if defined above). A histogram
386
+ // with sparse buckets will be ingested as a native histogram by a
387
+ // Prometheus server with that feature enable. Sparse buckets are
388
+ // exponential buckets covering the whole float64 range (with the
389
+ // exception of the “zero” bucket, see SparseBucketsZeroThreshold
390
+ // below). From any one bucket to the next, the width of the bucket
391
+ // grows by a constant factor. SparseBucketsFactor provides an upper
392
+ // bound for this factor (exception see below). The smaller
393
+ // SparseBucketsFactor, the more buckets will be used and thus the more
394
+ // costly the histogram will become. A generally good trade-off between
395
+ // cost and accuracy is a value of 1.1 (each bucket is at most 10% wider
396
+ // than the previous one), which will result in each power of two
397
+ // divided into 8 buckets (e.g. there will be 8 buckets between 1 and 2,
398
+ // same as between 2 and 4, and 4 and 8, etc.).
398
399
//
399
400
// Details about the actually used factor: The factor is calculated as
400
401
// 2^(2^n), where n is an integer number between (and including) -8 and
@@ -723,18 +724,18 @@ func (h *histogram) Write(out *dto.Metric) error {
723
724
his .Bucket = append (his .Bucket , b )
724
725
}
725
726
if h .sparseSchema > math .MinInt32 {
726
- his .SbZeroThreshold = proto .Float64 (math .Float64frombits (atomic .LoadUint64 (& coldCounts .sparseZeroThresholdBits )))
727
- his .SbSchema = proto .Int32 (atomic .LoadInt32 (& coldCounts .sparseSchema ))
727
+ his .ZeroThreshold = proto .Float64 (math .Float64frombits (atomic .LoadUint64 (& coldCounts .sparseZeroThresholdBits )))
728
+ his .Schema = proto .Int32 (atomic .LoadInt32 (& coldCounts .sparseSchema ))
728
729
zeroBucket := atomic .LoadUint64 (& coldCounts .sparseZeroBucket )
729
730
730
731
defer func () {
731
732
coldCounts .sparseBucketsPositive .Range (addAndReset (& hotCounts .sparseBucketsPositive , & hotCounts .sparseBucketsNumber ))
732
733
coldCounts .sparseBucketsNegative .Range (addAndReset (& hotCounts .sparseBucketsNegative , & hotCounts .sparseBucketsNumber ))
733
734
}()
734
735
735
- his .SbZeroCount = proto .Uint64 (zeroBucket )
736
- his .SbNegative = makeSparseBuckets (& coldCounts .sparseBucketsNegative )
737
- his .SbPositive = makeSparseBuckets (& coldCounts .sparseBucketsPositive )
736
+ his .ZeroCount = proto .Uint64 (zeroBucket )
737
+ his .NegativeSpan , his . NegativeDelta = makeSparseBuckets (& coldCounts .sparseBucketsNegative )
738
+ his .PositiveSpan , his . PositiveDelta = makeSparseBuckets (& coldCounts .sparseBucketsPositive )
738
739
}
739
740
addAndResetCounts (hotCounts , coldCounts )
740
741
return nil
@@ -1235,7 +1236,7 @@ func pickSparseSchema(bucketFactor float64) int32 {
1235
1236
}
1236
1237
}
1237
1238
1238
- func makeSparseBuckets (buckets * sync.Map ) * dto.SparseBuckets {
1239
+ func makeSparseBuckets (buckets * sync.Map ) ([] * dto.BucketSpan , [] int64 ) {
1239
1240
var ii []int
1240
1241
buckets .Range (func (k , v interface {}) bool {
1241
1242
ii = append (ii , k .(int ))
@@ -1244,16 +1245,19 @@ func makeSparseBuckets(buckets *sync.Map) *dto.SparseBuckets {
1244
1245
sort .Ints (ii )
1245
1246
1246
1247
if len (ii ) == 0 {
1247
- return nil
1248
+ return nil , nil
1248
1249
}
1249
1250
1250
- sbs := dto.SparseBuckets {}
1251
- var prevCount int64
1252
- var nextI int
1251
+ var (
1252
+ spans []* dto.BucketSpan
1253
+ deltas []int64
1254
+ prevCount int64
1255
+ nextI int
1256
+ )
1253
1257
1254
1258
appendDelta := func (count int64 ) {
1255
- * sbs . Span [len (sbs . Span )- 1 ].Length ++
1256
- sbs . Delta = append (sbs . Delta , count - prevCount )
1259
+ * spans [len (spans )- 1 ].Length ++
1260
+ deltas = append (deltas , count - prevCount )
1257
1261
prevCount = count
1258
1262
}
1259
1263
@@ -1270,7 +1274,7 @@ func makeSparseBuckets(buckets *sync.Map) *dto.SparseBuckets {
1270
1274
// We have to create a new span, either because we are
1271
1275
// at the very beginning, or because we have found a gap
1272
1276
// of more than two buckets.
1273
- sbs . Span = append (sbs . Span , & dto.SparseBuckets_Span {
1277
+ spans = append (spans , & dto.BucketSpan {
1274
1278
Offset : proto .Int32 (iDelta ),
1275
1279
Length : proto .Uint32 (0 ),
1276
1280
})
@@ -1284,7 +1288,7 @@ func makeSparseBuckets(buckets *sync.Map) *dto.SparseBuckets {
1284
1288
appendDelta (count )
1285
1289
nextI = i + 1
1286
1290
}
1287
- return & sbs
1291
+ return spans , deltas
1288
1292
}
1289
1293
1290
1294
// addToSparseBucket increments the sparse bucket at key by the provided
0 commit comments