Skip to content

Commit

Permalink
metrics: Delete ToProm() method from granular metrics
Browse files Browse the repository at this point in the history
Instead, implement prometheus.Collector interface for granular metrics structs,
i.e. Describe and Collect methods that access the underlying Prometheus metric.
This allows us to register and test granular metrics directly, without
unnecessarily exposing internal implementation.

Signed-off-by: Anna Kapuscinska <anna@isovalent.com>
  • Loading branch information
lambdanis committed Apr 17, 2024
1 parent fbe7e3c commit 1a50ec4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/metrics/eventmetrics/eventmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func InitHealthMetrics(registry *prometheus.Registry) {
}

func InitEventsMetrics(registry *prometheus.Registry) {
registry.MustRegister(EventsProcessed.ToProm())
registry.MustRegister(policyStats.ToProm())
registry.MustRegister(EventsProcessed)
registry.MustRegister(policyStats)
}

func InitEventsMetricsForDocs(registry *prometheus.Registry) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/metrics/eventmetrics/eventmetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestHandleProcessedEvent(t *testing.T) {
assert.NoError(t, testutil.CollectAndCompare(EventsProcessed.ToProm(), strings.NewReader("")))
assert.NoError(t, testutil.CollectAndCompare(EventsProcessed, strings.NewReader("")))
handleProcessedEvent(nil, nil)
// empty process
handleProcessedEvent(nil, &tetragon.GetEventsResponse{Event: &tetragon.GetEventsResponse_ProcessKprobe{ProcessKprobe: &tetragon.ProcessKprobe{}}})
Expand Down Expand Up @@ -79,7 +79,7 @@ tetragon_events_total{binary="binary_c",namespace="namespace_c",pod="pod_c",type
tetragon_events_total{binary="binary_e",namespace="",pod="",type="PROCESS_EXIT",workload=""} 1
tetragon_events_total{binary="binary_e",namespace="namespace_e",pod="pod_e",type="PROCESS_EXIT",workload="workload_e"} 1
`)
assert.NoError(t, testutil.CollectAndCompare(EventsProcessed.ToProm(), expected))
assert.NoError(t, testutil.CollectAndCompare(EventsProcessed, expected))
}

func TestHandleOriginalEvent(t *testing.T) {
Expand Down
24 changes: 18 additions & 6 deletions pkg/metrics/granularmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ func MustNewGranularCounter[L FilteredLabels](opts prometheus.CounterOpts, extra
return result
}

func (m *GranularCounter[L]) ToProm() *prometheus.CounterVec {
return m.metric
func (m *GranularCounter[L]) Describe(ch chan<- *prometheus.Desc) {
m.metric.Describe(ch)
}

func (m *GranularCounter[L]) Collect(ch chan<- prometheus.Metric) {
m.metric.Collect(ch)
}

func (m *GranularCounter[L]) WithLabelValues(commonLvs *L, extraLvs ...string) prometheus.Counter {
Expand Down Expand Up @@ -86,8 +90,12 @@ func MustNewGranularGauge[L FilteredLabels](opts prometheus.GaugeOpts, extraLabe
return result
}

func (m *GranularGauge[L]) ToProm() *prometheus.GaugeVec {
return m.metric
func (m *GranularGauge[L]) Describe(ch chan<- *prometheus.Desc) {
m.metric.Describe(ch)
}

func (m *GranularGauge[L]) Collect(ch chan<- prometheus.Metric) {
m.metric.Collect(ch)
}

func (m *GranularGauge[L]) WithLabelValues(commonLvs *L, extraLvs ...string) prometheus.Gauge {
Expand Down Expand Up @@ -124,8 +132,12 @@ func MustNewGranularHistogram[L FilteredLabels](opts prometheus.HistogramOpts, e
return result
}

func (m *GranularHistogram[L]) ToProm() *prometheus.HistogramVec {
return m.metric
func (m *GranularHistogram[L]) Describe(ch chan<- *prometheus.Desc) {
m.metric.Describe(ch)
}

func (m *GranularHistogram[L]) Collect(ch chan<- prometheus.Metric) {
m.metric.Collect(ch)
}

func (m *GranularHistogram[L]) WithLabelValues(commonLvs *L, extraLvs ...string) prometheus.Observer {
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/syscallmetrics/syscallmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
)

func InitMetrics(registry *prometheus.Registry) {
registry.MustRegister(syscallStats.ToProm())
registry.MustRegister(syscallStats)

// NOTES:
// * Delete syscalls_total? It seems to duplicate policy_events_total.
Expand Down

0 comments on commit 1a50ec4

Please sign in to comment.