From 2e2acf489d6afb90d14a6d26a2075fab0073ecc2 Mon Sep 17 00:00:00 2001 From: wuhuizuo Date: Wed, 17 Apr 2024 10:39:06 +0800 Subject: [PATCH] chore(cloudevents-server): add debug log for tailing container log (#133) Signed-off-by: wuhuizuo Signed-off-by: wuhuizuo --- .../pkg/events/custom/tekton/handler.go | 16 ++++++++++++++++ cloudevents-server/pkg/events/handler/handler.go | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cloudevents-server/pkg/events/custom/tekton/handler.go b/cloudevents-server/pkg/events/custom/tekton/handler.go index 65ded1e..b499200 100644 --- a/cloudevents-server/pkg/events/custom/tekton/handler.go +++ b/cloudevents-server/pkg/events/custom/tekton/handler.go @@ -205,22 +205,38 @@ func getStepStatuses(status *v1beta1.TaskRunStatus, logGetter func(podName, cont } func getStepLog(baseURL, ns, podName, containerName string, tailLines int) (string, error) { + errLogEvent := log.Error(). + Str("namespace", ns). + Str("pod", podName). + Str("container", containerName). + Int("tail", tailLines) + apiURL, err := url.JoinPath(baseURL, fmt.Sprintf("api/v1/namespaces/%s/pods/%s/log", ns, podName)) if err != nil { + errLogEvent.Err(err).Send() return "", err } resp, err := http.Get(fmt.Sprintf("%s?container=%s&tailLines=%d", apiURL, containerName, tailLines)) if err != nil { + errLogEvent.Err(err).Send() return "", err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { + errLogEvent.Err(err).Send() return "", err } + errLogEvent.Discard() + log.Debug(). + Str("namespace", ns). + Str("pod", podName). + Str("container", containerName). + Int("tail", tailLines). + Msg("get log succeed.") return string(body), nil } diff --git a/cloudevents-server/pkg/events/handler/handler.go b/cloudevents-server/pkg/events/handler/handler.go index b98fe37..c30a79b 100644 --- a/cloudevents-server/pkg/events/handler/handler.go +++ b/cloudevents-server/pkg/events/handler/handler.go @@ -27,7 +27,7 @@ func (h *CompositeEventHandler) SupportEventTypes() []string { func (h *CompositeEventHandler) Handle(event cloudevents.Event) cloudevents.Result { handlers, ok := h.handleMap[event.Type()] if !ok { - log.Error().Str("type", event.Type()).Msg("no handlers registered for the event") + log.Warn().Str("ce-type", event.Type()).Msg("no handlers registered for the event") return cloudevents.NewHTTPResult(http.StatusNotFound, "no handlers registered for event type: %s, ignoring it", event.Type()) }