istio
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
15package monitoring
16
17import (
18"context"
19
20api "go.opentelemetry.io/otel/metric"
21
22"istio.io/istio/pkg/log"
23)
24
25type distribution struct {
26baseMetric
27d api.Float64Histogram
28// precomputedRecordOption is just a precomputation to avoid allocations on each record call
29precomputedRecordOption []api.RecordOption
30}
31
32var _ Metric = &distribution{}
33
34func newDistribution(o options) *distribution {
35d, err := meter().Float64Histogram(o.name,
36api.WithDescription(o.description),
37api.WithUnit(string(o.unit)))
38if err != nil {
39log.Fatalf("failed to create distribution: %v", err)
40}
41r := &distribution{d: d}
42r.baseMetric = baseMetric{
43name: o.name,
44rest: r,
45}
46return r
47}
48
49func (f *distribution) Record(value float64) {
50f.runRecordHook(value)
51if f.precomputedRecordOption != nil {
52f.d.Record(context.Background(), value, f.precomputedRecordOption...)
53} else {
54f.d.Record(context.Background(), value)
55}
56}
57
58func (f *distribution) With(labelValues ...LabelValue) Metric {
59attrs, set := rebuildAttributes(f.baseMetric, labelValues)
60nm := &distribution{
61d: f.d,
62precomputedRecordOption: []api.RecordOption{api.WithAttributeSet(set)},
63}
64nm.baseMetric = baseMetric{
65name: f.name,
66attrs: attrs,
67rest: nm,
68}
69return nm
70}
71