Skip to content

Commit

Permalink
chore(cloudevents-server): add debug log for tailing container log (#133
Browse files Browse the repository at this point in the history
)

Signed-off-by: wuhuizuo <[email protected]>

Signed-off-by: wuhuizuo <[email protected]>
  • Loading branch information
wuhuizuo authored Apr 17, 2024
1 parent e41b124 commit 2e2acf4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions cloudevents-server/pkg/events/custom/tekton/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cloudevents-server/pkg/events/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down

0 comments on commit 2e2acf4

Please sign in to comment.