Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements feature to stream pod logs in Loki #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AxiomSamarth
Copy link

This PR is intended to stream the pod logs in Loki to address issue #1

As goroutines are employed to stream the pod logs, to keep them active, we need to ensure that the main doesn't terminate. Hence, a new flag stream-duration is newly introduced. This will keep main active and the streaming will continue for a duration as set by stream-duration. Default value of stream-duration is set to 1 hour.

@@ -53,15 +58,21 @@ func runRun() error {
return err
}

err = rr.Run(context.Background())
cancelCtx, cancelFunc := context.WithTimeout(context.Background(), runOpts.streamDuration)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created this new context with the timeout set to stream-duration value which is used across the methods & functions here.

go.mod Outdated Show resolved Hide resolved
internal/runner/runner.go Outdated Show resolved Hide resolved
labels["container_name"] = container.Name
// streamPodLogs will stream the pod logs and load the logs to loki with relevant
// labels, loglines and timestamp
func (r *runner) streamPodLogs(cancelCtx context.Context, cs kubernetes.Interface, pod corev1.Pod) error {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zawachte I want to know if I can utilize the cancelCtx here to return cancelCtx.Done() when the context deadline is exceeded when streamPodLogs is called. I am still catching up with the goroutine channels aspect. Is there any suggestions you have for me to handle this better?

reader.Split(bufio.ScanLines)
defer stream.Close()

for reader.Scan() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the Follow attribute in the podLogOptions has been set to true, this reader.Scan() will always be true and reads continuously (and hence can be used as infinite loop and reader too)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant