tetragon
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Authors of Tetragon
3
4package metrics5
6import "github.com/prometheus/client_golang/prometheus"7
8// BPFMetric represents a metric read directly from a BPF map.
9// It's intended to be used in custom collectors. The interface doesn't provide
10// any validation, so it's up to the collector implementer to guarantee the
11// metrics consistency.
12type BPFMetric interface {13Desc() *prometheus.Desc14MustMetric(value float64, labelValues ...string) prometheus.Metric15}
16
17type bpfCounter struct {18desc *prometheus.Desc19}
20
21func NewBPFCounter(desc *prometheus.Desc) BPFMetric {22return &bpfCounter{desc: desc}23}
24
25func (c *bpfCounter) Desc() *prometheus.Desc {26return c.desc27}
28
29func (c *bpfCounter) MustMetric(value float64, labelValues ...string) prometheus.Metric {30return prometheus.MustNewConstMetric(c.desc, prometheus.CounterValue, value, labelValues...)31}
32
33type bpfGauge struct {34desc *prometheus.Desc35}
36
37func NewBPFGauge(desc *prometheus.Desc) BPFMetric {38return &bpfGauge{desc: desc}39}
40
41func (g *bpfGauge) Desc() *prometheus.Desc {42return g.desc43}
44
45func (g *bpfGauge) MustMetric(value float64, labelValues ...string) prometheus.Metric {46return prometheus.MustNewConstMetric(g.desc, prometheus.GaugeValue, value, labelValues...)47}
48