From 686ead95a38bd5f77ba1f2e0c1643d5ec5bdd146 Mon Sep 17 00:00:00 2001 From: Anna Kapuscinska Date: Sat, 30 Sep 2023 01:28:06 +0100 Subject: [PATCH] metrics: Add BPF metrics types These types are slim helpers intended to be used in custom collectors that read metrics directly from BPF maps. Signed-off-by: Anna Kapuscinska --- pkg/metrics/bpfmetric.go | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 pkg/metrics/bpfmetric.go diff --git a/pkg/metrics/bpfmetric.go b/pkg/metrics/bpfmetric.go new file mode 100644 index 00000000000..3a660e63eb9 --- /dev/null +++ b/pkg/metrics/bpfmetric.go @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Authors of Tetragon + +package metrics + +import "github.com/prometheus/client_golang/prometheus" + +// BPFMetric represents a metric read directly from a BPF map. +// It's intended to be used in custom collectors. The interface doesn't provide +// any validation, so it's up to the collector implementer to guarantee the +// metrics consistency. +type BPFMetric interface { + Desc() *prometheus.Desc + MustMetric(value float64, labelValues ...string) prometheus.Metric +} + +type bpfCounter struct { + desc *prometheus.Desc +} + +func NewBPFCounter(desc *prometheus.Desc) BPFMetric { + return &bpfCounter{desc: desc} +} + +func (c *bpfCounter) Desc() *prometheus.Desc { + return c.desc +} + +func (c *bpfCounter) MustMetric(value float64, labelValues ...string) prometheus.Metric { + return prometheus.MustNewConstMetric(c.desc, prometheus.CounterValue, value, labelValues...) +} + +type bpfGauge struct { + desc *prometheus.Desc +} + +func NewBPFGauge(desc *prometheus.Desc) BPFMetric { + return &bpfGauge{desc: desc} +} + +func (g *bpfGauge) Desc() *prometheus.Desc { + return g.desc +} + +func (g *bpfGauge) MustMetric(value float64, labelValues ...string) prometheus.Metric { + return prometheus.MustNewConstMetric(g.desc, prometheus.GaugeValue, value, labelValues...) +}