-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |