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

Messages don't make it to Graylog when called at end of program #4

Open
bencromwell opened this issue May 23, 2024 · 3 comments
Open

Comments

@bencromwell
Copy link

bencromwell commented May 23, 2024

Hello,

In handler.go there is a goroutine for writing the message.

func (h *GraylogHandler) Handle(ctx context.Context, record slog.Record) error {
        // snip

	go func() {
		_ = h.option.Writer.WriteMessage(msg)
	}()

        // snip
}

I'm finding that when a program attempts to write to Graylog immediately before it exits (such as a final error message), that message consistently never makes it to Graylog. The goroutines are all cancelled as soon as it exits, so it never finishes.

Changing this to just:

func (h *GraylogHandler) Handle(ctx context.Context, record slog.Record) error {
        // snip

        = h.option.Writer.WriteMessage(msg)

        // snip
}

Does ensure the logs are written. However, I assume it was made into a routine for a reason.

Is there a better way to resolve this?

@samber
Copy link
Owner

samber commented May 23, 2024

I moved this call into a goroutine, to prevent blocking the main thread.

After reviewing the library, I confirm a potential blocking behavior when you write to the socket.

I would suggest an update of the Graylog lib, to add buffering, followed by a defer graylog.Close() in your program, to flush before exiting.

WDYT?

@bencromwell
Copy link
Author

Would this library need to change to depend on an interface in order to support such a change?

@samber
Copy link
Owner

samber commented May 28, 2024

If github.com/Graylog2/go-gelf could buffer logs and flush periodically to the connection, this slog-graylog lib would be non-blocking without goroutine.

But go-gelf would need a method to flush before program exit as well.

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

No branches or pull requests

2 participants