Skip to content

Commit

Permalink
pkg/metrics: add metrics for policy kernel memory use
Browse files Browse the repository at this point in the history
Similarly to TracingPolicy state, you can retrieve this information
through the gRPC API by listing the policies or using the metrics
interface.

Signed-off-by: Mahe Tardy <[email protected]>
  • Loading branch information
mtardy committed Oct 8, 2024
1 parent 2b7c1ca commit 4a92d64
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/content/en/docs/reference/metrics.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pkg/metrics/policymetrics/policymetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ var policyState = metrics.MustNewCustomGauge(metrics.NewOpts(
nil, []metrics.ConstrainedLabel{stateLabel}, nil,
))

var policyKernelMemory = metrics.MustNewCustomGauge(metrics.NewOpts(
consts.MetricsNamespace, "", "tracingpolicy_kernel_memory_bytes",
"The amount of kernel memory in bytes used by policy's sensors non-shared BPF maps (memlock).",
nil, nil, []metrics.UnconstrainedLabel{{Name: "policy", ExampleValue: "example-policy"}},
))

// This metric collector converts the output of ListTracingPolicies into a few
// gauges metrics on collection. Thus, it needs a sensor manager to query.
func NewPolicyCollector() metrics.CollectorWithInit {
return metrics.NewCustomCollector(
metrics.CustomMetrics{
policyState,
policyKernelMemory,
},
collect,
collectForDocs,
Expand All @@ -63,6 +70,7 @@ func collect(ch chan<- prometheus.Metric) {
for _, policy := range list.Policies {
state := policy.State
counters[state]++
ch <- policyKernelMemory.MustMetric(float64(policy.KernelMemoryBytes), policy.Name)
}

ch <- policyState.MustMetric(
Expand All @@ -87,4 +95,5 @@ func collectForDocs(ch chan<- prometheus.Metric) {
for _, state := range stateLabel.Values {
ch <- policyState.MustMetric(0, state)
}
ch <- policyKernelMemory.MustMetric(0, "example-policy")
}
5 changes: 4 additions & 1 deletion pkg/metrics/policymetrics/policymetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (

func Test_policyStatusCollector_Collect(t *testing.T) {
expectedMetrics := func(disabled, enabled, err, load_error int) io.Reader {
return strings.NewReader(fmt.Sprintf(`# HELP tetragon_tracingpolicy_loaded The number of loaded tracing policy by state.
return strings.NewReader(fmt.Sprintf(`# HELP tetragon_tracingpolicy_kernel_memory_bytes The amount of kernel memory in bytes used by policy's sensors non-shared BPF maps (memlock).
# TYPE tetragon_tracingpolicy_kernel_memory_bytes gauge
tetragon_tracingpolicy_kernel_memory_bytes{policy="pizza"} 0
# HELP tetragon_tracingpolicy_loaded The number of loaded tracing policy by state.
# TYPE tetragon_tracingpolicy_loaded gauge
tetragon_tracingpolicy_loaded{state="disabled"} %d
tetragon_tracingpolicy_loaded{state="enabled"} %d
Expand Down

0 comments on commit 4a92d64

Please sign in to comment.