go-agent icon indicating copy to clipboard operation
go-agent copied to clipboard

Attributes are not forwarded to NewRelic for logs

Open mariusgrigaitis opened this issue 10 months ago • 22 comments

Logs in context integrations and newrelic-agent does not forward attributes from logs

https://docs.newrelic.com/docs/logs/logs-context/configure-logs-context-go/#installing-a-logs-in-context-plugin

https://github.com/newrelic/go-agent/blob/master/v3/integrations/logcontext-v2/nrslog/handler.go#L130

Expected Behavior

Attributes appear on NR.

Reproduction case

https://github.com/newrelic/go-agent/blob/master/v3/integrations/logcontext-v2/nrslog/example/main.go

Additional attributes can be added to prove the case in the linked example

Additional context

What's the point of integrating structured logging solutions into NewRelic if attributes (essential part of structured) are not being forwarded?

Seems to be the case for all logcontext-v2 integrations, since attributes are not part of logcontext-v2 design?

Related: #768

mariusgrigaitis avatar Mar 27 '24 15:03 mariusgrigaitis

This functionality is quite critical. Good slog practice requires adding attributes with additional metadatada, right now all is lost, only message is displayed in NewRelic's console.

r--w avatar Apr 02 '24 17:04 r--w

@mariusgrigaitis I managed to add a simple middleware to add all the attributes to the message and this way you can at least see meaningful logs in NewRelic's console, here's the part of the code using https://github.com/samber/slog-multi/

....
logger := slog.New(
		slogmulti.
			Pipe(slogmulti.NewHandleInlineMiddleware(concatAttrsMiddleware)).
			Handler(nrslog.JSONHandler(app, os.Stdout, opts)),
	)

....
func concatAttrsMiddleware(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error {
	flat := strings.Builder{}

	record.Attrs(func(attr slog.Attr) bool {
		flat.WriteString("key: " + attr.Key + " value: " + fmt.Sprintf("%v", attr.Value))
		return true
	})

	message := record.Message
	if flat.Len() > 0 {
		message += ", " + flat.String()
	}

	return next(ctx, slog.NewRecord(record.Time, record.Level, message, record.PC))
}

r--w avatar Apr 03 '24 21:04 r--w

Hi all, thank you for your advocacy and your patience. I hear your concerns and have gotten the go ahead to prioritize this ASAP. Supporting attribute collection for all v2 logs in context packages is my top priority. I will communicate expectations and upcoming changes as they come. I will communicate about this primarily in https://github.com/newrelic/go-agent/issues/768 since that is where most people seem to be tracking this issue.

iamemilio avatar Apr 05 '24 14:04 iamemilio

Thank you! I fought with this for hours before I realized it was expected behavior. Looking forward to a fix.

clarkmcc avatar Apr 08 '24 18:04 clarkmcc

I am also developing on a library as a hobby to include attributes in the Logs in Context while also logging to stdout in JSON format.

I hope you can use this as a temporary solution.

https://github.com/miyamo2/altnrslog

miyamo2 avatar Apr 13 '24 08:04 miyamo2

Capturing attributes should already work for decorated logs captured by log forwarders, but the agent is not yet able to handle and emit log attributes. We will have that code settled and shipped soon, and then we can start plugging it into our logging tools: https://github.com/newrelic/go-agent/pull/900

iamemilio avatar Apr 17 '24 19:04 iamemilio

https://github.com/newrelic/go-agent/issues/768#issuecomment-2065146652

iamemilio avatar Apr 18 '24 19:04 iamemilio

https://github.com/newrelic/go-agent/releases/tag/v3.33.0 - Adds this feature to Zap. Upcoming releases will add this to zerolog and slog too

iamemilio avatar May 01 '24 19:05 iamemilio

#911 PR for slog attribute support.

mirackara avatar May 03 '24 19:05 mirackara

Same as @clarkmcc , spent hours troubleshooting this until I found this issue.

Using logrus with .WithField() and .WithFields().

dselans avatar May 26 '24 06:05 dselans

#911 PR for slog attribute support.

I saw it was approved a month ago. Any updates on when this is going to get released?

conamu avatar Jun 03 '24 11:06 conamu

@conamu They added support for attributes for uber/zap in 3.33.0 - it works well but it doesn't include initial fields or fields added via WithField() :(

As a tmp bandaid, I added my own log wrapper that keeps track of the fields and just re-adds them every time a log.Debug/Info/Warn/etc is called (with or without extra attributes). Could do the same for logrus/whatever other logger you're using.

Gist: https://gist.github.com/dselans/b318d89158ad83b930221650db15c6ec

dselans avatar Jun 03 '24 17:06 dselans

we are already using logrus in most of the codebase. Im more interested in a solution to work with slog, a native package, instead of having to fix another library.

conamu avatar Jun 03 '24 21:06 conamu

The Slog package is out :) https://github.com/newrelic/go-agent/tree/master/v3/integrations/logcontext-v2/nrslog

iamemilio avatar Jun 07 '24 17:06 iamemilio

Working on a fix for #915

iamemilio avatar Jun 07 '24 17:06 iamemilio

See: https://github.com/newrelic/go-agent/pull/919

iamemilio avatar Jun 13 '24 14:06 iamemilio

Keenly looking forward to the slog attribute handling. I could incorporate a workaround but I would rather not include additional libraries to intercept the slog output.

Any word on when this is planned to be included in a release now that it's merged?

BoldBrandonM avatar Jul 30 '24 16:07 BoldBrandonM

We are hoping to do a release this Thursday. These logging features will be included in that.

iamemilio avatar Jul 30 '24 17:07 iamemilio

Fantastic news, thank you!

BoldBrandonM avatar Jul 30 '24 17:07 BoldBrandonM

We are deferring the release to Monday August 5th to make space to address #939, which is a high priority issue. Thanks for your patience, we are eager to get these features in your hands!

iamemilio avatar Aug 01 '24 15:08 iamemilio

As of the release 3.34.0, the Go agent should support full attribute collection for Zap, Slog, and Logrus. Support for Zerolog is on the way in an upcoming release!

iamemilio avatar Aug 05 '24 17:08 iamemilio

As of the release 3.34.0, the Go agent should support full attribute collection for Zap, Slog, and Logrus. Support for Zerolog is on the way in an upcoming release!

Thank you! Just tested this out with Slog and it's working well for our use-case 🥂

BoldBrandonM avatar Aug 06 '24 14:08 BoldBrandonM