-
Notifications
You must be signed in to change notification settings - Fork 173
feat(metrics): add CollectorSourceDiscardedLogs alert for discarded source logs #3293
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path" | ||
| "regexp" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
| k8sYAML "k8s.io/apimachinery/pkg/util/yaml" | ||
| ) | ||
|
|
||
| var _ = Describe("CollectorSourceDiscardedLogs alert", Ordered, func() { | ||
| var discardAlert monitoringv1.Rule | ||
|
|
||
| BeforeAll(func() { | ||
| mdir, err := os.Getwd() | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| mdir = path.Dir(path.Dir(mdir)) | ||
| data, err := os.ReadFile(path.Join(mdir, "config", "prometheus", "collector_alerts.yaml")) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| rule := &monitoringv1.PrometheusRule{} | ||
| err = k8sYAML.NewYAMLOrJSONDecoder(bytes.NewReader(data), 1000).Decode(rule) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| metricRegex := regexp.MustCompile(`(vector_\w+|logcollector_\w+)`) | ||
| for _, group := range rule.Spec.Groups { | ||
| for _, r := range group.Rules { | ||
| if r.Alert == "" { | ||
| continue | ||
| } | ||
| metrics := metricRegex.FindAllString(r.Expr.String(), -1) | ||
| for _, metric := range metrics { | ||
| Expect(collectorMinimalAllowlist.allowedMetrics).To(ContainElement(metric), | ||
| "metric %q used in alert %q is not in the collector minimal allowlist", metric, r.Alert) | ||
| } | ||
| if r.Alert == "CollectorSourceDiscardedLogs" { | ||
| discardAlert = r | ||
| } | ||
| } | ||
| } | ||
| Expect(discardAlert.Alert).NotTo(BeEmpty(), "CollectorSourceDiscardedLogs alert not found in collector_alerts.yaml") | ||
| }) | ||
|
|
||
| It("should use discard and error metrics for source components", func() { | ||
| expr := discardAlert.Expr.String() | ||
| Expect(expr).To(ContainSubstring("vector_component_discarded_events_total")) | ||
| Expect(expr).To(ContainSubstring("vector_component_errors_total")) | ||
| Expect(expr).To(ContainSubstring(`component_kind="source"`)) | ||
| Expect(expr).To(ContainSubstring("reading_line_from_file")) | ||
| Expect(expr).To(ContainSubstring("reading_line_from_kubernetes_log")) | ||
| }) | ||
|
|
||
| It("should group by labels that identify the affected log stream", func() { | ||
| expr := discardAlert.Expr.String() | ||
| Expect(expr).To(ContainSubstring("namespace")) | ||
| Expect(expr).To(ContainSubstring("app_kubernetes_io_instance")) | ||
| Expect(expr).To(ContainSubstring("component_id")) | ||
| Expect(expr).To(ContainSubstring("component_type")) | ||
| }) | ||
|
|
||
| It("should have severity warning", func() { | ||
| Expect(discardAlert.Labels["severity"]).To(Equal("warning")) | ||
| }) | ||
| }) |
This file contains hidden or 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 hidden or 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,107 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| obs "github.com/openshift/cluster-logging-operator/api/observability/v1" | ||
| "github.com/openshift/cluster-logging-operator/internal/constants" | ||
| "github.com/openshift/cluster-logging-operator/internal/runtime" | ||
| "github.com/openshift/cluster-logging-operator/test/framework/functional" | ||
| testruntime "github.com/openshift/cluster-logging-operator/test/runtime/observability" | ||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| ) | ||
|
|
||
| var _ = Describe("[Functional][Metrics] Discarded source logs metrics", func() { | ||
|
|
||
| var ( | ||
| framework *functional.CollectorFunctionalFramework | ||
| metricsReaderRole *rbacv1.ClusterRole | ||
| metricsReaderBinding *rbacv1.ClusterRoleBinding | ||
| tokenReviewBinding *rbacv1.ClusterRoleBinding | ||
| ) | ||
|
|
||
| AfterEach(func() { | ||
| if tokenReviewBinding != nil { | ||
| _ = framework.Test.Delete(tokenReviewBinding) | ||
| } | ||
| if metricsReaderBinding != nil { | ||
| _ = framework.Test.Delete(metricsReaderBinding) | ||
| } | ||
| if metricsReaderRole != nil { | ||
| _ = framework.Test.Delete(metricsReaderRole) | ||
| } | ||
| framework.Cleanup() | ||
| }) | ||
|
|
||
| BeforeEach(func() { | ||
| framework = functional.NewCollectorFunctionalFramework() | ||
| testruntime.NewClusterLogForwarderBuilder(framework.Forwarder). | ||
| FromInput(obs.InputTypeAudit). | ||
| ToHttpOutput() | ||
|
|
||
| framework.VisitConfig = func(conf string) string { | ||
| return strings.ReplaceAll(conf, "max_line_bytes = 3145728", "max_line_bytes = 256") | ||
| } | ||
|
|
||
| roleName := fmt.Sprintf("%s-metrics-reader", framework.Name) | ||
| metricsReaderRole = runtime.NewClusterRole( | ||
| roleName, | ||
| runtime.NewNonResourceURLPolicyRule([]string{"/metrics"}, []string{"get"}), | ||
| ) | ||
| Expect(framework.Test.Create(metricsReaderRole)).To(Succeed()) | ||
|
|
||
| metricsReaderBinding = runtime.NewClusterRoleBinding( | ||
| roleName, | ||
| runtime.NewClusterRoleRef(roleName), | ||
| runtime.NewServiceAccountSubject("default", framework.Namespace), | ||
| ) | ||
| Expect(framework.Test.Create(metricsReaderBinding)).To(Succeed()) | ||
|
|
||
| tokenReviewBinding = runtime.NewClusterRoleBinding( | ||
| fmt.Sprintf("%s-token-reviewer", framework.Name), | ||
| runtime.NewClusterRoleRef("system:auth-delegator"), | ||
| runtime.NewServiceAccountSubject("default", framework.Namespace), | ||
| ) | ||
| Expect(framework.Test.Create(tokenReviewBinding)).To(Succeed()) | ||
| }) | ||
|
|
||
| It("should generate vector_component_discarded_events_total when source logs exceed max_line_bytes", func() { | ||
| Expect(framework.Deploy()).To(BeNil()) | ||
|
|
||
| auditLogFile := "/var/log/kube-apiserver/audit.log" | ||
|
|
||
| // Write oversized lines (~1.5KB each, exceeding 256 byte limit) followed by a short line | ||
| // in a single write so Vector processes them together in one read pass. | ||
| longLine := functional.NewKubeAuditLog(time.Now()) | ||
| shortLine := `{"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"Metadata"}` | ||
| writeCmd := fmt.Sprintf( | ||
| "mkdir -p %s && for i in $(seq 1 5); do echo '%s' >> %s; done && echo '%s' >> %s", | ||
| "/var/log/kube-apiserver", | ||
| strings.ReplaceAll(longLine, "'", "'\\''"), | ||
| auditLogFile, | ||
| shortLine, | ||
| auditLogFile, | ||
| ) | ||
| _, err := framework.RunCommand(constants.CollectorName, "bash", "-c", writeCmd) | ||
| Expect(err).To(BeNil(), "failed to write audit log entries") | ||
|
|
||
| metricsURL := fmt.Sprintf("https://%s.%s:24231/metrics", framework.Name, framework.Namespace) | ||
| curlCmd := fmt.Sprintf(`curl -ks -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" %s`, metricsURL) | ||
| grepDiscardCmd := fmt.Sprintf(`%s | grep -i discard`, curlCmd) | ||
|
|
||
| Eventually(func() string { | ||
| metrics, _ := framework.RunCommand(constants.CollectorName, "sh", "-c", grepDiscardCmd) | ||
| return metrics | ||
| }, 60*time.Second, 10*time.Second).Should( | ||
| And( | ||
| ContainSubstring("vector_component_discarded_events_total"), | ||
| ContainSubstring(`component_kind="source"`), | ||
| ), | ||
| "expected vector_component_discarded_events_total metric with component_kind=source", | ||
| ) | ||
| }) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.