MCPcopy Create free account
hub / github.com/OpenFunction/samples / LogsHandler

Function LogsHandler

functions/knative/logs-handler-function/LogsHandler.go:25–95  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

23)
24
25func LogsHandler(w http.ResponseWriter, r *http.Request) {
26 reqBody, err := ioutil.ReadAll(r.Body)
27 if err != nil {
28 w.WriteHeader(500)
29 fmt.Fprintln(w, "Get request body failed")
30 return
31 }
32 content := string(reqBody)
33 matchHTTPCode, _ := regexp.MatchString(fmt.Sprintf(" %s ", HTTPCodeNotFound), content)
34 matchNamespace, _ := regexp.MatchString(fmt.Sprintf("namespace_name\":\"%s", Namespace), content)
35 matchPodName := regexp.MustCompile(fmt.Sprintf(`(%s)`, PodName)).FindStringSubmatch(content)
36
37 if matchHTTPCode && matchNamespace && matchPodName != nil {
38 log.Printf("Match log - Content: %s", content)
39
40 match := regexp.MustCompile(`([A-Z]+) (/\S*) HTTP`).FindStringSubmatch(content)
41 if match == nil {
42 w.WriteHeader(500)
43 fmt.Fprintln(w, "Cannot retrieve information")
44 return
45 }
46 path := match[len(match)-1]
47 method := match[len(match)-2]
48 podName := matchPodName[len(matchPodName)-1]
49
50 notify := &alert.Data{
51 Receiver: "notification_manager",
52 Status: "firing",
53 Alerts: alert.Alerts{},
54 GroupLabels: alert.KV{"alertname": AlertName, "namespace": Namespace},
55 CommonLabels: alert.KV{"alertname": AlertName, "namespace": Namespace, "severity": Severity},
56 CommonAnnotations: alert.KV{},
57 ExternalURL: "",
58 }
59 alt := alert.Alert{
60 Status: "firing",
61 Labels: alert.KV{
62 "alertname": AlertName,
63 "namespace": Namespace,
64 "severity": Severity,
65 "pod": podName,
66 "path": path,
67 "method": method,
68 },
69 Annotations: alert.KV{},
70 StartsAt: time.Now(),
71 EndsAt: time.Time{},
72 GeneratorURL: "",
73 Fingerprint: "",
74 }
75 notify.Alerts = append(notify.Alerts, alt)
76 notifyBytes, _ := json.Marshal(notify)
77
78 req, err := http.NewRequest("POST", NotificationManagerAddress, bytes.NewReader(notifyBytes))
79 if err != nil {
80 w.WriteHeader(500)
81 fmt.Fprintln(w, "Failed to create request")
82 return

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected