-
Notifications
You must be signed in to change notification settings - Fork 438
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's optional, as ~Span destructor calls it anyway. But the |
||
} | ||
// ... 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.