Skip to content

Commit

Permalink
editoast: add metadata to DB telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
woshilapin committed May 14, 2024
1 parent 134b623 commit 3b61898
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 28 deletions.
2 changes: 2 additions & 0 deletions editoast/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustflags = ["--cfg", "tracing_unstable"]
13 changes: 13 additions & 0 deletions editoast/editoast_derive/src/modelv2/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ impl RawIdentifier {
}
}
}

fn get_ref_ident_lvalue(&self) -> syn::Expr {
match self {
Self::Field(ident) => parse_quote! { &#ident },
Self::Compound(idents) => {
parse_quote! { (#(&#idents),*) }
}
}
}
}

impl Identifier {
Expand All @@ -78,6 +87,10 @@ impl Identifier {
self.raw.get_ident_lvalue()
}

fn get_ref_lvalue(&self) -> syn::Expr {
self.raw.get_ref_ident_lvalue()
}

fn get_diesel_eqs(&self) -> Vec<syn::Expr> {
self.get_idents()
.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl ToTokens for ChangesetDecl {
})
.unzip();
tokens.extend(quote! {
#[derive(Default, Queryable, AsChangeset, Insertable, #(#additional_derives),*)]
#[derive(Debug, Default, Queryable, AsChangeset, Insertable, #(#additional_derives),*)]
#[diesel(table_name = #table)]
#vis struct #ident {
#(
Expand Down
10 changes: 9 additions & 1 deletion editoast/editoast_derive/src/modelv2/codegen/count_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ impl ToTokens for CountImpl {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::prelude::Count for #model {
#[tracing::instrument(name = #span_name, skip_all, ret, err)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(
nb_filters = settings.filters.len(),
nb_sorts = settings.sorts.len(),
paginate_counting = settings.paginate_counting,
limit,
offset,
))]
async fn count(
conn: &'async_trait mut crate::modelsv2::DbConnection,
settings: crate::modelsv2::prelude::SelectionSettings<Self>,
Expand All @@ -33,10 +39,12 @@ impl ToTokens for CountImpl {

if settings.paginate_counting {
if let Some(limit) = settings.limit {
tracing::Span::current().record("limit", limit);
query = query.limit(limit);
}

if let Some(offset) = settings.offset {
tracing::Span::current().record("offset", offset);
query = query.offset(offset);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ impl ToTokens for CreateBatchImpl {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::CreateBatch<#changeset> for #model {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_ids))]
async fn create_batch<
I: std::iter::IntoIterator<Item = #changeset> + Send + 'async_trait,
C: Default + std::iter::Extend<Self> + Send,
C: Default + std::iter::Extend<Self> + Send + std::fmt::Debug,
>(
conn: &mut crate::modelsv2::DbConnection,
values: I,
Expand All @@ -39,6 +39,8 @@ impl ToTokens for CreateBatchImpl {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use futures_util::stream::TryStreamExt;
let values = values.into_iter().collect::<Vec<_>>();
tracing::Span::current().record("query_ids", tracing::field::debug(&values));
Ok(crate::chunked_for_libpq! {
#field_count,
values,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ impl ToTokens for CreateBatchWithKeyImpl {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::CreateBatchWithKey<#changeset, #ty> for #model {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_id))]
async fn create_batch_with_key<
I: std::iter::IntoIterator<Item = #changeset> + Send + 'async_trait,
C: Default + std::iter::Extend<(#ty, Self)> + Send,
C: Default + std::iter::Extend<(#ty, Self)> + Send + std::fmt::Debug,
>(
conn: &mut crate::modelsv2::DbConnection,
values: I,
Expand All @@ -45,6 +45,8 @@ impl ToTokens for CreateBatchWithKeyImpl {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use futures_util::stream::TryStreamExt;
let values = values.into_iter().collect::<Vec<_>>();
tracing::Span::current().record("query_ids", tracing::field::debug(&values));
Ok(crate::chunked_for_libpq! {
#field_count,
values,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl ToTokens for CreateImpl {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::Create<#model> for #changeset {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err)]
async fn create(
self,
conn: &mut crate::modelsv2::DbConnection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ impl ToTokens for DeleteBatchImpl {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::DeleteBatch<#ty> for #model {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_ids))]
async fn delete_batch<I: std::iter::IntoIterator<Item = #ty> + Send + 'async_trait>(
conn: &mut crate::modelsv2::DbConnection,
ids: I,
) -> crate::error::Result<usize> {
use #table_mod::dsl;
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
let ids = ids.into_iter().collect::<Vec<_>>();
tracing::Span::current().record("query_ids", tracing::field::debug(&ids));
let counts = crate::chunked_for_libpq! {
#params_per_row,
ids,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl ToTokens for DeleteImpl {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::Delete for #model {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_id = ?self.#primary_key))]
async fn delete(
&self,
conn: &mut crate::modelsv2::DbConnection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ impl ToTokens for DeleteStaticImpl {
} = self;
let ty = identifier.get_type();
let id_ident = identifier.get_lvalue();
let id_ref_ident = identifier.get_ref_lvalue();
let eqs = identifier.get_diesel_eqs();
let span_name = format!("database-delete_static<{}>", model);

tokens.extend(quote! {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::DeleteStatic<#ty> for #model {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_id))]
async fn delete_static(
conn: &mut crate::modelsv2::DbConnection,
#id_ident: #ty,
) -> crate::error::Result<bool> {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use #table_mod::dsl;
tracing::Span::current().record("query_id", tracing::field::debug(#id_ref_ident));
diesel::delete(dsl::#table_name.#(filter(#eqs)).*)
.execute(conn)
.await
Expand Down
4 changes: 3 additions & 1 deletion editoast/editoast_derive/src/modelv2/codegen/exists_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ impl ToTokens for ExistsImpl {
} = self;
let ty = identifier.get_type();
let id_ident = identifier.get_lvalue();
let id_ref_ident = identifier.get_ref_lvalue();
let eqs = identifier.get_diesel_eqs();
let span_name = format!("database-exists<{}>", model);

tokens.extend(quote! {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::Exists<#ty> for #model {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_id))]
async fn exists(
conn: &mut crate::modelsv2::DbConnection,
#id_ident: #ty,
) -> crate::error::Result<bool> {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use #table_mod::dsl;
tracing::Span::current().record("query_id", tracing::field::debug(#id_ref_ident));
diesel::select(diesel::dsl::exists(dsl::#table_name.#(filter(#eqs)).*))
.get_result(conn)
.await
Expand Down
10 changes: 9 additions & 1 deletion editoast/editoast_derive/src/modelv2/codegen/list_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ impl ToTokens for ListImpl {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::prelude::List for #model {
#[tracing::instrument(name = #span_name, skip_all, ret, err)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(
nb_filters = settings.filters.len(),
nb_sorts = settings.sorts.len(),
paginate_counting = settings.paginate_counting,
limit,
offset,
))]
async fn list(
conn: &'async_trait mut crate::modelsv2::DbConnection,
settings: crate::modelsv2::prelude::SelectionSettings<Self>,
Expand All @@ -42,10 +48,12 @@ impl ToTokens for ListImpl {
}

if let Some(limit) = settings.limit {
tracing::Span::current().record("limit", limit);
query = query.limit(limit);
}

if let Some(offset) = settings.offset {
tracing::Span::current().record("offset", offset);
query = query.offset(offset);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ impl ToTokens for RetrieveBatchImpl {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::RetrieveBatchUnchecked<#ty> for #model {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_id))]
async fn retrieve_batch_unchecked<
I: std::iter::IntoIterator<Item = #ty> + Send + 'async_trait,
C: Default + std::iter::Extend<#model> + Send,
C: Default + std::iter::Extend<#model> + Send + std::fmt::Debug,
>(
conn: &mut crate::modelsv2::DbConnection,
ids: I,
Expand All @@ -44,6 +44,8 @@ impl ToTokens for RetrieveBatchImpl {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use futures_util::stream::TryStreamExt;
let ids = ids.into_iter().collect::<Vec<_>>();
tracing::Span::current().record("query_ids", tracing::field::debug(&ids));
Ok(crate::chunked_for_libpq! {
#params_per_row,
ids,
Expand All @@ -68,7 +70,7 @@ impl ToTokens for RetrieveBatchImpl {
#[tracing::instrument(name = #span_name_with_key, skip_all)]
async fn retrieve_batch_with_key_unchecked<
I: std::iter::IntoIterator<Item = #ty> + Send + 'async_trait,
C: Default + std::iter::Extend<(#ty, #model)> + Send,
C: Default + std::iter::Extend<(#ty, #model)> + Send + std::fmt::Debug,
>(
conn: &mut crate::modelsv2::DbConnection,
ids: I,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ impl ToTokens for RetrieveImpl {
} = self;
let ty = identifier.get_type();
let id_ident = identifier.get_lvalue();
let id_ref_ident = identifier.get_ref_lvalue();
let eqs = identifier.get_diesel_eqs();
let span_name = format!("database-retrieve<{}>", model);

tokens.extend(quote! {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::Retrieve<#ty> for #model {
#[tracing::instrument(name = #span_name, skip(conn))]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_id))]
async fn retrieve(
conn: &mut crate::modelsv2::DbConnection,
#id_ident: #ty,
) -> crate::error::Result<Option<#model>> {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use #table_mod::dsl;
tracing::Span::current().record("query_id", tracing::field::debug(#id_ref_ident));
dsl::#table_name
.#(filter(#eqs)).*
.first::<#row>(conn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl ToTokens for UpdateBatchImpl {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::UpdateBatchUnchecked<#model, #ty> for #changeset {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_ids))]
async fn update_batch_unchecked<
I: std::iter::IntoIterator<Item = #ty> + Send + 'async_trait,
C: Default + std::iter::Extend<#model> + Send,
C: Default + std::iter::Extend<#model> + Send + std::fmt::Debug,
>(
self,
conn: &mut crate::modelsv2::DbConnection,
Expand All @@ -49,6 +49,8 @@ impl ToTokens for UpdateBatchImpl {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use futures_util::stream::TryStreamExt;
let ids = ids.into_iter().collect::<Vec<_>>();
tracing::Span::current().record("query_ids", tracing::field::debug(&ids));
Ok(crate::chunked_for_libpq! {
// FIXME: that count is correct for each row, but the maximum buffer size
// should be libpq's max MINUS the size of the changeset
Expand Down
4 changes: 3 additions & 1 deletion editoast/editoast_derive/src/modelv2/codegen/update_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ impl ToTokens for UpdateImpl {
} = self;
let ty = identifier.get_type();
let id_ident = identifier.get_lvalue();
let id_ref_ident = identifier.get_ref_lvalue();
let eqs = identifier.get_diesel_eqs();
let span_name = format!("database-update<{}>", model);

tokens.extend(quote! {
#[automatically_derived]
#[async_trait::async_trait]
impl crate::modelsv2::Update<#ty, #model> for #changeset {
#[tracing::instrument(name = #span_name, skip_all)]
#[tracing::instrument(name = #span_name, skip_all, ret, err, fields(query_id))]
async fn update(
self,
conn: &mut crate::modelsv2::DbConnection,
Expand All @@ -40,6 +41,7 @@ impl ToTokens for UpdateImpl {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use #table_mod::dsl;
tracing::Span::current().record("query_id", tracing::field::debug(#id_ref_ident));
diesel::update(dsl::#table_name.#(filter(#eqs)).*)
.set(&self)
.get_result::<#row>(conn)
Expand Down
6 changes: 4 additions & 2 deletions editoast/src/modelsv2/prelude/create.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Debug;

use crate::error::EditoastError;
use crate::error::Result;
use crate::modelsv2::DbConnection;
Expand Down Expand Up @@ -48,7 +50,7 @@ where
/// ```
async fn create_batch<
I: IntoIterator<Item = Cs> + Send + 'async_trait,
C: Default + std::iter::Extend<Self> + Send,
C: Default + std::iter::Extend<Self> + Send + Debug,
>(
conn: &mut DbConnection,
values: I,
Expand All @@ -72,7 +74,7 @@ where
/// Just like [CreateBatch::create_batch] but the returned models are paired with their key
async fn create_batch_with_key<
I: IntoIterator<Item = Cs> + Send + 'async_trait,
C: Default + std::iter::Extend<(K, Self)> + Send,
C: Default + std::iter::Extend<(K, Self)> + Send + Debug,
>(
conn: &mut DbConnection,
values: I,
Expand Down
Loading

0 comments on commit 3b61898

Please sign in to comment.