-
-
Notifications
You must be signed in to change notification settings - Fork 300
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
An alternative to the "append_info_to_payload" strategy #135
Conversation
Thanks ❤️ I'll give this a look over the weekend. |
This is an interesting approach and the API feels right but I have some concerns about the 'magic' as you stated and how this would perform in the real-world given it's right there in the critical section. I'll give that a closer look though before making any assumptions. @pxlpnk any thoughts? |
So far I like the idea, I am a bit biased against magic in code especially as this could break every request. Is there any way this could be made more transparent? I see myself hunting a bug in the wrong place somehow. 🎱 |
@smudge It's been a while but I'm still keen on this approach.
Is there still any interest from your part on this? Would be great to land this. |
Definitely interested in figuring this out. I was thinking something like this might be a little less magical: config.lograge.custom_payload do |controller|
{
host: controller.request.host,
user_id: controller.current_user.try(:id),
fwd: controller.request.remote_ip
}
end The block must be executed with the context of the controller, but it's a little more explicit. (Wouldn't need to rely on The trick is in passing the custom payload values along with the Here's another alternative (avoiding the use of a config.lograge.extend_data do |data|
data[:host] = request.host
data[:user_id] = current_user.try(:id)
data[:fwd] = request.remote_ip
end More like the original implementation, the controller context is implicit to the block. But, at the same time, the way the data is called (using a block instead of a proc) is more clear: by calling Any thoughts on these variations? Basically, if you can land on a config API that makes sense and doesn't cause confusion, then the actual implementation is less important. It can be made more or less magical, but it doesn't end up affecting the end-user. |
I'm a fan of the first approach. While it feels a little clumsy having to continually reach into the yielded |
I am also a fan of the first approach. |
@smudge Any feels on this still? |
Would love to get this, working now on getting user info into our log lines. |
This addresses the 'append_info_to_payload' use case. Essentially, this combines the `custom_payload` config and the `append_info_to_payload` method override into a single config option. Unfortunately, because of the way that the logging instrumentation uses an `ActiveSupport::Notification` event to pass the payload through to lograge, I had to perform a bit of magic to get this working. But this might still serve a good use despite the magic.
@@ -5,7 +5,9 @@ rvm: | |||
- 2.3.3 | |||
- jruby-9.1.6.0 | |||
- jruby-head | |||
before_install: gem install bundler | |||
before_install: | |||
- gem update --system |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was needed in order to fix a can't modify frozen String
error on the travis builds when installing the rainbow
gem. It's unrelated to the rest of my PR. (See rubygems/bundler#5357)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! Have been hit with this one over and over.
Well, 2 years later, I put another commit together, and the configuration now looks like this: config.lograge.custom_payload do |controller|
{
host: controller.request.host,
user_id: controller.current_user.try(:id),
fwd: controller.request.remote_ip
}
end I added test coverage for this and also made sure existing tests are all green. Not 100% satisfied with the underlying implementation, but having an explicit "controller" arg allowed this to move from an underlying |
Thanks for picking this up again! I'm just giving it a quick go over but it looks awesome at first glance. |
Any update on this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
This is a solution relating to #94. It started more as a proof-of-concept, but I ended up reworking it a little and writing tests, so it might be useful for you, depending on whether the pain point is actually worth solving.
Essentially, this combines the
custom_payload
option and theappend_info_to_payload
method override strategy (see #94) into a single config option that looks like this:Unfortunately, because of the way that the logging instrumentation uses an
ActiveSupport::Notification
event to pass the payload through to lograge, I had to perform a bit of magic to get this working. (Notice howrequest
andcurrent_user
are magically accessible from within the proc.)Also, I chose to create a new config (
custom_payload
) rather than change the behavior of the existing config (custom_options
) because the two approaches are not really comparable.I'd be open to re-implementing this with a bit less magic. Let me know what you think.