istio
70 строк · 1.7 Кб
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 monitoring16
17import (18"context"19
20api "go.opentelemetry.io/otel/metric"21
22"istio.io/istio/pkg/log"23)
24
25type counter struct {26baseMetric
27c api.Float64Counter28// precomputedAddOption is just a precomputation to avoid allocations on each record call29precomputedAddOption []api.AddOption30}
31
32var _ Metric = &counter{}33
34func newCounter(o options) *counter {35c, err := meter().Float64Counter(o.name,36api.WithDescription(o.description),37api.WithUnit(string(o.unit)))38if err != nil {39log.Fatalf("failed to create counter: %v", err)40}41r := &counter{c: c}42r.baseMetric = baseMetric{43name: o.name,44rest: r,45}46return r47}
48
49func (f *counter) Record(value float64) {50f.runRecordHook(value)51if f.precomputedAddOption != nil {52f.c.Add(context.Background(), value, f.precomputedAddOption...)53} else {54f.c.Add(context.Background(), value)55}56}
57
58func (f *counter) With(labelValues ...LabelValue) Metric {59attrs, set := rebuildAttributes(f.baseMetric, labelValues)60nm := &counter{61c: f.c,62precomputedAddOption: []api.AddOption{api.WithAttributeSet(set)},63}64nm.baseMetric = baseMetric{65name: f.name,66attrs: attrs,67rest: nm,68}69return nm70}
71