Skip to content

Commit

Permalink
metrics: Add BPF metrics types
Browse files Browse the repository at this point in the history
These types are slim helpers intended to be used in custom collectors that read
metrics directly from BPF maps.

Signed-off-by: Anna Kapuscinska <anna@isovalent.com>
  • Loading branch information
lambdanis committed Oct 3, 2023
1 parent 845f592 commit c95db9e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/metrics/bpfmetric.go
Original file line number Diff line number Diff line change
@@ -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...)
}

0 comments on commit c95db9e

Please sign in to comment.