Skip to content

Commit

Permalink
List/Query cache support for /firestore/response_time tracing attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Oct 7, 2023
1 parent 992df9b commit b25dc95
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/db/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,25 @@ impl FirestoreDb {
"/firestore/collection_name" = collection_id,
"/firestore/ids_count" = full_doc_ids.len(),
"/firestore/cache_result" = field::Empty,
"/firestore/response_time" = field::Empty
);

let begin_query_utc: DateTime<Utc> = Utc::now();

let cached_stream: BoxStream<FirestoreResult<(String, Option<FirestoreDocument>)>> =
cache.get_docs_by_paths(full_doc_ids).await?;

let cached_vec: Vec<(String, Option<FirestoreDocument>)> =
cached_stream.try_collect::<Vec<_>>().await?;

let end_query_utc: DateTime<Utc> = Utc::now();
let query_duration = end_query_utc.signed_duration_since(begin_query_utc);

span.record(
"/firestore/response_time",
query_duration.num_milliseconds(),
);

if cached_vec.len() == full_doc_ids.len()
|| matches!(
self.session_params.cache_mode,
Expand Down
11 changes: 11 additions & 0 deletions src/db/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,11 @@ impl FirestoreDb {
"Firestore List Cached",
"/firestore/collection_name" = params.collection_id,
"/firestore/cache_result" = field::Empty,
"/firestore/response_time" = field::Empty
);

let begin_query_utc: DateTime<Utc> = Utc::now();

let collection_path = if let Some(parent) = params.parent.as_ref() {
format!("{}/{}", parent, params.collection_id.as_str())
} else {
Expand All @@ -509,6 +512,14 @@ impl FirestoreDb {

let cached_result = cache.list_all_docs(&collection_path).await?;

let end_query_utc: DateTime<Utc> = Utc::now();
let query_duration = end_query_utc.signed_duration_since(begin_query_utc);

span.record(
"/firestore/response_time",
query_duration.num_milliseconds(),
);

match cached_result {
FirestoreCachedValue::UseCached(stream) => {
span.record("/firestore/cache_result", "hit");
Expand Down
12 changes: 12 additions & 0 deletions src/db/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,27 @@ impl FirestoreDb {
"Firestore Query Cached",
"/firestore/collection_name" = collection_id.as_str(),
"/firestore/cache_result" = field::Empty,
"/firestore/response_time" = field::Empty
);

let begin_query_utc: DateTime<Utc> = Utc::now();

let collection_path = if let Some(parent) = params.parent.as_ref() {
format!("{}/{}", parent, collection_id)
} else {
format!("{}/{}", self.get_documents_path(), collection_id.as_str())
};

let result = cache.query_docs(&collection_path, params).await?;

let end_query_utc: DateTime<Utc> = Utc::now();
let query_duration = end_query_utc.signed_duration_since(begin_query_utc);

span.record(
"/firestore/response_time",
query_duration.num_milliseconds(),
);

match result {
FirestoreCachedValue::UseCached(stream) => {
span.record("/firestore/cache_result", "hit");
Expand Down

0 comments on commit b25dc95

Please sign in to comment.