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

logging: add AddFields #739

Merged
merged 2 commits into from
Dec 23, 2024

Conversation

kindermoumoute
Copy link
Contributor

Support the similar helpers from the v1 introduced in #91:

Use case: let an interceptor or the grpc method implemention add more fields to logging interceptor (e.g. "finished call").

Changes

  • store the fields into the fieldsCtxValue struct
  • add function AddFields
  • add test TestAddFields

Verification

Old tests and new tests pass.

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

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

Could you clarify the difference between AddFields and InjectFields?

interceptors/logging/logging.go Outdated Show resolved Hide resolved
@kindermoumoute
Copy link
Contributor Author

Could you clarify the difference between AddFields and InjectFields?

I updated the descriptions of both:

  • InjectFields returns a new context with merged fields
  • AddFields updates the fields already in the context

@johanbrandhorst
Copy link
Collaborator

Could you clarify the difference between AddFields and InjectFields?

I updated the descriptions of both:

* **InjectFields** returns a new context with merged fields

* **AddFields** updates the fields already in the context

Thanks for the explanation, but I'm a bit confused. This seems like an anti pattern to me. Contexts are generally immutable. Could you elaborate on the use case?

@kindermoumoute
Copy link
Contributor Author

kindermoumoute commented Dec 22, 2024

Thanks for the explanation, but I'm a bit confused. This seems like an anti pattern to me. Contexts are generally immutable. Could you elaborate on the use case?

The main use case is to add business-level attributes into the existing context so they appear in the final logs.

func (s *server) GetFoo(ctx context.Context, _ *emptypb.Empty) (*bar.GetFooResponse, error) {
	// Retrieve some metadata from the database
	fooMetadata := model.GetFoo(ctx)

	// AddFields makes these metadata visible from the "finished call" log
	logging.AddField(ctx, "metadata", fooMetadata)

	return &bar.GetFooResponse{...}, nil
}

Another approach would be to generate another line of log at the business level, but the idea here is also to limit the amount of line of logs to 1 per unary method. I agree it does sound anti-pattern to use context this way, but I couldn't find a better workaround yet - I think this is why AddFields was added and maintained in the first place in the v1.

interceptors/logging/logging.go Outdated Show resolved Hide resolved
Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]>
@johanbrandhorst johanbrandhorst merged commit 3ce33cf into grpc-ecosystem:main Dec 23, 2024
6 of 7 checks passed
@johanbrandhorst
Copy link
Collaborator

Thanks for your contributino!

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.

2 participants