-
Notifications
You must be signed in to change notification settings - Fork 172
[release-6.6] test(metrics): add functional tests verifying component_sent_bytes_total carries component_id labels #3382
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package functional | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| "k8s.io/apimachinery/pkg/util/wait" | ||
|
|
||
| "github.com/openshift/cluster-logging-operator/internal/constants" | ||
| "github.com/openshift/cluster-logging-operator/internal/runtime" | ||
| ) | ||
|
|
||
| // SetupMetricsRBAC creates the cluster-scoped RBAC resources needed to scrape | ||
| // the collector's /metrics endpoint from within a test pod. Names are prefixed | ||
| // with the framework's namespace so parallel test packages cannot collide. | ||
| // The returned function deletes all created resources and should be registered | ||
| // with DeferCleanup or called in AfterEach. | ||
| func (f *CollectorFunctionalFramework) SetupMetricsRBAC() (metricsReaderRole *rbacv1.ClusterRole, metricsReaderBinding *rbacv1.ClusterRoleBinding, tokenReviewBinding *rbacv1.ClusterRoleBinding, err error) { | ||
| roleName := fmt.Sprintf("%s-%s-metrics-reader", f.Test.NS.Name, f.Name) | ||
|
|
||
| metricsReaderRole = runtime.NewClusterRole( | ||
| roleName, | ||
| runtime.NewNonResourceURLPolicyRule([]string{"/metrics"}, []string{"get"}), | ||
| ) | ||
| if err = f.Test.Create(metricsReaderRole); err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| metricsReaderBinding = runtime.NewClusterRoleBinding( | ||
| roleName, | ||
| runtime.NewClusterRoleRef(roleName), | ||
| runtime.NewServiceAccountSubject("default", f.Namespace), | ||
| ) | ||
| if err = f.Test.Create(metricsReaderBinding); err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| tokenReviewName := fmt.Sprintf("%s-%s-token-reviewer", f.Test.NS.Name, f.Name) | ||
| tokenReviewBinding = runtime.NewClusterRoleBinding( | ||
| tokenReviewName, | ||
| runtime.NewClusterRoleRef("system:auth-delegator"), | ||
| runtime.NewServiceAccountSubject("default", f.Namespace), | ||
| ) | ||
| if err = f.Test.Create(tokenReviewBinding); err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
|
Comment on lines
+24
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Rbac setup leaks on failure SetupMetricsRBAC returns early on create errors without deleting any previously created cluster-scoped RBAC objects, so a mid-way failure leaves ClusterRoles/ClusterRoleBindings behind. Since caller cleanup is only registered after SetupMetricsRBAC returns successfully, partial failures are not cleaned up. Agent Prompt
|
||
|
|
||
| return metricsReaderRole, metricsReaderBinding, tokenReviewBinding, nil | ||
| } | ||
|
|
||
| // CollectMetricLines polls the collector's Prometheus endpoint until a line | ||
| // matching both metricName and waitFor is found, then returns all lines | ||
| // matching metricName. | ||
| func (f *CollectorFunctionalFramework) CollectMetricLines(metricName, waitFor string, timeout time.Duration) ([]string, error) { | ||
| var matched []string | ||
| var lastErr error | ||
| err := wait.PollUntilContextTimeout(context.TODO(), 3*time.Second, timeout, true, func(ctx context.Context) (bool, error) { | ||
| raw, err := f.RunCommand(constants.CollectorName, "bash", "-c", | ||
| fmt.Sprintf("curl -ks --max-time 10 -H \"Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)\" https://%s.%s:24231/metrics", f.Name, f.Namespace)) | ||
| if err != nil { | ||
| lastErr = err | ||
| return false, nil | ||
| } | ||
| matched = nil | ||
| for _, line := range strings.Split(raw, "\n") { | ||
| if strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
| if strings.Contains(line, metricName) { | ||
| matched = append(matched, line) | ||
| } | ||
| } | ||
|
Comment on lines
+68
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. Overbroad metric matching CollectMetricLines selects lines using strings.Contains(line, metricName), which can accidentally include non-target series whose names merely contain the substring (e.g., similarly named metrics), making the new tests brittle and potentially failing for unrelated metric lines. The repo’s canonical metric name is vector_component_sent_bytes_total, but the new tests pass the shorter component_sent_bytes_total substring, relying on this imprecise matching. Agent Prompt
|
||
| for _, line := range matched { | ||
| if strings.Contains(line, waitFor) { | ||
| return true, nil | ||
| } | ||
| } | ||
| return false, nil | ||
| }) | ||
| if err != nil && lastErr != nil { | ||
| return matched, fmt.Errorf("%w (last scrape error: %v)", err, lastErr) | ||
| } | ||
| return matched, err | ||
| } | ||
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.
3. Misleading cleanup comment
🐞 Bug⚙ MaintainabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools