istio

Форк
0
/
distribution.go 
70 строк · 1.8 Кб
1
// Copyright Istio 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 monitoring
16

17
import (
18
	"context"
19

20
	api "go.opentelemetry.io/otel/metric"
21

22
	"istio.io/istio/pkg/log"
23
)
24

25
type distribution struct {
26
	baseMetric
27
	d api.Float64Histogram
28
	// precomputedRecordOption is just a precomputation to avoid allocations on each record call
29
	precomputedRecordOption []api.RecordOption
30
}
31

32
var _ Metric = &distribution{}
33

34
func newDistribution(o options) *distribution {
35
	d, err := meter().Float64Histogram(o.name,
36
		api.WithDescription(o.description),
37
		api.WithUnit(string(o.unit)))
38
	if err != nil {
39
		log.Fatalf("failed to create distribution: %v", err)
40
	}
41
	r := &distribution{d: d}
42
	r.baseMetric = baseMetric{
43
		name: o.name,
44
		rest: r,
45
	}
46
	return r
47
}
48

49
func (f *distribution) Record(value float64) {
50
	f.runRecordHook(value)
51
	if f.precomputedRecordOption != nil {
52
		f.d.Record(context.Background(), value, f.precomputedRecordOption...)
53
	} else {
54
		f.d.Record(context.Background(), value)
55
	}
56
}
57

58
func (f *distribution) With(labelValues ...LabelValue) Metric {
59
	attrs, set := rebuildAttributes(f.baseMetric, labelValues)
60
	nm := &distribution{
61
		d:                       f.d,
62
		precomputedRecordOption: []api.RecordOption{api.WithAttributeSet(set)},
63
	}
64
	nm.baseMetric = baseMetric{
65
		name:  f.name,
66
		attrs: attrs,
67
		rest:  nm,
68
	}
69
	return nm
70
}
71

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

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

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

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