knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/observability/metrics/k8s/instruments.go (about)

     1  /*
     2  Copyright 2025 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package k8s
    18  
    19  import (
    20  	"context"
    21  
    22  	"go.opentelemetry.io/otel/attribute"
    23  	"go.opentelemetry.io/otel/metric"
    24  
    25  	"k8s.io/client-go/util/workqueue"
    26  )
    27  
    28  type gauge struct {
    29  	m     metric.Int64UpDownCounter
    30  	attrs attribute.Set
    31  }
    32  
    33  func (g *gauge) Inc() {
    34  	g.m.Add(context.Background(), 1, metric.WithAttributeSet(g.attrs))
    35  }
    36  
    37  func (g *gauge) Dec() {
    38  	g.m.Add(context.Background(), -1, metric.WithAttributeSet(g.attrs))
    39  }
    40  
    41  type counter struct {
    42  	m     metric.Int64Counter
    43  	attrs attribute.Set
    44  }
    45  
    46  func (c *counter) Inc() {
    47  	c.m.Add(context.Background(), 1, metric.WithAttributeSet(c.attrs))
    48  }
    49  
    50  type histogram struct {
    51  	m     metric.Float64Histogram
    52  	attrs attribute.Set
    53  }
    54  
    55  func (h *histogram) Observe(val float64) {
    56  	h.m.Record(context.Background(), val, metric.WithAttributeSet(h.attrs))
    57  }
    58  
    59  type settableGauge struct {
    60  	m     metric.Float64Gauge
    61  	attrs attribute.Set
    62  }
    63  
    64  func (s *settableGauge) Set(val float64) {
    65  	s.m.Record(context.Background(), val, metric.WithAttributeSet(s.attrs))
    66  }
    67  
    68  var (
    69  	_ workqueue.GaugeMetric         = (*gauge)(nil)
    70  	_ workqueue.CounterMetric       = (*counter)(nil)
    71  	_ workqueue.HistogramMetric     = (*histogram)(nil)
    72  	_ workqueue.SettableGaugeMetric = (*settableGauge)(nil)
    73  )