diff --git a/main.go b/main.go index ba1eed5..6b35467 100644 --- a/main.go +++ b/main.go @@ -54,7 +54,7 @@ func main() { var labels []string for _, label := range prometheusContainerLabels { - labels = append(labels, strings.Replace(label, ".", "_", -1)) + labels = append(labels, strings.ReplaceAll(label, ".", "_")) } kubernetesCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{ Name: "klog_pod_oomkill", @@ -65,8 +65,8 @@ func main() { go func() { glog.Info("Starting prometheus metrics") - http.Handle("/metrics", promhttp.Handler()) - glog.Warning(http.ListenAndServe(metricsAddr, nil)) + http.Handle("/metrics", promhttp.Handler()) //permit + glog.Warning(http.ListenAndServe(metricsAddr, nil)) //permit }() kmsgWatcher := kmsg.NewKmsgWatcher(types.WatcherConfig{Plugin: "kmsg"}) @@ -89,12 +89,16 @@ func main() { } } -func getContainerIDFromLog(log string) (string, string) { +func getContainerIDFromLog(log string) (podUID, containerID string) { + podUID = "" + containerID = "" + if matches := kmesgRE.FindStringSubmatch(log); matches != nil { - return matches[1], matches[2] + podUID = matches[1] + containerID = matches[2] } - return "", "" + return } func getContainerLabels(containerID string, cli *containerd.Client) (map[string]string, error) { @@ -111,8 +115,7 @@ func prometheusCount(containerLabels map[string]string) { var counter prometheus.Counter var err error - var labels map[string]string - labels = make(map[string]string) + labels := make(map[string]string) for key, label := range prometheusContainerLabels { labels[label] = containerLabels[key] } diff --git a/main_test.go b/main_test.go index e0bdbe3..ddee882 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "strings" "testing" @@ -33,14 +34,14 @@ func parseMessage(input string) (kmsgparser.Message, error) { // PRIORITY,SEQUENCE_NUM,TIMESTAMP,-;MESSAGE parts := strings.SplitN(input, ";", 2) if len(parts) != 2 { - return kmsgparser.Message{}, fmt.Errorf("invalid kmsg; must contain a ';'") + return kmsgparser.Message{}, errors.New("invalid kmsg; must contain a ';'") } metadata, message := parts[0], parts[1] metadataParts := strings.Split(metadata, ",") if len(metadataParts) < 3 { - return kmsgparser.Message{}, fmt.Errorf("invalid kmsg: must contain at least 3 ',' separated pieces at the start") + return kmsgparser.Message{}, errors.New("invalid kmsg: must contain at least 3 ',' separated pieces at the start") } return kmsgparser.Message{ @@ -48,17 +49,18 @@ func parseMessage(input string) (kmsgparser.Message, error) { }, nil } -func getTestData() ([]string, []string, []string) { - return []string{ - "6,22743,6115623303887,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,mems_allowed=0-1,oom_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task=prometheus-conf,pid=3401999,uid=0", - "6,23800,6780904484233,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,mems_allowed=0,oom_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice,task_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice/cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,task=stress,pid=255629,uid=0", - }, - []string{ - "d11ab7b0-d6db-4a24-a7de-4a2faf1e6980", - "dfc377c9_c533_4d51_af9e_6e0e0b3db83b", - }, - []string{ - "9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64", - "2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc", - } +func getTestData() (klog, podUIDs, containerIDs []string) { + klog = []string{ + "6,22743,6115623303887,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,mems_allowed=0-1,oom_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task=prometheus-conf,pid=3401999,uid=0", + "6,23800,6780904484233,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,mems_allowed=0,oom_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice,task_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice/cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,task=stress,pid=255629,uid=0", + } + podUIDs = []string{ + "d11ab7b0-d6db-4a24-a7de-4a2faf1e6980", + "dfc377c9_c533_4d51_af9e_6e0e0b3db83b", + } + containerIDs = []string{ + "9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64", + "2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc", + } + return }