fix: log analyzer failed with multiple containers in the pod (#920)

* Log analyzer failed with multiple containers in the pod #884

Signed-off-by: lwan3 <lili_wan@intuit.com>

* Merge conflicts from main

Signed-off-by: lwan3 <lili_wan@intuit.com>

---------

Signed-off-by: lwan3 <lili_wan@intuit.com>
Co-authored-by: lwan3 <lili_wan@intuit.com>
Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
lili-wan
2024-02-21 04:33:44 -08:00
committed by GitHub
parent 6ac815c10f
commit 98286a965e

View File

@@ -49,12 +49,13 @@ func (LogAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
// Iterate through each pod // Iterate through each pod
for _, pod := range list.Items { for _, pod := range list.Items {
var failures []common.Failure
podName := pod.Name podName := pod.Name
for _, c := range pod.Spec.Containers {
var failures []common.Failure
podLogOptions := v1.PodLogOptions{ podLogOptions := v1.PodLogOptions{
TailLines: &tailLines, TailLines: &tailLines,
Container: c.Name,
} }
podLogs, err := a.Client.Client.CoreV1().Pods(pod.Namespace).GetLogs(podName, &podLogOptions).DoRaw(a.Context) podLogs, err := a.Client.Client.CoreV1().Pods(pod.Namespace).GetLogs(podName, &podLogOptions).DoRaw(a.Context)
if err != nil { if err != nil {
failures = append(failures, common.Failure{ failures = append(failures, common.Failure{
@@ -66,7 +67,6 @@ func (LogAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
}, },
}, },
}) })
} else { } else {
rawlogs := string(podLogs) rawlogs := string(podLogs)
if errorPattern.MatchString(strings.ToLower(rawlogs)) { if errorPattern.MatchString(strings.ToLower(rawlogs)) {
@@ -82,13 +82,14 @@ func (LogAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
} }
} }
if len(failures) > 0 { if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", pod.Namespace, pod.Name)] = common.PreAnalysis{ preAnalysis[fmt.Sprintf("%s/%s/%s", pod.Namespace, pod.Name, c.Name)] = common.PreAnalysis{
FailureDetails: failures, FailureDetails: failures,
Pod: pod, Pod: pod,
} }
AnalyzerErrorsMetric.WithLabelValues(kind, pod.Name, pod.Namespace).Set(float64(len(failures))) AnalyzerErrorsMetric.WithLabelValues(kind, pod.Name, pod.Namespace).Set(float64(len(failures)))
} }
} }
}
for key, value := range preAnalysis { for key, value := range preAnalysis {
currentAnalysis := common.Result{ currentAnalysis := common.Result{
Kind: "Pod", Kind: "Pod",