Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit cf37032

Browse files
bors[bot]koivunej
andauthored
Merge #453
453: Fix multinode test spans r=koivunej a=koivunej This PR changes how the spans are set up from IpfsOptions and in UninitializedIpfs. While debugging the now ignored test (see #450) I found that the spans were configured wrong and thus none of the multiple nodes created by `spawn_nodes` could be differentiated. This also renames the spans to more logical from the "$root" given at `IpfsOptions::span`: `Span::current` => `$root:init` `Span::current` => `$root:init:swarm` `$root:swarm` => `$root:exec` (literally the executor libp2p spawns futures through) `$root` => `$root:bg_task` => `$root:swarm` (background task) `$root` => `$root:facade` (futures created through Ipfs::* methods) Still very far from perfect, but perhaps a step into better direction. Co-authored-by: Joonas Koivunen <joonas.koivunen@gmail.com>
2 parents a89b0c9 + 329587d commit cf37032

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* feat(http): create `Profile` abstraction [#421]
77
* feat: `sled` pinstore [#439], [#442], [#444]
88
* chore: update a lot of dependencies including libp2p, tokio, warp [#446]
9+
* fix: rename spans (part of [#453])
910

1011
[#429]: https://github.com/rs-ipfs/rust-ipfs/pull/429
1112
[#428]: https://github.com/rs-ipfs/rust-ipfs/pull/428
@@ -15,6 +16,7 @@
1516
[#442]: https://github.com/rs-ipfs/rust-ipfs/pull/442
1617
[#444]: https://github.com/rs-ipfs/rust-ipfs/pull/444
1718
[#446]: https://github.com/rs-ipfs/rust-ipfs/pull/446
19+
[#453]: https://github.com/rs-ipfs/rust-ipfs/pull/453
1820

1921
# 0.2.1
2022

src/lib.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
340340

341341
/// Initialize the ipfs node. The returned `Ipfs` value is cloneable, send and sync, and the
342342
/// future should be spawned on a executor as soon as possible.
343+
///
344+
/// The future returned from this method should not need
345+
/// (instrumenting)[`tracing_futures::Instrument::instrument`] as the [`IpfsOptions::span`]
346+
/// will be used as parent span for all of the awaited and created futures.
343347
pub async fn start(self) -> Result<(Ipfs<Types>, impl Future<Output = ()>), Error> {
344348
use futures::stream::StreamExt;
345349

@@ -350,16 +354,28 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
350354
mut options,
351355
} = self;
352356

353-
repo.init().await?;
354-
355-
let (to_task, receiver) = channel::<IpfsEvent>(1);
356-
357-
let facade_span = options
357+
let root_span = options
358358
.span
359359
.take()
360-
.unwrap_or_else(|| tracing::trace_span!("ipfs"));
360+
// not sure what would be the best practice with tracing and spans
361+
.unwrap_or_else(|| tracing::trace_span!(parent: &Span::current(), "ipfs"));
362+
363+
// the "current" span which is not entered but the awaited futures are instrumented with it
364+
let init_span = tracing::trace_span!(parent: &root_span, "init");
365+
366+
// stored in the Ipfs, instrumenting every method call
367+
let facade_span = tracing::trace_span!("facade");
361368

362-
let swarm_span = tracing::trace_span!(parent: facade_span.clone(), "swarm");
369+
// stored in the executor given to libp2p, used to spawn at least the connections,
370+
// instrumenting each of those.
371+
let exec_span = tracing::trace_span!(parent: &root_span, "exec");
372+
373+
// instruments the IpfsFuture, the background task.
374+
let swarm_span = tracing::trace_span!(parent: &root_span, "swarm");
375+
376+
repo.init().instrument(init_span.clone()).await?;
377+
378+
let (to_task, receiver) = channel::<IpfsEvent>(1);
363379

364380
let ipfs = Ipfs {
365381
span: facade_span,
@@ -368,8 +384,12 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
368384
to_task,
369385
};
370386

387+
// FIXME: mutating options above is an unfortunate side-effect of this call, which could be
388+
// reordered for less error prone code.
371389
let swarm_options = SwarmOptions::from(&options);
372-
let swarm = create_swarm(swarm_options, swarm_span, repo).await?;
390+
let swarm = create_swarm(swarm_options, exec_span, repo)
391+
.instrument(tracing::trace_span!(parent: &init_span, "swarm"))
392+
.await?;
373393

374394
let IpfsOptions {
375395
listening_addrs, ..
@@ -386,7 +406,7 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
386406
fut.start_add_listener_address(addr, None);
387407
}
388408

389-
Ok((ipfs, fut))
409+
Ok((ipfs, fut.instrument(swarm_span)))
390410
}
391411
}
392412

@@ -1663,12 +1683,12 @@ mod node {
16631683
pub async fn with_options(opts: IpfsOptions) -> Self {
16641684
let id = opts.keypair.public().into_peer_id();
16651685

1666-
let (ipfs, fut): (Ipfs<TestTypes>, _) = UninitializedIpfs::new(opts)
1667-
.start()
1668-
.in_current_span()
1669-
.await
1670-
.unwrap();
1671-
let bg_task = tokio::task::spawn(fut.in_current_span());
1686+
// for future: assume UninitializedIpfs handles instrumenting any futures with the
1687+
// given span
1688+
1689+
let (ipfs, fut): (Ipfs<TestTypes>, _) =
1690+
UninitializedIpfs::new(opts).start().await.unwrap();
1691+
let bg_task = tokio::task::spawn(fut);
16721692
let addrs = ipfs.identity().await.unwrap().1;
16731693

16741694
Node {

src/p2p/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl From<&IpfsOptions> for SwarmOptions {
5555
/// Creates a new IPFS swarm.
5656
pub async fn create_swarm<TIpfsTypes: IpfsTypes>(
5757
options: SwarmOptions,
58-
swarm_span: Span,
58+
span: Span,
5959
repo: Arc<Repo<TIpfsTypes>>,
6060
) -> io::Result<TSwarm<TIpfsTypes>> {
6161
let peer_id = options.peer_id;
@@ -68,7 +68,7 @@ pub async fn create_swarm<TIpfsTypes: IpfsTypes>(
6868

6969
// Create a Swarm
7070
let swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id)
71-
.executor(Box::new(SpannedExecutor(swarm_span)))
71+
.executor(Box::new(SpannedExecutor(span)))
7272
.build();
7373

7474
Ok(swarm)

src/repo/fs/pinstore.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use tokio::fs;
1313
use tokio::sync::Semaphore;
1414
use tokio_stream::{empty, wrappers::ReadDirStream, StreamExt};
1515
use tokio_util::either::Either;
16-
use tracing_futures::Instrument;
1716

1817
// PinStore is a trait from ipfs::repo implemented on FsDataStore defined at ipfs::repo::fs or
1918
// parent module.
@@ -318,7 +317,7 @@ impl PinStore for FsDataStore {
318317
}
319318
};
320319

321-
Box::pin(st.in_current_span())
320+
Box::pin(st)
322321
}
323322

324323
async fn query(

0 commit comments

Comments
 (0)