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 20997fa commit 34f1c1c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
30 changes: 11 additions & 19 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())
return cacheErrors.WithLabelValues(er.String(), eventType.String())
}

// Get a new handle on an eventCacheRetriesTotal metric for an entryType
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 34f1c1c

Please sign in to comment.