Skip to content

Commit

Permalink
eventcache: Refactor errors counter
Browse files Browse the repository at this point in the history
Use new helpers from pkg/metrics to define custom metrics and collector.

There are no functional changes in this commit.

Signed-off-by: Anna Kapuscinska <anna@isovalent.com>
  • Loading branch information
lambdanis committed Sep 2, 2024
1 parent 205eee5 commit 3ec8ea9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
32 changes: 12 additions & 20 deletions pkg/eventcache/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ var (
Name: "entry_type",
Values: maps.Values(cacheEntryTypeLabelValues),
}
errorLabel = metrics.ConstrainedLabel{
Name: "error",
Values: maps.Values(cacheErrorLabelValues),
}
)

var (
eventCacheErrorsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: consts.MetricsNamespace,
Name: "event_cache_errors_total",
Help: "The total of errors encountered while fetching process exec information from the cache.",
ConstLabels: nil,
}, []string{"error", "event_type"})
cacheErrors = metrics.MustNewCounter(metrics.NewOpts(
consts.MetricsNamespace, subsystem, "errors_total",
"The total of errors encountered while fetching process exec information from the cache.",
nil, []metrics.ConstrainedLabel{errorLabel, metrics.EventTypeLabel}, nil,
), nil)
cacheSize = metrics.MustNewCustomGauge(metrics.NewOpts(
consts.MetricsNamespace, "", "event_cache_entries",
"The number of entries in the event cache.",
Expand Down Expand Up @@ -101,29 +104,18 @@ func newCacheCollector() prometheus.Collector {
}

func RegisterMetrics(group metrics.Group) {
group.MustRegister(eventCacheErrorsTotal)
group.MustRegister(
newCacheCollector(),
cacheErrors,
cacheInserts,
cacheRetries,
failedFetches,
)
}

func InitMetrics() {
// Initialize metrics with labels
for ev := range tetragon.EventType_name {
if tetragon.EventType(ev) != tetragon.EventType_UNDEF && tetragon.EventType(ev) != tetragon.EventType_TEST {
for er := range cacheErrorLabelValues {
EventCacheError(er, tetragon.EventType(ev)).Add(0)
}
}
}
}

// Get a new handle on an eventCacheErrorsTotal metric for an error
func EventCacheError(er CacheError, eventType tetragon.EventType) prometheus.Counter {
return eventCacheErrorsTotal.WithLabelValues(er.String(), eventType.String())
func CacheErrors(er CacheError, eventType tetragon.EventType) prometheus.Counter {
return cacheErrors.WithLabelValues(er.String(), eventType.String())
}

// Get a new handle on an eventCacheRetriesTotal metric for an entryType
Expand Down
4 changes: 2 additions & 2 deletions pkg/grpc/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func GetProcessExec(event *MsgExecveEventUnix, useCache bool) *tetragon.ProcessE
}

if tetragonProcess.Pid == nil {
eventcache.EventCacheError(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
eventcache.CacheErrors(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
return nil
}

Expand Down Expand Up @@ -380,7 +380,7 @@ func GetProcessExit(event *MsgExitEventUnix) *tetragon.ProcessExit {
}

if tetragonProcess.Pid == nil {
eventcache.EventCacheError(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
eventcache.CacheErrors(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/grpc/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func GetProcessKprobe(event *MsgGenericKprobeUnix) *tetragon.ProcessKprobe {
}

if tetragonProcess.Pid == nil {
eventcache.EventCacheError(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
eventcache.CacheErrors(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
return nil
}

Expand Down Expand Up @@ -532,7 +532,7 @@ func (msg *MsgGenericTracepointUnix) HandleMessage() *tetragon.GetEventsResponse
}

if tetragonProcess.Pid == nil {
eventcache.EventCacheError(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
eventcache.CacheErrors(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
return nil
}

Expand Down Expand Up @@ -661,7 +661,7 @@ func GetProcessLoader(msg *MsgProcessLoaderUnix) *tetragon.ProcessLoader {
}

if tetragonProcess.Pid == nil {
eventcache.EventCacheError(eventcache.NilProcessPid, notify.EventType(notifyEvent)).Inc()
eventcache.CacheErrors(eventcache.NilProcessPid, notify.EventType(notifyEvent)).Inc()
return nil
}

Expand Down Expand Up @@ -772,7 +772,7 @@ func GetProcessUprobe(event *MsgGenericUprobeUnix) *tetragon.ProcessUprobe {
}

if tetragonProcess.Pid == nil {
eventcache.EventCacheError(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
eventcache.CacheErrors(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
return nil
}

Expand Down Expand Up @@ -896,7 +896,7 @@ func GetProcessLsm(event *MsgGenericLsmUnix) *tetragon.ProcessLsm {
}

if tetragonProcess.Pid == nil {
eventcache.EventCacheError(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
eventcache.CacheErrors(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
return nil
}

Expand Down
1 change: 0 additions & 1 deletion pkg/metricsconfig/healthmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func registerHealthMetrics(group metrics.Group) {
group.ExtendInit(errormetrics.InitMetrics)
// event cache metrics
eventcache.RegisterMetrics(group)
group.ExtendInit(eventcache.InitMetrics)
// event metrics
eventmetrics.RegisterHealthMetrics(group)
group.ExtendInit(eventmetrics.InitHealthMetrics)
Expand Down

0 comments on commit 3ec8ea9

Please sign in to comment.