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

Would like to pass the current context to the Event to facilitate Tracing #558

Closed
danielbprice opened this issue Jun 21, 2023 · 4 comments
Closed

Comments

@danielbprice
Copy link
Contributor

I'm trying to get zerolog hooked up to Datadog's unified tracing/logging scheme. In this scheme, you tag your log messages with the current trace & span IDs, and then in the Datadog console you can navigate between them. When it works, it's pretty awesome.

If anyone has a recipe for this, I'd love to know about it. I am not sure, but I think there is a facility missing in zerolog to make this work smoothly.

Datadog needs you to publish special fields in your log messages: dd.trace_id, dd.span_id, dd.service, dd.env and dd.version. Some of these (service, env, and version) are straightforward and static during the runtime of the application. But dd.trace_id and dd.span_id come from your current span.

Generally speaking, at least for Datadog, the span rides around in your context.Context, and functions that want to do something to the span, or make a new child span, will reference the context to get it. You could certainly extract that from your current context, and manually add those to each and every zerolog log message in your application, i.e. with .Str("dd.trace_id", trace_id).Str("dd.span_id", span_id) but that would really suck.

zerolog provides a Hook function, and a hook function seems like a pretty good place to add in the trace ID and the span ID. The hook could be defined at the program's startup and added to the top-level logger. But this won't work because there is no obvious way to get the current context.Context into the Hook, that I can find.

It appears that in go 1.21, when the Go team defined the slog API, they solved this problem by creating Ctx variants of their top-level functions, because "Some handlers may wish to include information from the context.Context that is available at the call site. One example of such information is the identifier for the current span when tracing is enabled." See https://pkg.go.dev/golang.org/x/exp/slog#hdr-Contexts.

And so essentially my request is for a way to do this in zerolog, because I don't want to port all of my logging to slog. My hope is to be able to do something like:

logger := zerolog.New(os.Stderr).Hook(tracingHook)
...
logger.InfoCtx(ctx).Msg("hello world")

Or perhaps: logger.Info().Ctx(ctx).Msg("hello world")

If there is already a way to do this in zerolog, I've had a lot of trouble finding it; in that case, this is a request for a "howto" document somewhere. I read all about Logger.WithContext and Logger.Ctx but as best I can tell they don't do what I'm asking for here. Another options would be something like Event.Ctx(ctx context.Context).

I am willing to try implementing this if the maintainers can give me a sense of what would be acceptable.

@danielbprice
Copy link
Contributor Author

Ahh, I guess I want https://github.com/rs/zerolog/pull/395/files

@SophisticaSean
Copy link

2nd this, ddtrace upstream provides a hook you can use for the old, no-longer-maintained logger logrus here.

we want to switch over to zerolog as an organization and would like a similar level of ease-of-use as the old logrus hook provided to ensure ddtrace context info is propagated properly throughout our go logs.

@danielbprice
Copy link
Contributor Author

@SophisticaSean Hi Sean, thanks for the motivation to go finish up #559. I am hoping to work on it some more this week. I am awaiting feedback from Olivier about the interface style. If you have a perspective, feel free to weigh in.

@danielbprice
Copy link
Contributor Author

Olivier merged #559, so I am closing this as Done. @SophisticaSean Do let me know if this does or doesn't seem to do the trick for you.

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