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

Update readthedocs api guide: nested spans and context propagator #792

Merged
merged 5 commits into from
May 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion docs/public/GettingStarted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,57 @@ related span is ended.

The concept of an active span is important, as any span that is created
without explicitly specifying a parent is parented to the currently
active span.
active span. A span without a parent is called root span.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
active span. A span without a parent is called root span.
active span. A span with a null parent is called root span.


Create nested Spans
^^^^^^^^^^^^^^^^^^^

.. code:: cpp

auto outer_span = tracer->StartSpan("Outer operation");
auto outer_scope = tracer->WithActiveSpan(outer_span);
{
auto inner_span = tracer->StartSpan("Inner operation");
auto inner_scope = tracer->WithActiveSpan(inner_span);
// ... perform inner operation
inner_span->End();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this call to ->End() optional? The destruction of inner_span would call End().

Copy link
Member Author

@lalitb lalitb May 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's optional, as ~Span destructor calls it anyway. But the Span object returned by Tracer::StartSpan is shared_ptr and in case instrumentation code is referring to this object at some other places, the ~Span destructor will be called when all the references are cleaned up. It's always good ( and no harm ) to call it explicitly to export the Span when it is complete. And later when the destructor is called, it won't perform this again.

}
// ... perform outer operation
outer_span->End();


Spans can be nested, and have a parent-child relationship with other spans.
When a given span is active, the newly created span inherits the active span's
trace ID, and other context attributes.

Context Propagation
^^^^^^^^^^^^^^^^^^

.. code:: cpp

// set global propagator
opentelemetry::context::propagation::GlobalTextMapPropagator::SetGlobalPropagator(
nostd::shared_ptr<opentelemetry::context::propagation::TextMapPropagator>(
new opentelemetry::trace::propagation::HttpTraceContext()));

// get global propagator
HttpTextMapCarrier<opentelemetry::ext::http::client::Headers> carrier;
auto propagator =
opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator();

//inject context to headers
auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent();
propagator->Inject(carrier, current_ctx);

//Extract headers to context
auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent();
auto new_context = propagator->Extract(carrier, current_ctx);
auto remote_span = opentelemetry::trace::propagation::GetSpan(new_context);



``Context`` contains the meta-data of the currently active Span including Span Id,
Trace Id, and flags. Context Propagation is an important mechanism in distributed
tracing to transfer this Context across service boundary often through HTTP headers.
OpenTelemetry provides a text-based approach to propagate context to remote services
using the W3C Trace Context HTTP headers.