-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metrics: Define map metrics as custom collectors
Signed-off-by: Anna Kapuscinska <anna@isovalent.com>
- Loading branch information
Showing
9 changed files
with
180 additions
and
113 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
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,30 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package eventcache | ||
|
||
import ( | ||
"github.com/cilium/tetragon/pkg/metrics/mapmetrics" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
// bpfCollector implements prometheus.Collector. It collects metrics directly from BPF maps. | ||
type bpfCollector struct{} | ||
|
||
func NewBPFCollector() *bpfCollector { | ||
return &bpfCollector{} | ||
} | ||
|
||
func (c bpfCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- mapmetrics.MapSize.Desc() | ||
} | ||
|
||
func (c bpfCollector) Collect(ch chan<- prometheus.Metric) { | ||
ec := Get() | ||
if ec != nil { | ||
ch <- mapmetrics.MapSize.MustMetric( | ||
float64(ec.len()), | ||
"eventcache", "0", | ||
) | ||
} | ||
} |
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,43 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package metrics | ||
|
||
import "github.com/prometheus/client_golang/prometheus" | ||
|
||
// BPF metric types store metric descriptors, but not labels or values. | ||
// Instead, they expose a method to create a new prometheus.Metric. These types | ||
// are intended to be used in custom collectors exposing values directly from | ||
// BPF maps. | ||
|
||
type bpfCounter struct { | ||
desc *prometheus.Desc | ||
} | ||
|
||
func NewBPFCounter(desc *prometheus.Desc) *bpfCounter { | ||
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) *bpfGauge { | ||
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...) | ||
} |
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
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
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
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
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
Oops, something went wrong.