Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add LocalSpans::to_span_records() #185

Merged
merged 5 commits into from
Jan 24, 2024

Conversation

andylokandy
Copy link
Collaborator

@andylokandy andylokandy commented Jan 23, 2024

Signed-off-by: Andy Lok [email protected]

Closes #184

@andylokandy andylokandy requested a review from zhongzc January 23, 2024 18:13
@andylokandy
Copy link
Collaborator Author

@dotansimha Does it work for your project?

@dotansimha
Copy link
Contributor

@andylokandy thank you for this PR!

How would a usage of this configuration look like? I assume it might still need threads and a global collector that's being initialized? 🤔

I just created this PR: #186 , but it might be a too big of a change?

@andylokandy
Copy link
Collaborator Author

andylokandy commented Jan 23, 2024

Here is the example:

impl LocalSpans {
/// Converts the `LocalSpans` to `SpanRecord`s.
///
/// The converted spans will appear as if they were collected within the given parent context.
/// The parent of the top local span is set to the given parent.
///
/// This function is particularly useful when you want to manually collect the span records
/// without involving the global collector. This function does not require that the global
/// collector is set up by [`set_reporter()`].
///
/// # Example
///
/// ```rust
/// use minitrace::local::LocalCollector;
/// use minitrace::local::LocalSpans;
/// use minitrace::prelude::*;
///
/// // Collect local spans manually without a parent
/// let collector = LocalCollector::start();
/// let span = LocalSpan::enter_with_local_parent("a child span");
/// drop(span);
///
/// // Collect local spans into a LocalSpans instance
/// let local_spans: LocalSpans = collector.collect();
///
/// // Convert LocalSpans to SpanRecords with a given parent context
/// let parent_context = SpanContext::random();
/// let span_records = local_spans.to_span_records(parent_context);
///
/// // Now you can manually handle the span records
/// for record in span_records {
/// println!("{:?}", record);
/// }
/// ```
///
/// [`set_reporter()`]: crate::set_reporter
pub fn to_span_records(&self, parent: SpanContext) -> Vec<SpanRecord> {

Signed-off-by: Andy Lok <[email protected]>
@coveralls
Copy link

coveralls commented Jan 23, 2024

Pull Request Test Coverage Report for Build 7637141736

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 80.9%

Totals Coverage Status
Change from base Build 6945473252: 0.2%
Covered Lines: 1690
Relevant Lines: 2089

💛 - Coveralls

@dotansimha
Copy link
Contributor

dotansimha commented Jan 23, 2024

Thanks @andylokandy !

I gave it a spin and it seems to fail at runtime: time not implemented on this platform (on WASM runtime).

All I did so far it to add minitrace with features = ["enable"] and did the following at startup:

  let collector = LocalCollector::start();
  let result = run_flow(req, env).await;
  let local_spans = collector.collect();
  let parent_context = SpanContext::random();
  let span_records = local_spans.to_span_records(parent_context); // ❗ 

I'm pretty sure this originates in to_span_records, because without calling it, it seems to work fine. I printed local_spans and it seems to correctly contain the spans 🎉

@dotansimha
Copy link
Contributor

dotansimha commented Jan 23, 2024

One more thing that I noticed, and not sure how to confirm: seems like only spans created with #[trace] are collected with this kind of collector. Spans attached with in_span are not collected? 🤔

EDIT: Seems like using enter_with_local_parent works. I can live with that 👍

@andylokandy
Copy link
Collaborator Author

andylokandy commented Jan 23, 2024

I gave it a spin and it seems to fail at runtime: time not implemented on this platform (on WASM runtime).

Could you use SystemTime::now() on wasm target? You may also try wasm32-wasi target. If none of them works, you need to enable #[feature(wasm_syscall)]

One more thing that I noticed, and not sure how to confirm: seems like only spans created with #[trace] are collected with this kind of collector. Spans attached with in_span are not collected?

In a LocalCollector set up, Span will become noop, since it's a Threading-thing.

@dotansimha
Copy link
Contributor

Could you use SystemTime::now() on wasm target? You may also try wasm32-wasi target.

I'm not able to use wasm32-wasi because of a limitation for the runtime (CloudFlare Worker), it's only using wasm32-unknown-unknown (related: cloudflare/workers-rs#435).

And SystemTime::now() is not supported on this target, see rust-lang/rust#48564 (comment)

@andylokandy
Copy link
Collaborator Author

Let's move the wasm discussion to #187

@dotansimha

This comment was marked as off-topic.

@andylokandy

This comment was marked as off-topic.

@dotansimha
Copy link
Contributor

I can confirm that with the fix in tikv/minstant#34 , this PR works great!

Signed-off-by: Andy Lok <[email protected]>
Signed-off-by: Andy Lok <[email protected]>
@andylokandy andylokandy merged commit 13cd1cc into tikv:master Jan 24, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: allow to use minitrace without global collector
4 participants