-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
weiznich
merged 6 commits into
diesel-rs:master
from
Ten0:instrumentation_downcast_support
Jul 26, 2024
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
034826a
Add support for downcasting instrumentation
Ten0 f5a316a
Make Instrumentation downcasting work correctly by avoiding additiona…
Ten0 38b8e02
consistently double line break
Ten0 d260299
improve comment
Ten0 31a159e
Merge remote-tracking branch 'upstream/master' into instrumentation_d…
Ten0 f8d2d9d
Improve comments
Ten0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use std::fmt::Debug; | ||
use std::fmt::Display; | ||
use downcast_rs::Downcast; | ||
use std::fmt::{Debug, Display}; | ||
use std::num::NonZeroU32; | ||
use std::ops::DerefMut; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
static GLOBAL_INSTRUMENTATION: std::sync::RwLock<fn() -> Option<Box<dyn Instrumentation>>> = | ||
std::sync::RwLock::new(|| None); | ||
|
@@ -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 { | ||
/// The function that is invoced for each event | ||
fn on_connection_event(&mut self, event: InstrumentationEvent<'_>); | ||
} | ||
downcast_rs::impl_downcast!(Instrumentation); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use that for (I can address that in a followup PR) |
||
/// Get an instance of the default [`Instrumentation`] | ||
/// | ||
|
@@ -266,9 +267,11 @@ pub fn get_default_instrumentation() -> Option<Box<dyn Instrumentation>> { | |
/// | ||
/// // a simple logger that prints all events to stdout | ||
/// fn simple_logger() -> Option<Box<dyn Instrumentation>> { | ||
/// // we need the explicit argument type there due | ||
/// // to bugs in rustc | ||
/// Some(Box::new(|event: InstrumentationEvent<'_>| println!("{event:?}"))) | ||
/// // we need the explicit argument type there due | ||
/// // to bugs in rustc | ||
/// Some(Box::new(|event: InstrumentationEvent<'_>| { | ||
/// println!("{event:?}") | ||
/// })) | ||
/// } | ||
/// | ||
/// set_default_instrumentation(simple_logger); | ||
|
@@ -313,3 +316,86 @@ where | |
} | ||
} | ||
} | ||
|
||
#[diesel_derives::__diesel_public_if( | ||
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" | ||
)] | ||
/// An optional dyn instrumentation. | ||
/// For ease of use, this type implements [`Deref`] and [`DerefMut`] to `&dyn Instrumentation`, | ||
/// falling back to a no-op implementation if no instrumentation is set. | ||
pub(crate) struct DynInstrumentation { | ||
/// zst | ||
weiznich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
no_instrumentation: NoInstrumentation, | ||
inner: Option<Box<dyn Instrumentation>>, | ||
} | ||
impl Deref for DynInstrumentation { | ||
type Target = dyn Instrumentation; | ||
Ten0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fn deref(&self) -> &Self::Target { | ||
self.inner.as_deref().unwrap_or(&self.no_instrumentation) | ||
} | ||
} | ||
impl DerefMut for DynInstrumentation { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
Ten0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.inner | ||
.as_deref_mut() | ||
.unwrap_or(&mut self.no_instrumentation) | ||
} | ||
} | ||
impl DynInstrumentation { | ||
#[diesel_derives::__diesel_public_if( | ||
Ten0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" | ||
)] | ||
pub(crate) fn default_instrumentation() -> Self { | ||
Self { | ||
inner: get_default_instrumentation(), | ||
no_instrumentation: NoInstrumentation, | ||
} | ||
} | ||
|
||
#[diesel_derives::__diesel_public_if( | ||
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" | ||
)] | ||
pub(crate) fn none() -> Self { | ||
Self { | ||
inner: None, | ||
no_instrumentation: NoInstrumentation, | ||
} | ||
} | ||
|
||
#[diesel_derives::__diesel_public_if( | ||
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" | ||
)] | ||
pub(crate) fn on_connection_event(&mut self, event: InstrumentationEvent<'_>) { | ||
if let Some(inner) = self.inner.as_deref_mut() { | ||
inner.on_connection_event(event) | ||
} | ||
} | ||
} | ||
Ten0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
impl<I: Instrumentation> From<I> for DynInstrumentation { | ||
fn from(instrumentation: I) -> Self { | ||
Ten0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Self { | ||
inner: Some(unpack_instrumentation(Box::new(instrumentation))), | ||
no_instrumentation: NoInstrumentation, | ||
} | ||
} | ||
} | ||
|
||
struct NoInstrumentation; | ||
impl Instrumentation for NoInstrumentation { | ||
fn on_connection_event(&mut self, _: InstrumentationEvent<'_>) {} | ||
Ten0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Unwrap unnecessary boxing levels | ||
fn unpack_instrumentation( | ||
mut instrumentation: Box<dyn Instrumentation>, | ||
) -> Box<dyn Instrumentation> { | ||
loop { | ||
match instrumentation.downcast::<Box<dyn Instrumentation>>() { | ||
Ok(extra_boxed_instrumentation) => instrumentation = *extra_boxed_instrumentation, | ||
Err(not_extra_boxed_instrumentation) => { | ||
break not_extra_boxed_instrumentation; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 allT: 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?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.
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.