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

Function LogsHandler

functions/async/logs-handler-function/LogsHandler.go:23–74  ·  view source on GitHub ↗
(ctx ofctx.Context, in []byte)

Source from the content-addressed store, hash-verified

21)
22
23func LogsHandler(ctx ofctx.Context, in []byte) (ofctx.Out, error) {
24 content := string(in)
25 matchHTTPCode, _ := regexp.MatchString(fmt.Sprintf(" %s ", HTTPCodeNotFound), content)
26 matchNamespace, _ := regexp.MatchString(fmt.Sprintf("namespace_name\":\"%s", Namespace), content)
27 matchPodName := regexp.MustCompile(fmt.Sprintf(`(%s)`, PodName)).FindStringSubmatch(content)
28
29 if matchHTTPCode && matchNamespace && matchPodName != nil {
30 log.Printf("Match log - Content: %s", content)
31
32 match := regexp.MustCompile(`([A-Z]+) (/\S*) HTTP`).FindStringSubmatch(content)
33 if match == nil {
34 return ctx.ReturnOnInternalError(), errors.New("failed to match event")
35 }
36 path := match[len(match)-1]
37 method := match[len(match)-2]
38 podName := matchPodName[len(matchPodName)-1]
39
40 notify := &alert.Data{
41 Receiver: "notification_manager",
42 Status: "firing",
43 Alerts: alert.Alerts{},
44 GroupLabels: alert.KV{"alertname": AlertName, "namespace": Namespace},
45 CommonLabels: alert.KV{"alertname": AlertName, "namespace": Namespace, "severity": Severity},
46 CommonAnnotations: alert.KV{},
47 ExternalURL: "",
48 }
49 alt := alert.Alert{
50 Status: "firing",
51 Labels: alert.KV{
52 "alertname": AlertName,
53 "namespace": Namespace,
54 "severity": Severity,
55 "pod": podName,
56 "path": path,
57 "method": method,
58 },
59 Annotations: alert.KV{},
60 StartsAt: time.Now(),
61 EndsAt: time.Time{},
62 GeneratorURL: "",
63 Fingerprint: "",
64 }
65 notify.Alerts = append(notify.Alerts, alt)
66 notifyBytes, _ := json.Marshal(notify)
67
68 if _, err := ctx.Send("notify", notifyBytes); err != nil {
69 panic(err)
70 }
71 log.Printf("Send log to notification manager.")
72 }
73 return ctx.ReturnOnSuccess(), nil
74}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected