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

Why do I need to keep track of the lifetime of the scope/span objects myself? #2249

Open
Falmarri opened this issue Jul 27, 2023 · 5 comments
Labels
good first issue Good for newcomers help wanted Good for taking. Extra help will be provided by maintainers Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@Falmarri
Copy link

I'm trying to instrument some code that's used in a library that has very specific code flow. Keeping track of the spans and scopes myself is very tricky because I can't pass the spans/scopes. I have to store them in some locally managed storage.

In other tracing libraries (not c++), I've always just been able to do the equivalent of tracer->StartSpan(), and then some time later tracer->CurrentSpan()->End() or whatever. Is there advice on managing scopes if their lifetime is complicated? The only ways I see it working are global state, or polluting the types of all my functions to pass the scope around.

@github-actions github-actions bot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Jul 27, 2023
@lalitb
Copy link
Member

lalitb commented Jul 31, 2023

Is there advice on managing scopes if their lifetime is complicated?

Scope objects are designed to manage the parent-child spans created within the nested functions. It won't work well for anything complicated involving async/threads.

The only ways I see it working are global state, or polluting the types of all my functions to pass the scope around.

Yes sort of. Though you can also use RunTimeStorage to avoid changing the function prototypes in some scenarios.

So to create span in func1() and end in func2(), without passing scope/span objects (not tested to compile):

  void func1() {
    auto span1 = tracer->StartSpan("span1");
    tracer_api::context::RuntimeContext::GetCurrent().SetValue("myspan", span1);
    func2();
 }

 void func2() {
    .... some work
  auto span1 =  context::RuntimeContext::GetCurrent().GetValue("myspan");
  span1->End();
  tracer_api::context::RuntimeContext::GetCurrent().SetValue("myspan", NoopSpan{}); // to remove all references of span1
}

@lalitb
Copy link
Member

lalitb commented Jul 31, 2023

Also, I think it is worth evaluating if the support can be added to explicitly make the Span current without using a Scope object. Something like:

tracer->UseSpan(tracer->StartSpan("span1"));

And then somewhere else:

tracer->GetCurrentSpan()->End();

Contributions are in-general welcome to add a proposal implementation to discuss further.

@esigo esigo added triage/accepted Indicates an issue or PR is ready to be actively worked on. help wanted Good for taking. Extra help will be provided by maintainers good first issue Good for newcomers and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jul 31, 2023
@github-actions
Copy link

This issue is available for anyone to work on. Make sure to reference this issue in your pull request.
✨ Thank you for your contribution! ✨

@Falmarri
Copy link
Author

Storing in runtime context might work for me. I can activate it when needed, I just can't easily maintain a reference myself.

I am using this though
#1298 (comment) which I understand to be kind of a hack. It does seem to work though

@github-actions
Copy link

This issue was marked as stale due to lack of activity.

@github-actions github-actions bot added the Stale label Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Good for taking. Extra help will be provided by maintainers Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

3 participants