Created
April 8, 2025 12:44
-
-
Save mihai-dinculescu/e22d1fec4af2ca2bcb0e0f16388cf507 to your computer and use it in GitHub Desktop.
Wrapper Snippet
This file contains hidden or 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
// Here’s a snippet that likely provides a clearer explanation of why my organization chose to use a wrapper. | |
// The primary motivation was to unify instrumentation in alignment with both industry standards and organizational practices. | |
pub struct MyAsyncPgConnection { | |
conn: AsyncPgConnection, | |
span_provider: PgQuerySpan, | |
} | |
impl MyAsyncPgConnection { | |
/// Build a transaction, specifying additional details such as isolation level | |
pub fn build_transaction(&mut self) -> TransactionBuilder<Self> { | |
// This is what I would like to be able to do | |
TransactionBuilder::new(self) | |
} | |
} | |
impl AsyncConnection for MyAsyncPgConnection { | |
async fn establish(database_url: &str) -> ConnectionResult<Self> { | |
async { | |
// create the connection with or without TLS depending on env | |
let mut conn = ... | |
let namespace = diesel::select(current_database()) | |
.get_result(&mut conn) | |
.await | |
.map_err(ConnectionError::CouldntSetupConfiguration)?; | |
Ok(PgConnection { | |
conn, | |
span_provider: PgQuerySpan::new(namespace), | |
}) | |
} | |
.inspect_err(|e| { | |
// record error with the necessary semantic fields | |
// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/exceptions/exceptions-spans.md | |
}) | |
.instrument(PgEstablishSpan.span()) | |
.await | |
} | |
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query> | |
where | |
T: AsQuery + 'query, | |
T::Query: QueryFragment<Self::Backend> + QueryId + 'query, | |
{ | |
self.conn.load(source).instrument(self.span_provider.span()) | |
} | |
... | |
} | |
pub struct PgQuerySpan { | |
namespace: String, | |
} | |
impl PgQuerySpan { | |
pub fn new(namespace: String) -> Self { | |
Self { namespace } | |
} | |
} | |
impl SpanProvider for PgQuerySpan { | |
fn span(&self) -> Span { | |
info_span!( | |
"query", | |
// set up a handful of standard fields | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment