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

Attributes are not forwarded to NewRelic for logs #883

Open
mariusgrigaitis opened this issue Mar 27, 2024 · 9 comments
Open

Attributes are not forwarded to NewRelic for logs #883

mariusgrigaitis opened this issue Mar 27, 2024 · 9 comments
Assignees
Labels
bug p1 Highest priority work items roadmap

Comments

@mariusgrigaitis
Copy link

mariusgrigaitis commented Mar 27, 2024

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

@r--w
Copy link

r--w commented Apr 2, 2024

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
Copy link

r--w commented Apr 3, 2024

@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))
}

@iamemilio
Copy link
Contributor

iamemilio commented Apr 5, 2024

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 #768 since that is where most people seem to be tracking this issue.

@iamemilio iamemilio self-assigned this Apr 8, 2024
@iamemilio iamemilio added roadmap p1 Highest priority work items labels Apr 8, 2024
@clarkmcc
Copy link

clarkmcc commented Apr 8, 2024

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

@miyamo2
Copy link

miyamo2 commented Apr 13, 2024

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

@iamemilio
Copy link
Contributor

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: #900

@iamemilio
Copy link
Contributor

#768 (comment)

@iamemilio
Copy link
Contributor

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

@mirackara
Copy link
Contributor

#911 PR for slog attribute support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug p1 Highest priority work items roadmap
Projects
None yet
Development

No branches or pull requests

6 participants