-
Notifications
You must be signed in to change notification settings - Fork 381
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
Refactor and extend the interface for metrics with configurable labels #1548
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/cilium/tetragon/pkg/metrics/consts" | ||
"github.com/cilium/tetragon/pkg/option" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
var ( | ||
granularLabelFilter = NewLabelFilter( | ||
consts.KnownMetricLabelFilters, | ||
option.Config.MetricsLabelFilter, | ||
) | ||
) | ||
|
||
type LabelFilter struct { | ||
known []string | ||
enabled map[string]interface{} | ||
} | ||
|
||
func NewLabelFilter(known []string, enabled map[string]interface{}) *LabelFilter { | ||
return &LabelFilter{ | ||
known: known, | ||
enabled: enabled, | ||
} | ||
} | ||
|
||
// metric | ||
|
||
type granularMetricIface interface { | ||
filter(labels ...string) ([]string, error) | ||
mustFilter(labels ...string) []string | ||
} | ||
|
||
type granularMetric struct { | ||
labels []string | ||
labelFilter *LabelFilter | ||
eval sync.Once | ||
} | ||
|
||
func newGranularMetric(f *LabelFilter, labels []string) (*granularMetric, error) { | ||
for _, label := range labels { | ||
if slices.Contains(f.known, label) { | ||
return nil, fmt.Errorf("passed labels can't contain any of the following: %v", f.known) | ||
} | ||
} | ||
return &granularMetric{ | ||
labels: append(labels, f.known...), | ||
labelFilter: f, | ||
}, nil | ||
} | ||
|
||
// filter takes in string arguments and returns a slice of those strings omitting the labels not configured in the metric labelFilter. | ||
// IMPORTANT! The filtered metric labels must be passed last and in the exact order of granularMetric.labelFilter.known. | ||
func (m *granularMetric) filter(labels ...string) ([]string, error) { | ||
offset := len(labels) - len(m.labelFilter.known) | ||
if offset < 0 { | ||
return nil, fmt.Errorf("not enough labels provided to filter") | ||
} | ||
result := labels[:offset] | ||
for i, label := range m.labelFilter.known { | ||
if _, ok := m.labelFilter.enabled[label]; ok { | ||
result = append(result, labels[offset+i]) | ||
} | ||
} | ||
return result, nil | ||
} | ||
|
||
func (m *granularMetric) mustFilter(labels ...string) []string { | ||
result, err := m.filter(labels...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return result | ||
} | ||
|
||
// counter | ||
|
||
type GranularCounter interface { | ||
granularMetricIface | ||
ToProm() *prometheus.CounterVec | ||
WithLabelValues(lvs ...string) prometheus.Counter | ||
} | ||
|
||
type granularCounter struct { | ||
*granularMetric | ||
metric *prometheus.CounterVec | ||
opts prometheus.CounterOpts | ||
} | ||
|
||
func NewGranularCounter(f *LabelFilter, opts prometheus.CounterOpts, labels []string) (GranularCounter, error) { | ||
metric, err := newGranularMetric(f, labels) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &granularCounter{ | ||
granularMetric: metric, | ||
opts: opts, | ||
}, nil | ||
} | ||
|
||
func MustNewGranularCounter(opts prometheus.CounterOpts, labels []string) GranularCounter { | ||
counter, err := NewGranularCounter(granularLabelFilter, opts, labels) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return counter | ||
} | ||
|
||
func (m *granularCounter) ToProm() *prometheus.CounterVec { | ||
m.eval.Do(func() { | ||
m.labels = m.mustFilter(m.labels...) | ||
m.metric = NewCounterVecWithPod(m.opts, m.labels) | ||
}) | ||
return m.metric | ||
} | ||
|
||
func (m *granularCounter) WithLabelValues(lvs ...string) prometheus.Counter { | ||
filtered := m.mustFilter(lvs...) | ||
return m.ToProm().WithLabelValues(filtered...) | ||
} | ||
|
||
// gauge | ||
|
||
type GranularGauge interface { | ||
granularMetricIface | ||
ToProm() *prometheus.GaugeVec | ||
WithLabelValues(lvs ...string) prometheus.Gauge | ||
} | ||
|
||
type granularGauge struct { | ||
*granularMetric | ||
metric *prometheus.GaugeVec | ||
opts prometheus.GaugeOpts | ||
} | ||
|
||
func NewGranularGauge(f *LabelFilter, opts prometheus.GaugeOpts, labels []string) (GranularGauge, error) { | ||
for _, label := range labels { | ||
if slices.Contains(f.known, label) { | ||
return nil, fmt.Errorf("passed labels can't contain any of the following: %v", f.known) | ||
} | ||
} | ||
return &granularGauge{ | ||
granularMetric: &granularMetric{ | ||
labels: append(labels, f.known...), | ||
}, | ||
opts: opts, | ||
}, nil | ||
} | ||
|
||
func MustNewGranularGauge(opts prometheus.GaugeOpts, labels []string) GranularGauge { | ||
result, err := NewGranularGauge(granularLabelFilter, opts, labels) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return result | ||
} | ||
|
||
func (m *granularGauge) ToProm() *prometheus.GaugeVec { | ||
m.eval.Do(func() { | ||
m.labels = m.mustFilter(m.labels...) | ||
m.metric = NewGaugeVecWithPod(m.opts, m.labels) | ||
}) | ||
return m.metric | ||
} | ||
|
||
func (m *granularGauge) WithLabelValues(lvs ...string) prometheus.Gauge { | ||
filtered := m.mustFilter(lvs...) | ||
return m.ToProm().WithLabelValues(filtered...) | ||
} | ||
|
||
// histogram | ||
|
||
type GranularHistogram interface { | ||
granularMetricIface | ||
ToProm() *prometheus.HistogramVec | ||
WithLabelValues(lvs ...string) prometheus.Observer | ||
} | ||
|
||
type granularHistogram struct { | ||
*granularMetric | ||
metric *prometheus.HistogramVec | ||
opts prometheus.HistogramOpts | ||
} | ||
|
||
func NewGranularHistogram(f *LabelFilter, opts prometheus.HistogramOpts, labels []string) (GranularHistogram, error) { | ||
for _, label := range labels { | ||
if slices.Contains(f.known, label) { | ||
return nil, fmt.Errorf("passed labels can't contain any of the following: %v", f.known) | ||
} | ||
} | ||
return &granularHistogram{ | ||
granularMetric: &granularMetric{ | ||
labels: append(labels, f.known...), | ||
}, | ||
opts: opts, | ||
}, nil | ||
} | ||
|
||
func MustNewGranularHistogram(opts prometheus.HistogramOpts, labels []string) GranularHistogram { | ||
result, err := NewGranularHistogram(granularLabelFilter, opts, labels) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return result | ||
} | ||
|
||
func (m *granularHistogram) ToProm() *prometheus.HistogramVec { | ||
m.eval.Do(func() { | ||
m.labels = m.mustFilter(m.labels...) | ||
m.metric = NewHistogramVecWithPod(m.opts, m.labels) | ||
}) | ||
return m.metric | ||
} | ||
|
||
func (m *granularHistogram) WithLabelValues(lvs ...string) prometheus.Observer { | ||
filtered := m.mustFilter(lvs...) | ||
return m.ToProm().WithLabelValues(filtered...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package metrics | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/cilium/tetragon/pkg/metrics/consts" | ||
) | ||
|
||
var ( | ||
sampleCounterOpts = prometheus.CounterOpts{ | ||
Namespace: consts.MetricsNamespace, | ||
Name: "test_events_total", | ||
Help: "The number of test events", | ||
} | ||
sampleSyscallCounterOpts = prometheus.CounterOpts{ | ||
Namespace: consts.MetricsNamespace, | ||
Name: "test_syscalls_total", | ||
Help: "The number of test syscalls", | ||
} | ||
) | ||
|
||
func TestLabelFilter(t *testing.T) { | ||
// define label filter and metrics | ||
sampleLabelFilter := NewLabelFilter( | ||
consts.KnownMetricLabelFilters, | ||
map[string]interface{}{ | ||
"namespace": nil, | ||
"workload": nil, | ||
"pod": nil, | ||
"binary": nil, | ||
}, | ||
) | ||
sampleCounter, err := NewGranularCounter(sampleLabelFilter, sampleCounterOpts, []string{}) | ||
assert.NoError(t, err) | ||
sampleSyscallCounter, err := NewGranularCounter(sampleLabelFilter, sampleSyscallCounterOpts, []string{"syscall"}) | ||
assert.NoError(t, err) | ||
// instantiate the underlying metrics | ||
sampleCounter.ToProm() | ||
sampleSyscallCounter.ToProm() | ||
// check that labels are filtered correctly | ||
sampleLabelValues := []string{"test-namespace", "test-deployment", "test-deployment-d9jo2", "test-binary"} | ||
expectedLabelValues := []string{"test-namespace", "test-deployment", "test-deployment-d9jo2", "test-binary"} | ||
assert.Equal(t, expectedLabelValues, sampleCounter.mustFilter(sampleLabelValues...)) | ||
assert.Equal(t, append([]string{"test-syscall"}, expectedLabelValues...), sampleSyscallCounter.mustFilter(append([]string{"test-syscall"}, sampleLabelValues...)...)) | ||
|
||
// define another label filter and metrics | ||
sampleLabelFilter = NewLabelFilter( | ||
consts.KnownMetricLabelFilters, | ||
map[string]interface{}{ | ||
"namespace": nil, | ||
"workload": nil, | ||
}, | ||
) | ||
sampleCounter, err = NewGranularCounter(sampleLabelFilter, sampleCounterOpts, []string{}) | ||
assert.NoError(t, err) | ||
sampleSyscallCounter, err = NewGranularCounter(sampleLabelFilter, sampleSyscallCounterOpts, []string{"syscall"}) | ||
assert.NoError(t, err) | ||
// instantiate the underlying metrics | ||
sampleCounter.ToProm() | ||
sampleSyscallCounter.ToProm() | ||
// check that labels are filtered correctly | ||
sampleLabelValues = []string{"test-namespace", "test-deployment", "test-deployment-d9jo2", "test-binary"} | ||
expectedLabelValues = []string{"test-namespace", "test-deployment"} | ||
assert.Equal(t, expectedLabelValues, sampleCounter.mustFilter(sampleLabelValues...)) | ||
assert.Equal(t, append([]string{"test-syscall"}, expectedLabelValues...), sampleSyscallCounter.mustFilter(append([]string{"test-syscall"}, sampleLabelValues...)...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @lambdanis I think you reintroduced
golang.org/x/exp/slices
here, I don't know if it's intended since you removed that #1560.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just rebased one of my PR and noticed that go.mod needed to include the new dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't understand how the tests pass here and not on my rebased PR... https://github.com/cilium/tetragon/actions/runs/6508448671/job/17677830078?pr=1562#step:5:610
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the tests passed on this PR a while ago, before some deps got updated?
Anyway, thanks for fixing this import.