inspektor-gadget

Форк
0
89 строк · 2.3 Кб
1
// Copyright 2023 The Inspektor Gadget authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package prometheus
16

17
import (
18
	"math"
19
	"strings"
20

21
	"github.com/shopspring/decimal"
22
	log "github.com/sirupsen/logrus"
23

24
	"github.com/inspektor-gadget/inspektor-gadget/pkg/prometheus/config"
25
)
26

27
type BucketType string
28

29
const (
30
	BucketTypeExp2   BucketType = "exp2"
31
	BucketTypeLinear BucketType = "linear"
32
)
33

34
var AllBucketTypes = map[string]struct{}{
35
	string(BucketTypeExp2):   {},
36
	string(BucketTypeLinear): {},
37
}
38

39
type BucketConfig struct {
40
	Type       BucketType
41
	Min        int
42
	Max        int
43
	Multiplier float64
44
}
45

46
func bucketConfigsFromConfig(c *config.Config) map[string]*BucketConfig {
47
	buckets := make(map[string]*BucketConfig)
48
	for _, m := range c.Metrics {
49
		if strings.ToLower(m.Type) == "histogram" {
50
			buckets[m.Name] = &BucketConfig{
51
				Type:       BucketType(m.Bucket.Type),
52
				Min:        m.Bucket.Min,
53
				Max:        m.Bucket.Max,
54
				Multiplier: m.Bucket.Multiplier,
55
			}
56
		}
57
	}
58
	return buckets
59
}
60

61
func (c *BucketConfig) buckets() []float64 {
62
	switch c.Type {
63
	case BucketTypeExp2:
64
		return exp2Buckets(c.Min, c.Max, c.Multiplier)
65
	case BucketTypeLinear:
66
		return linearBuckets(c.Min, c.Max, c.Multiplier)
67
	default:
68
		log.Warnf("unknown bucket type: %s", c.Type)
69
		return nil
70
	}
71
}
72

73
func exp2Buckets(min, max int, multiplier float64) []float64 {
74
	buckets := make([]float64, 0, max-min)
75
	for i := min; i < max; i++ {
76
		buckets = append(buckets, math.Pow(2, float64(i))*multiplier)
77
	}
78
	return buckets
79
}
80

81
func linearBuckets(min, max int, multiplier float64) []float64 {
82
	buckets := make([]float64, 0, max-min)
83
	for i := min; i < max; i++ {
84
		bucketId := decimal.NewFromInt(int64(i))
85
		bucketValue, _ := bucketId.Mul(decimal.NewFromFloat(multiplier)).Float64()
86
		buckets = append(buckets, bucketValue)
87
	}
88
	return buckets
89
}
90

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.