Skip to content

Commit 2ab8e6a

Browse files
committed
chore(sdk): Utilize the root span in more places
1 parent 1a7fcc9 commit 2ab8e6a

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

crates/matrix-sdk/src/client/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use serde::de::DeserializeOwned;
6969
use tokio::sync::OnceCell;
7070
#[cfg(feature = "e2e-encryption")]
7171
use tracing::error;
72-
use tracing::{debug, field::display, info, instrument, Span};
72+
use tracing::{debug, field::display, info, instrument, Instrument, Span};
7373
use url::Url;
7474

7575
#[cfg(feature = "e2e-encryption")]
@@ -2340,9 +2340,11 @@ impl Client {
23402340
sync_settings.token = self.sync_token().await;
23412341
}
23422342

2343+
let parent_span = Span::current();
2344+
23432345
async_stream::stream! {
23442346
loop {
2345-
yield self.sync_loop_helper(&mut sync_settings).await;
2347+
yield self.sync_loop_helper(&mut sync_settings).instrument(parent_span.clone()).await;
23462348

23472349
Client::delay_sync(&mut last_sync_time).await
23482350
}

crates/matrix-sdk/src/room/joined.rs

+39-20
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ use ruma::{
3131
EventId, OwnedTransactionId, TransactionId, UserId,
3232
};
3333
use serde_json::Value;
34-
use tracing::debug;
35-
#[cfg(feature = "e2e-encryption")]
36-
use tracing::instrument;
34+
use tracing::{debug, instrument};
3735

3836
use super::Left;
3937
use crate::{
@@ -84,6 +82,7 @@ impl Joined {
8482
}
8583

8684
/// Leave this room.
85+
#[instrument(skip_all, parent = &self.client.root_span)]
8786
pub async fn leave(&self) -> Result<Left> {
8887
self.inner.leave().await
8988
}
@@ -95,6 +94,7 @@ impl Joined {
9594
/// * `user_id` - The user to ban with `UserId`.
9695
///
9796
/// * `reason` - The reason for banning this user.
97+
#[instrument(skip_all, parent = &self.client.root_span)]
9898
pub async fn ban_user(&self, user_id: &UserId, reason: Option<&str>) -> Result<()> {
9999
let request = assign!(
100100
ban_user::v3::Request::new(self.inner.room_id().to_owned(), user_id.to_owned()),
@@ -112,6 +112,7 @@ impl Joined {
112112
/// room.
113113
///
114114
/// * `reason` - Optional reason why the room member is being kicked out.
115+
#[instrument(skip_all, parent = &self.client.root_span)]
115116
pub async fn kick_user(&self, user_id: &UserId, reason: Option<&str>) -> Result<()> {
116117
let request = assign!(
117118
kick_user::v3::Request::new(self.inner.room_id().to_owned(), user_id.to_owned()),
@@ -126,6 +127,7 @@ impl Joined {
126127
/// # Arguments
127128
///
128129
/// * `user_id` - The `UserId` of the user to invite to the room.
130+
#[instrument(skip_all, parent = &self.client.root_span)]
129131
pub async fn invite_user_by_id(&self, user_id: &UserId) -> Result<()> {
130132
let recipient = InvitationRecipient::UserId { user_id: user_id.to_owned() };
131133

@@ -140,6 +142,7 @@ impl Joined {
140142
/// # Arguments
141143
///
142144
/// * `invite_id` - A third party id of a user to invite to the room.
145+
#[instrument(skip_all, parent = &self.client.root_span)]
143146
pub async fn invite_user_by_3pid(&self, invite_id: Invite3pid) -> Result<()> {
144147
let recipient = InvitationRecipient::ThirdPartyId(invite_id);
145148
let request = invite_user::v3::Request::new(self.inner.room_id().to_owned(), recipient);
@@ -207,35 +210,44 @@ impl Joined {
207210
};
208211

209212
if send {
210-
let typing = if typing {
211-
self.client
212-
.inner
213-
.typing_notice_times
214-
.insert(self.inner.room_id().to_owned(), Instant::now());
215-
Typing::Yes(TYPING_NOTICE_TIMEOUT)
216-
} else {
217-
self.client.inner.typing_notice_times.remove(self.inner.room_id());
218-
Typing::No
219-
};
220-
221-
let request = TypingRequest::new(
222-
self.inner.own_user_id().to_owned(),
223-
self.inner.room_id().to_owned(),
224-
typing,
225-
);
226-
self.client.send(request, None).await?;
213+
self.send_typing_notice(typing).await?;
227214
}
228215

229216
Ok(())
230217
}
231218

219+
#[instrument(name = "typing_notice", skip(self), parent = &self.client.root_span)]
220+
async fn send_typing_notice(&self, typing: bool) -> Result<()> {
221+
let typing = if typing {
222+
self.client
223+
.inner
224+
.typing_notice_times
225+
.insert(self.inner.room_id().to_owned(), Instant::now());
226+
Typing::Yes(TYPING_NOTICE_TIMEOUT)
227+
} else {
228+
self.client.inner.typing_notice_times.remove(self.inner.room_id());
229+
Typing::No
230+
};
231+
232+
let request = TypingRequest::new(
233+
self.inner.own_user_id().to_owned(),
234+
self.inner.room_id().to_owned(),
235+
typing,
236+
);
237+
238+
self.client.send(request, None).await?;
239+
240+
Ok(())
241+
}
242+
232243
/// Send a request to notify this room that the user has read specific
233244
/// event.
234245
///
235246
/// # Arguments
236247
///
237248
/// * `event_id` - The `EventId` specifies the event to set the read receipt
238249
/// on.
250+
#[instrument(skip_all, parent = &self.client.root_span)]
239251
pub async fn read_receipt(&self, event_id: &EventId) -> Result<()> {
240252
let request = create_receipt::v3::Request::new(
241253
self.inner.room_id().to_owned(),
@@ -256,6 +268,7 @@ impl Joined {
256268
///
257269
/// * read_receipt - An `EventId` to specify the event to set the read
258270
/// receipt on.
271+
#[instrument(skip_all, parent = &self.client.root_span)]
259272
pub async fn read_marker(
260273
&self,
261274
fully_read: &EventId,
@@ -301,6 +314,7 @@ impl Joined {
301314
/// }
302315
/// # anyhow::Ok(()) });
303316
/// ```
317+
#[instrument(skip_all, parent = &self.client.root_span)]
304318
pub async fn enable_encryption(&self) -> Result<()> {
305319
use ruma::{
306320
events::room::encryption::RoomEncryptionEventContent, EventEncryptionAlgorithm,
@@ -401,6 +415,7 @@ impl Joined {
401415
/// Warning: This waits until a sync happens and does not return if no sync
402416
/// is happening! It can also return early when the room is not a joined
403417
/// room anymore!
418+
#[instrument(skip_all, parent = &self.client.root_span)]
404419
pub async fn sync_up(&self) {
405420
while !self.is_synced() && self.room_type() == RoomType::Joined {
406421
self.client.inner.sync_beat.listen().wait_timeout(Duration::from_secs(1));
@@ -671,6 +686,7 @@ impl Joined {
671686
/// }
672687
/// # anyhow::Ok(()) });
673688
/// ```
689+
#[instrument(skip_all, parent = &self.client.root_span)]
674690
pub async fn send_attachment(
675691
&self,
676692
body: &str,
@@ -824,6 +840,7 @@ impl Joined {
824840
/// joined_room.send_state_event(content).await?;
825841
/// # anyhow::Ok(()) };
826842
/// ```
843+
#[instrument(skip_all, parent = &self.client.root_span)]
827844
pub async fn send_state_event(
828845
&self,
829846
content: impl StateEventContent<StateKey = EmptyStateKey>,
@@ -923,6 +940,7 @@ impl Joined {
923940
/// }
924941
/// # anyhow::Ok(()) });
925942
/// ```
943+
#[instrument(skip_all, parent = &self.client.root_span)]
926944
pub async fn send_state_event_raw(
927945
&self,
928946
content: Value,
@@ -973,6 +991,7 @@ impl Joined {
973991
/// }
974992
/// # anyhow::Ok(()) });
975993
/// ```
994+
#[instrument(skip_all, parent = &self.client.root_span)]
976995
pub async fn redact(
977996
&self,
978997
event_id: &EventId,

crates/matrix-sdk/src/room/timeline/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl Timeline {
372372
///
373373
/// [`MessageLikeUnsigned`]: ruma::events::MessageLikeUnsigned
374374
/// [`SyncMessageLikeEvent`]: ruma::events::SyncMessageLikeEvent
375-
#[instrument(skip(self, content), fields(room_id = ?self.room().room_id()))]
375+
#[instrument(skip(self, content), parent = &self.inner.room().client.root_span, fields(room_id = ?self.room().room_id()))]
376376
pub async fn send(&self, content: AnyMessageLikeEventContent, txn_id: Option<&TransactionId>) {
377377
let txn_id = txn_id.map_or_else(TransactionId::new, ToOwned::to_owned);
378378
self.inner.handle_local_event(txn_id.clone(), content.clone()).await;

0 commit comments

Comments
 (0)