Skip to content

Commit 9649100

Browse files
authored
feat(sdk): Add a root span to our client
1 parent 292a983 commit 9649100

File tree

6 files changed

+137
-39
lines changed

6 files changed

+137
-39
lines changed

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ use ruma::{
2929
use thiserror::Error;
3030
#[cfg(not(target_arch = "wasm32"))]
3131
use tokio::sync::OnceCell;
32+
use tracing::{
33+
debug,
34+
field::{self, debug},
35+
instrument, span, Level, Span,
36+
};
3237
use url::Url;
3338

3439
use super::{Client, ClientInner};
@@ -88,10 +93,19 @@ pub struct ClientBuilder {
8893
appservice_mode: bool,
8994
server_versions: Option<Box<[MatrixVersion]>>,
9095
handle_refresh_tokens: bool,
96+
root_span: Span,
9197
}
9298

9399
impl ClientBuilder {
94100
pub(crate) fn new() -> Self {
101+
let root_span = span!(
102+
Level::INFO,
103+
"matrix-sdk",
104+
user_id = field::Empty,
105+
device_id = field::Empty,
106+
ed25519_key = field::Empty
107+
);
108+
95109
Self {
96110
homeserver_cfg: None,
97111
http_cfg: None,
@@ -101,6 +115,7 @@ impl ClientBuilder {
101115
appservice_mode: false,
102116
server_versions: None,
103117
handle_refresh_tokens: false,
118+
root_span,
104119
}
105120
}
106121

@@ -326,8 +341,12 @@ impl ClientBuilder {
326341
/// server discovery request is made which can fail; if you didn't set
327342
/// [`server_versions(false)`][Self::server_versions], that amounts to
328343
/// another request that can fail
344+
#[instrument(skip_all, parent = &self.root_span, target = "matrix_sdk::client", fields(homeserver))]
329345
pub async fn build(self) -> Result<Client, ClientBuildError> {
346+
debug!("Starting to build the Client");
347+
330348
let homeserver_cfg = self.homeserver_cfg.ok_or(ClientBuildError::MissingHomeserver)?;
349+
Span::current().record("homeserver", debug(&homeserver_cfg));
331350

332351
let inner_http_client = match self.http_cfg.unwrap_or_default() {
333352
#[allow(unused_mut)]
@@ -364,6 +383,8 @@ impl ClientBuilder {
364383
let homeserver = match homeserver_cfg {
365384
HomeserverConfig::Url(url) => url,
366385
HomeserverConfig::ServerName(server_name) => {
386+
debug!("Trying to discover the homeserver");
387+
367388
let homeserver = homeserver_from_name(&server_name);
368389
let well_known = http_client
369390
.send(
@@ -387,6 +408,7 @@ impl ClientBuilder {
387408
if let Some(proxy) = well_known.sliding_sync_proxy.map(|p| p.url) {
388409
sliding_sync_proxy = Url::parse(&proxy).ok();
389410
}
411+
debug!(homserver_url = well_known.homeserver.base_url, "Discovered the homeserver");
390412

391413
well_known.homeserver.base_url
392414
}
@@ -421,7 +443,15 @@ impl ClientBuilder {
421443
refresh_token_lock: Mutex::new(Ok(())),
422444
});
423445

424-
Ok(Client { inner })
446+
debug!("Done building the Client");
447+
448+
// We drop the root span here so it gets pushed to the subscribers, i.e. it gets
449+
// only uploaded to a OpenTelemetry collector if the span gets dropped.
450+
// We still want it around so other methods that get called by this
451+
// client instance are connected to it, so we clone.
452+
drop(self.root_span.clone());
453+
454+
Ok(Client { inner, root_span: self.root_span })
425455
}
426456
}
427457

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ impl LoginBuilder {
142142
/// Instead of calling this function and `.await`ing its return value, you
143143
/// can also `.await` the `LoginBuilder` directly.
144144
#[instrument(
145+
parent = &self.client.root_span,
145146
target = "matrix_sdk::client",
146147
name = "login",
147148
skip_all,
@@ -278,7 +279,13 @@ where
278279
///
279280
/// Instead of calling this function and `.await`ing its return value, you
280281
/// can also `.await` the `SsoLoginBuilder` directly.
281-
#[instrument(target = "matrix_sdk::client", name = "login", skip_all, fields(method = "sso"))]
282+
#[instrument(
283+
parent = &self.client.root_span,
284+
target = "matrix_sdk::client",
285+
name = "login",
286+
skip_all,
287+
fields(method = "sso"),
288+
)]
282289
pub async fn send(self) -> Result<login::v3::Response> {
283290
use std::{
284291
convert::Infallible,

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

+40-7
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, info, instrument};
72+
use tracing::{debug, field::display, info, instrument, trace, Instrument, Span};
7373
use url::Url;
7474

7575
#[cfg(feature = "e2e-encryption")]
@@ -128,6 +128,7 @@ pub enum LoopCtrl {
128128
#[derive(Clone)]
129129
pub struct Client {
130130
pub(crate) inner: Arc<ClientInner>,
131+
pub(crate) root_span: Span,
131132
}
132133

133134
pub(crate) struct ClientInner {
@@ -1152,6 +1153,15 @@ impl Client {
11521153
}
11531154
}
11541155

1156+
self.root_span
1157+
.record("user_id", display(&response.user_id))
1158+
.record("device_id", display(&response.device_id));
1159+
1160+
#[cfg(feature = "e2e-encryption")]
1161+
if let Some(key) = self.encryption().ed25519_key().await {
1162+
self.root_span.record("ed25519_key", key);
1163+
}
1164+
11551165
self.inner.base_client.receive_login_response(response).await?;
11561166

11571167
Ok(())
@@ -1216,10 +1226,27 @@ impl Client {
12161226
/// ```
12171227
///
12181228
/// [`login`]: #method.login
1229+
#[instrument(skip_all, parent = &self.root_span)]
12191230
pub async fn restore_session(&self, session: Session) -> Result<()> {
1231+
debug!("Restoring session");
1232+
12201233
let (meta, tokens) = session.into_parts();
1234+
1235+
self.root_span
1236+
.record("user_id", display(&meta.user_id))
1237+
.record("device_id", display(&meta.device_id));
1238+
12211239
self.base_client().set_session_tokens(tokens);
1222-
Ok(self.base_client().set_session_meta(meta).await?)
1240+
self.base_client().set_session_meta(meta).await?;
1241+
1242+
#[cfg(feature = "e2e-encryption")]
1243+
if let Some(key) = self.encryption().ed25519_key().await {
1244+
self.root_span.record("ed25519_key", key);
1245+
}
1246+
1247+
debug!("Done restoring session");
1248+
1249+
Ok(())
12231250
}
12241251

12251252
/// Refresh the access token.
@@ -1401,7 +1428,7 @@ impl Client {
14011428
/// client.register(request).await;
14021429
/// # })
14031430
/// ```
1404-
#[instrument(skip_all)]
1431+
#[instrument(skip_all, parent = &self.root_span)]
14051432
pub async fn register(
14061433
&self,
14071434
request: register::v3::Request,
@@ -1464,7 +1491,7 @@ impl Client {
14641491
///
14651492
/// let response = client.sync_once(sync_settings).await.unwrap();
14661493
/// # });
1467-
#[instrument(skip(self, definition))]
1494+
#[instrument(skip(self, definition), parent = &self.root_span)]
14681495
pub async fn get_or_upload_filter(
14691496
&self,
14701497
filter_name: &str,
@@ -2157,7 +2184,7 @@ impl Client {
21572184
/// .await;
21582185
/// })
21592186
/// ```
2160-
#[instrument(skip(self, callback))]
2187+
#[instrument(skip_all, parent = &self.root_span)]
21612188
pub async fn sync_with_callback<C>(
21622189
&self,
21632190
sync_settings: crate::config::SyncSettings,
@@ -2254,11 +2281,15 @@ impl Client {
22542281
}
22552282

22562283
loop {
2284+
trace!("Syncing");
22572285
let result = self.sync_loop_helper(&mut sync_settings).await;
22582286

2287+
trace!("Running callback");
22592288
if callback(result).await? == LoopCtrl::Break {
2289+
trace!("Callback told us to stop");
22602290
break;
22612291
}
2292+
trace!("Done running callback");
22622293

22632294
Client::delay_sync(&mut last_sync_time).await
22642295
}
@@ -2308,7 +2339,7 @@ impl Client {
23082339
///
23092340
/// # anyhow::Ok(()) });
23102341
/// ```
2311-
#[instrument(skip(self))]
2342+
#[instrument(skip(self), parent = &self.root_span)]
23122343
pub async fn sync_stream(
23132344
&self,
23142345
mut sync_settings: crate::config::SyncSettings,
@@ -2319,9 +2350,11 @@ impl Client {
23192350
sync_settings.token = self.sync_token().await;
23202351
}
23212352

2353+
let parent_span = Span::current();
2354+
23222355
async_stream::stream! {
23232356
loop {
2324-
yield self.sync_loop_helper(&mut sync_settings).await;
2357+
yield self.sync_loop_helper(&mut sync_settings).instrument(parent_span.clone()).await;
23252358

23262359
Client::delay_sync(&mut last_sync_time).await
23272360
}

0 commit comments

Comments
 (0)