Skip to content

Commit 1a7fcc9

Browse files
committed
feat(sdk): Add a root span to our client
1 parent 7e9187e commit 1a7fcc9

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
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)]
@@ -362,6 +381,8 @@ impl ClientBuilder {
362381
let homeserver = match homeserver_cfg {
363382
HomeserverConfig::Url(url) => url,
364383
HomeserverConfig::ServerName(server_name) => {
384+
debug!("Trying to discover the homeserver");
385+
365386
let homeserver = homeserver_from_name(&server_name);
366387
let well_known = http_client
367388
.send(
@@ -381,6 +402,7 @@ impl ClientBuilder {
381402
if let Some(issuer) = well_known.authentication.map(|auth| auth.issuer) {
382403
authentication_issuer = Url::parse(&issuer).ok();
383404
}
405+
debug!(homserver_url = well_known.homeserver.base_url, "Discovered the homeserver");
384406

385407
well_known.homeserver.base_url
386408
}
@@ -411,7 +433,15 @@ impl ClientBuilder {
411433
refresh_token_lock: Mutex::new(Ok(())),
412434
});
413435

414-
Ok(Client { inner })
436+
debug!("Done building the Client");
437+
438+
// We drop the root span here so it gets pushed to the subscribers, i.e. it gets
439+
// only uploaded to a OpenTelemetry collector if the span gets dropped.
440+
// We still want it around so other methods that get called by this
441+
// client instance are connected to it, so we clone.
442+
drop(self.root_span.clone());
443+
444+
Ok(Client { inner, root_span: self.root_span })
415445
}
416446
}
417447

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

+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,

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

+37-6
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, 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 {
@@ -1142,6 +1143,15 @@ impl Client {
11421143
}
11431144
}
11441145

1146+
self.root_span
1147+
.record("user_id", display(&response.user_id))
1148+
.record("device_id", display(&response.device_id));
1149+
1150+
#[cfg(feature = "e2e-encryption")]
1151+
if let Some(key) = self.encryption().ed25519_key().await {
1152+
self.root_span.record("ed25519_key", key);
1153+
}
1154+
11451155
self.inner.base_client.receive_login_response(response).await?;
11461156

11471157
Ok(())
@@ -1206,10 +1216,27 @@ impl Client {
12061216
/// ```
12071217
///
12081218
/// [`login`]: #method.login
1219+
#[instrument(skip_all, parent = &self.root_span)]
12091220
pub async fn restore_session(&self, session: Session) -> Result<()> {
1221+
debug!("Restoring session");
1222+
12101223
let (meta, tokens) = session.into_parts();
1224+
1225+
self.root_span
1226+
.record("user_id", display(&meta.user_id))
1227+
.record("device_id", display(&meta.device_id));
1228+
12111229
self.base_client().set_session_tokens(tokens);
1212-
Ok(self.base_client().set_session_meta(meta).await?)
1230+
self.base_client().set_session_meta(meta).await?;
1231+
1232+
#[cfg(feature = "e2e-encryption")]
1233+
if let Some(key) = self.encryption().ed25519_key().await {
1234+
self.root_span.record("ed25519_key", key);
1235+
}
1236+
1237+
debug!("Done restoring session");
1238+
1239+
Ok(())
12131240
}
12141241

12151242
/// Refresh the access token.
@@ -1391,7 +1418,7 @@ impl Client {
13911418
/// client.register(request).await;
13921419
/// # })
13931420
/// ```
1394-
#[instrument(skip_all)]
1421+
#[instrument(skip_all, parent = &self.root_span)]
13951422
pub async fn register(
13961423
&self,
13971424
request: register::v3::Request,
@@ -1454,7 +1481,7 @@ impl Client {
14541481
///
14551482
/// let response = client.sync_once(sync_settings).await.unwrap();
14561483
/// # });
1457-
#[instrument(skip(self, definition))]
1484+
#[instrument(skip(self, definition), parent = &self.root_span)]
14581485
pub async fn get_or_upload_filter(
14591486
&self,
14601487
filter_name: &str,
@@ -2147,7 +2174,7 @@ impl Client {
21472174
/// .await;
21482175
/// })
21492176
/// ```
2150-
#[instrument(skip(self, callback))]
2177+
#[instrument(skip_all, parent = &self.root_span)]
21512178
pub async fn sync_with_callback<C>(
21522179
&self,
21532180
sync_settings: crate::config::SyncSettings,
@@ -2244,11 +2271,15 @@ impl Client {
22442271
}
22452272

22462273
loop {
2274+
debug!("Syncing");
22472275
let result = self.sync_loop_helper(&mut sync_settings).await;
22482276

2277+
debug!("Running callback");
22492278
if callback(result).await? == LoopCtrl::Break {
2279+
debug!("Callback told us to stop");
22502280
break;
22512281
}
2282+
debug!("Done running callback");
22522283

22532284
Client::delay_sync(&mut last_sync_time).await
22542285
}
@@ -2298,7 +2329,7 @@ impl Client {
22982329
///
22992330
/// # anyhow::Ok(()) });
23002331
/// ```
2301-
#[instrument(skip(self))]
2332+
#[instrument(skip(self), parent = &self.root_span)]
23022333
pub async fn sync_stream(
23032334
&self,
23042335
mut sync_settings: crate::config::SyncSettings,

0 commit comments

Comments
 (0)