Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose kernel ringbuffer errors in metrics #2839

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Expose kernel ringbuffer errors in metrics
There are two metrics counting events lost in the ringbuffer:
* tetragon_missed_events_total, collected in BPF based on perf_event_output
  error
* tetragon_ringbuf_perf_event_lost_total, collected in observer based on
  Record.LostSamples from cilium/ebpf

When testing, I saw the former being higher than the latter. This might mean
there are failed writes to the ringbuffer that are not lost events seen by
Record.LostSamples. To investigate such issues, I'm adding error label to
tetragon_missed_events_total, representing kernel error returned by
perf_event_output.

According to @olsajiri EBUSY and ENOSPC would be the only we could really hit,
the rest is most likely due config error. So let's start with counting these
two, and aggregating other errors as "unknown". We can always split out more
errors in the future if needed.

Signed-off-by: Anna Kapuscinska <anna@isovalent.com>
lambdanis committed Aug 24, 2024

Unverified

The committer email address is not verified.
commit c270528916f99bd13d63db31fbd7da741ff683f5
19 changes: 15 additions & 4 deletions bpf/lib/process.h
Original file line number Diff line number Diff line change
@@ -564,8 +564,13 @@ FUNC_INLINE struct execve_info *execve_joined_info_map_get(__u64 tid)
_Static_assert(sizeof(struct execve_map_value) % 8 == 0,
"struct execve_map_value should have size multiple of 8 bytes");

#define SENT_FAILED_UNKNOWN 0 // unknown error
#define SENT_FAILED_EBUSY 1 // EBUSY
#define SENT_FAILED_ENOSPC 2 // ENOSPC
#define SENT_FAILED_MAX 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, extra tab screwing the diff in terminal


struct kernel_stats {
__u64 sent_failed[256];
__u64 sent_failed[256][SENT_FAILED_MAX];
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a note.. I think it's ok because it's still small map, but it is per cpu and this change multiplies the memory usage by 3


struct {
@@ -576,7 +581,7 @@ struct {
} tg_stats_map SEC(".maps");

FUNC_INLINE void
perf_event_output_metric(void *ctx, u8 metric, void *map, u64 flags, void *data, u64 size)
perf_event_output_metric(void *ctx, u8 msg_op, void *map, u64 flags, void *data, u64 size)
{
struct kernel_stats *valp;
__u32 zero = 0;
@@ -585,8 +590,14 @@ perf_event_output_metric(void *ctx, u8 metric, void *map, u64 flags, void *data,
err = perf_event_output(ctx, map, flags, data, size);
if (err < 0) {
valp = map_lookup_elem(&tg_stats_map, &zero);
if (valp)
__sync_fetch_and_add(&valp->sent_failed[metric], 1);
if (valp) {
if (err == -16) // EBUSY
__sync_fetch_and_add(&valp->sent_failed[msg_op][SENT_FAILED_EBUSY], 1);
else if (err == -28) // ENOSPC
__sync_fetch_and_add(&valp->sent_failed[msg_op][SENT_FAILED_ENOSPC], 1);
else
__sync_fetch_and_add(&valp->sent_failed[msg_op][SENT_FAILED_UNKNOWN], 1);
}
}
}

1 change: 1 addition & 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: 8 additions & 1 deletion pkg/api/processapi/processapi.go
Original file line number Diff line number Diff line change
@@ -49,6 +49,13 @@ const (
STRING_POSTFIX_MAX_LENGTH = 128
)

const (
SentFailedUnknown = iota
SentFailedEbusy
SentFailedEnospc
SentFailedMax
)

type MsgExec struct {
Size uint32
PID uint32
@@ -235,7 +242,7 @@ type MsgThrottleEvent struct {
}

type KernelStats struct {
SentFailed [256]uint64 `align:"sent_failed"`
SentFailed [256][SentFailedMax]uint64 `align:"sent_failed"`
}

type CgroupRateKey struct {
18 changes: 12 additions & 6 deletions pkg/metrics/eventmetrics/collector.go
Original file line number Diff line number Diff line change
@@ -39,20 +39,26 @@ func collect(ch chan<- prometheus.Metric) {

sum := processapi.KernelStats{}
for _, val := range allCpuValue {
for i, data := range val.SentFailed {
sum.SentFailed[i] += data
for opcode, errors := range val.SentFailed {
for er, count := range errors {
sum.SentFailed[opcode][er] += count
}
}
}

for i, data := range sum.SentFailed {
if data > 0 {
ch <- MissedEvents.MustMetric(float64(data), strconv.Itoa(i))
for opcode, errors := range sum.SentFailed {
for er, count := range errors {
if count > 0 {
ch <- MissedEvents.MustMetric(float64(count), strconv.Itoa(opcode), perfEventErrors[er])
}
}
}
}

func collectForDocs(ch chan<- prometheus.Metric) {
for _, opcode := range metrics.OpCodeLabel.Values {
ch <- MissedEvents.MustMetric(0, opcode)
for _, er := range perfEventErrorLabel.Values {
ch <- MissedEvents.MustMetric(0, opcode, er)
}
}
}
16 changes: 15 additions & 1 deletion pkg/metrics/eventmetrics/eventmetrics.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
package eventmetrics

import (
"golang.org/x/exp/maps"

v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/api/v1/tetragon/codegen/helpers"
@@ -20,6 +22,18 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

var (
perfEventErrors = map[int]string{
processapi.SentFailedUnknown: "unknown",
processapi.SentFailedEbusy: "EBUSY",
processapi.SentFailedEnospc: "ENOSPC",
}
perfEventErrorLabel = metrics.ConstrainedLabel{
Name: "error",
Values: maps.Values(perfEventErrors),
}
)

var (
EventsProcessed = metrics.MustNewGranularCounter[metrics.ProcessLabels](prometheus.CounterOpts{
Namespace: consts.MetricsNamespace,
@@ -30,7 +44,7 @@ var (
MissedEvents = metrics.MustNewCustomCounter(metrics.NewOpts(
consts.MetricsNamespace, "", "missed_events_total",
"The total number of Tetragon events per type that are failed to sent from the kernel.",
nil, []metrics.ConstrainedLabel{metrics.OpCodeLabel}, nil,
nil, []metrics.ConstrainedLabel{metrics.OpCodeLabel, perfEventErrorLabel}, nil,
))
FlagCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: consts.MetricsNamespace,