-
Notifications
You must be signed in to change notification settings - Fork 389
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
metrics: Refactor metrics label filter logic #2321
Merged
+315
−291
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2a7b45f
metrics: Move GetRegistry to metrics.go
lambdanis 1188fb6
option: Delete vars.go file
lambdanis 3cd580d
metrics: Refactor metrics label filter logic
lambdanis 2bc1dc6
option: Enable all metrics labels by default
lambdanis ce1f208
helm: Improve docs for tetragon.prometheus.metricsLabelFilter
lambdanis fbe7e3c
metrics: Unexport ProcessLabels fields
lambdanis 1a50ec4
metrics: Delete ToProm() method from granular metrics
lambdanis 317fb2d
metrics: Document NewProcessLabels function
lambdanis 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,53 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package metrics | ||
|
||
import ( | ||
"github.com/cilium/tetragon/pkg/option" | ||
) | ||
|
||
type FilteredLabels interface { | ||
Keys() []string | ||
Values() []string | ||
} | ||
|
||
type ProcessLabels struct { | ||
namespace string | ||
workload string | ||
pod string | ||
binary string | ||
} | ||
|
||
// NewProcessLabels creates a new ProcessLabels struct with the global labels | ||
// filter applied. To have a metric respect the labels filter, we have to: | ||
// 1. Define a granular metric with ProcessLabels type parameter (see granularmetric.go). | ||
// 2. When calling WithLabelValues, pass a ProcessLabels struct created with NewProcessLabels. | ||
func NewProcessLabels(namespace, workload, pod, binary string) *ProcessLabels { | ||
if !option.Config.MetricsLabelFilter["namespace"] { | ||
namespace = "" | ||
} | ||
if !option.Config.MetricsLabelFilter["workload"] { | ||
workload = "" | ||
} | ||
if !option.Config.MetricsLabelFilter["pod"] { | ||
pod = "" | ||
} | ||
if !option.Config.MetricsLabelFilter["binary"] { | ||
binary = "" | ||
} | ||
return &ProcessLabels{ | ||
namespace: namespace, | ||
workload: workload, | ||
pod: pod, | ||
binary: binary, | ||
} | ||
} | ||
|
||
func (l ProcessLabels) Keys() []string { | ||
return []string{"namespace", "workload", "pod", "binary"} | ||
} | ||
|
||
func (l ProcessLabels) Values() []string { | ||
return []string{l.namespace, l.workload, l.pod, l.binary} | ||
} |
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,53 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package metrics | ||
|
||
import ( | ||
"maps" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/cilium/tetragon/pkg/metrics/consts" | ||
"github.com/cilium/tetragon/pkg/option" | ||
) | ||
|
||
func TestProcessLabels(t *testing.T) { | ||
t.Cleanup(func() { | ||
// reset global config back to the default | ||
option.Config.MetricsLabelFilter = maps.Clone(consts.DefaultLabelsFilter) | ||
}) | ||
|
||
namespace := "test-namespace" | ||
workload := "test-deployment" | ||
pod := "test-deployment-d9jo2" | ||
binary := "test-binary" | ||
|
||
// by default all labels should be enabled | ||
processLabels := NewProcessLabels(namespace, workload, pod, binary) | ||
assert.Equal(t, processLabels.Values(), []string{namespace, workload, pod, binary}) | ||
|
||
// disable workload and pod | ||
option.Config.MetricsLabelFilter["workload"] = false | ||
option.Config.MetricsLabelFilter["pod"] = false | ||
processLabels = NewProcessLabels(namespace, workload, pod, binary) | ||
assert.Equal(t, processLabels.Values(), []string{namespace, "", "", binary}) | ||
|
||
// delete binary (this shouldn't really happen, we set the values to false instead) | ||
delete(option.Config.MetricsLabelFilter, "binary") | ||
processLabels = NewProcessLabels(namespace, workload, pod, binary) | ||
assert.Equal(t, processLabels.Values(), []string{namespace, "", "", ""}) | ||
|
||
// disable all | ||
for l := range consts.DefaultLabelsFilter { | ||
option.Config.MetricsLabelFilter[l] = false | ||
} | ||
processLabels = NewProcessLabels(namespace, workload, pod, binary) | ||
assert.Equal(t, processLabels.Values(), []string{"", "", "", ""}) | ||
|
||
// clear label filter (this shouldn't really happen, we set the values to false instead) | ||
option.Config.MetricsLabelFilter = map[string]bool{} | ||
processLabels = NewProcessLabels(namespace, workload, pod, binary) | ||
assert.Equal(t, processLabels.Values(), []string{"", "", "", ""}) | ||
} |
Oops, something went wrong.
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.
AFAIU, the disabled labels are set to "". The thing I'm not sure I understand is where does the filtering happens? Is it .WithLabelValues()?
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.
When calling
WithLabelValues()
you need to pass aProcessLabels
struct. If it's created withNewProcessLabels
then disabled labels are set to "", what from Prometheus perspective is equivalent to not having these labels at all. So filtering relies on always creatingProcessLabels
withNewProcessLabels
. I'll add a comment about it, as it's not obvious indeed.