Skip to content

Refactor ActiveRecord::QueryLogs context API#43535

Merged
byroot merged 1 commit intorails:mainfrom
Shopify:remove-query-logs-null-object
Oct 26, 2021
Merged

Refactor ActiveRecord::QueryLogs context API#43535
byroot merged 1 commit intorails:mainfrom
Shopify:remove-query-logs-null-object

Conversation

@casperisfine
Copy link
Contributor

This remove the null object as proposed in 8af7870 (cc @rafaelfranca @jhawthorn @kaspth).

Since it pretends to be nil but acts as thruthy in boolean context it cause more confusion than it cleans code.

Also update_context is removed in favor of set_context without a block.

Also cc @keeran

This remove the null object as discussed in
rails@8af7870.
Since it pretends to be nil but acts as thruthy in boolean context
it cause more confusion than it clean code.

`update_context` is removed in favor of `set_context` without
a block.
@keeran
Copy link
Member

keeran commented Oct 26, 2021

Cool thanks for the heads up - looking a lot like one of the original attempts now :)

@byroot byroot merged commit 43226af into rails:main Oct 26, 2021
@rafaelfranca
Copy link
Member

rafaelfranca commented Oct 29, 2021

I don’t even want to see the number of exceptions that will happen only in production in some contests only because people don’t know they always need to check the presence of the object in the context.

@byroot
Copy link
Member

byroot commented Oct 29, 2021

That's a fair concern, but at least an error is loud and obvious.

On the other hand context[:foo].bar.baz if context[:foo] printing garbage in the logs is much harder to debug. I wish there was a way to handle both but I don't see how.

@rafaelfranca
Copy link
Member

rafaelfranca commented Oct 29, 2021

If we split the tagging hash per context and only run the tags for a context, when that context is not new we remove the need to check for nil and don’t show garbage.

@byroot
Copy link
Member

byroot commented Oct 29, 2021

I'm sorry, but I don't understand what you are suggesting, maybe a code snippet would help?

@matthewd
Copy link
Member

FWIW I'm +1 on avoiding the null object: swallowing all possibly-misguided method calls seems almost as bad as wrapping the whole thing in rescue StandardError to me.

But I had also wondered about a simplification along the lines of what (I think) @rafaelfranca is suggesting, e.g.:

ActiveRecord::QueryLogs.tags_for(:controller) do |controller_context|
  {
    controller: controller_context.controller_name,
    action: controller_context.action_name,
    namespaced_controller: controller_context.class.name,
  }
end

@byroot
Copy link
Member

byroot commented Oct 29, 2021

But I had also wondered about a simplification along the lines of what (I think) @rafaelfranca is suggesting, e.g.:

Ah! Yes, I think that would work. There might be some edge cases where you want to access two context values, but I haven't encountered one yet.

@rafaelfranca
Copy link
Member

@matthewd got my suggestion. If you set two context values we should execute the tagging code for both contexts.

@rafaelfranca
Copy link
Member

Are you going to implement that or should I?

@byroot
Copy link
Member

byroot commented Nov 1, 2021

I have a bunch on my plate already, so feel free to implement it. Otherwise I can probably get to it later this week.

@rafaelfranca
Copy link
Member

Not sure if it is worth. Between the dynamic tags and direct context access adding per-context tagging code will just make the API and the implementation more confusing. I made peace with those occasional exceptions when tagging queries. I wish it didn't fail the entire request though. Maybe we can notify the exception when we had the error notification API and silent it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants