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

How to get parent span? #1904

Closed
VivekSubr opened this issue Jan 7, 2023 · 8 comments
Closed

How to get parent span? #1904

VivekSubr opened this issue Jan 7, 2023 · 8 comments
Assignees

Comments

@VivekSubr
Copy link

Opentelemetry spec says it should be possible to get a parent span given a span's context:
https://github.com/open-telemetry/opentelemetry-specification/blob/v1.0.0/specification/trace/api.md#determining-the-parent-span-from-a-context

But looking at the api, there's no apparent way how to do that? There's GetParentSpanId api in SpanData, but you can only get SpanData for InMemoryExporter.

@lalitb
Copy link
Member

lalitb commented Jan 7, 2023

The link shared talk's about getting SpanId of parent from the Context while creating a (child) span. This is happening here.

The C++ implementation doesn't provide ReadableSpan interface to get the SpanId of parent after the Span is created. This is historically by design ( to prioritize optimization over feature), and has been discussed here. Adding ReadableSpan interface would need major design changes, and won't be feasible now.

@lalitb lalitb self-assigned this Jan 8, 2023
@VivekSubr
Copy link
Author

@lalitb : What about this api?
auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent();

If child active span is ended, with this automatically give context of the parent span?

If there's no way to get parent span from a span, and if you are writing a wrapper library for opentelemetry, that would force a user to keep track of parent spans right? In some sort of Stack/Queue structure.... it's things like this that caused me to hit issues like: #1889 and #1888.

@lalitb
Copy link
Member

lalitb commented Jan 8, 2023

If child active span is ended, with this automatically give context of the parent span?

Yes it will give the context containing parent-span once the child active span is ended. There are no getters on Span object to obtain the parent SpanId, if that is what you are looking for. I don't know how this relates to #1889 and #1888.

@VivekSubr
Copy link
Author

@lalitb - any way to get spanId from the context returned by,
auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent();

@lalitb
Copy link
Member

lalitb commented Jan 9, 2023

@lalitb - any way to get spanId from the context returned by,

Something like this should work

auto span_id = opentelemetry::trace::GetCurrentSpan()->GetContext().span_id();

@VivekSubr
Copy link
Author

@lalitb - that api doesn't seem to work... once child span is ended, current span should be the parent right? I can see GetCurrentSpan() still returned that child that's been ended.

@lalitb
Copy link
Member

lalitb commented Jan 10, 2023

Please share the code you are trying. Just ending Span::End() is not enough for context unwinding. You need to ensure that the Span object (which is of type nostd::shared_ptr) gets destroyed too.

@VivekSubr
Copy link
Author

VivekSubr commented Jan 10, 2023

Your right, this works

    auto span  = m_tracer->StartSpan("Test Span");
    auto scode = m_tracer->WithActiveSpan(span);
    auto span_id = span->GetContext().span_id();
    std::cout<<"Parent span id "<<spanId2Str(span_id)<<"\n";
    {
        auto span2  = m_tracer->StartSpan("Child Span");
        auto scode2 = m_tracer->WithActiveSpan(span2);
        auto span_id2 = span2->GetContext().span_id();

        ASSERT_EQ(span_id2, m_tracer->GetCurrentSpan()->GetContext().span_id());
         
        std::cout<<"Child span id "<<spanId2Str(span_id2)<<"\n";
        span2->End();
    }

    ASSERT_EQ(span_id, m_tracer->GetCurrentSpan()->GetContext().span_id());
    std::cout<<"span id "<<spanId2Str(m_tracer->GetCurrentSpan()->GetContext().span_id())<<"\n";     `
```

Must be something wrong with the way I'm using it... thank you for all the help!

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

No branches or pull requests

2 participants