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

Add support for downcasting Instrumentation #4129

Merged
merged 6 commits into from
Jul 26, 2024

Conversation

Ten0
Copy link
Member

@Ten0 Ten0 commented Jul 25, 2024

This is useful to instrument additional events related to the connection.

My use-case is that I want to check that StartQuery s were consistent with FinishQuery s when releasing the connection to the Pool (which I already have a type wrapper of r2d2::PooledConnection with its Drop implementation for), and I also want to clean up the instrumentation object at the same time.

The DynInstrumentation type is useful because without it we actually did tend to return &mut Option<Box<dyn Instrumentation>> as &mut dyn Instrumentation from connection.instrumentation(), so downcasting would have to be done in these two steps by the user, which is counter-intuitive and doesn't seem ideal inside diesel itself either (because it induces extra dyn dispatch levels).

An alternate implementation would be to just make () implement Instrumentation and use Box always (Box of ZST doesn't allocate). This means we would always dyn dispatch even if instrumentation is disabled, but maybe the complexity/cost ratio is good, because probably the cost is very low given the events we currently instrument.

@Ten0 Ten0 requested a review from a team July 25, 2024 03:12
…l dyn dispatch level

we actually returned &mut Option<Box<dyn Instrumentation>> as &mut dyn Instrumentation, so downcasting would have to be done in these two steps, which is counter-intuitive and doesn't seem ideal inside diesel itself either.
@Ten0 Ten0 force-pushed the instrumentation_downcast_support branch from 5500029 to f5a316a Compare July 25, 2024 03:26
Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

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

Thanks for working on this. This seems to be a usefull addition. I have some conceptual questions that I would like to clarify and a number of stylistic requests.

@@ -242,10 +242,11 @@ impl<'a> InstrumentationEvent<'a> {
/// More complex usages and integrations with frameworks like
/// `tracing` and `log` are supposed to be part of their own
/// crates.
pub trait Instrumentation: Send + 'static {
pub trait Instrumentation: Downcast + Send + 'static {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if that's a breaking change or not.
On the one hand it adds a new super trait without a wild card impl for all types on the other hand Downcast is implemented for all T: Any and any is implemented by the compiler for all types that are 'static (we already have that bound) so it's likely not breaking?

Copy link
Member Author

@Ten0 Ten0 Jul 25, 2024

Choose a reason for hiding this comment

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

on the other hand Downcast is implemented for all T: Any and any is implemented by the compiler for all types that are 'static (we already have that bound)

Yes, that's why I thought that it was very unlikely to break anyone 😀

Also I've checked that this dependency is trustworthy and very lightweight.

/// The function that is invoced for each event
fn on_connection_event(&mut self, event: InstrumentationEvent<'_>);
}
downcast_rs::impl_downcast!(Instrumentation);
Copy link
Member

Choose a reason for hiding this comment

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

We should use that for BoxableConnection as well if we add this dependency.

(I can address that in a followup PR)

diesel/src/connection/instrumentation.rs Show resolved Hide resolved
diesel/src/connection/instrumentation.rs Show resolved Hide resolved
diesel/src/connection/instrumentation.rs Show resolved Hide resolved
diesel/src/connection/instrumentation.rs Show resolved Hide resolved
diesel/src/connection/instrumentation.rs Show resolved Hide resolved
diesel/src/connection/instrumentation.rs Outdated Show resolved Hide resolved
diesel/src/connection/instrumentation.rs Show resolved Hide resolved
@Ten0
Copy link
Member Author

Ten0 commented Jul 25, 2024

Thanks for the extremely quick review! 😊🙏

@weiznich weiznich enabled auto-merge July 26, 2024 19:33
@weiznich weiznich added this pull request to the merge queue Jul 26, 2024
Merged via the queue into diesel-rs:master with commit 16a03d0 Jul 26, 2024
49 checks passed
@Ten0 Ten0 deleted the instrumentation_downcast_support branch July 27, 2024 01:19
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