Propagation
You are viewing the English version of this page because it has not yet been fully translated. Interested in helping out? See Contributing.
通过上下文传播,信号可以彼此关联,而不受其生成位置的限制。尽管上下文传播并不限于链路跟踪, 但它允许链路在跨越进程和网络边界的任意分布式服务之间构建系统的因果关系信息。
在绝大多数用例中,原生支持 OpenTelemetry 的库或插桩库会自动为你在服务之间传播跟踪上下文。 只有在极少数情况下,你才需要手动传播上下文。
要了解更多,请参阅上下文传播。
Automatic context propagation
Distributed traces extend beyond a single service, meaning some context must be propagated across services to create the parent-child relationship between Spans. This requires cross service context propagation, a mechanism where identifiers for a Trace are sent to remote processes.
Instrumentation libraries for HTTP frameworks and servers like Phoenix, Cowboy, Elli and clients like Tesla automatically inject or extract context using the globally registered propagators. By default the global propagators used are the W3C Trace Context and Baggage formats.
You can configure global propagators using the OTP application environment
variable text_map_propagators
:
%% sys.config
...
{text_map_propagators, [baggage,
trace_context]},
...
## runtime.exs
...
text_map_propagators: [:baggage, :trace_context],
...
You can also pass a comma separated list using the environment variable
OTEL_PROPAGATORS
. Both forms of configuration accept the values
trace_context
, baggage
, b3
and b3multi
.
Manual context propagation
To manually inject or extract context, you can use the
otel_propagator_text_map
module:
%% uses the context from the process dictionary to add to an empty list of headers
Headers = otel_propagator_text_map:inject([]),
%% creates a context in the process dictionary from Headers
otel_propagator_text_map:extract(Headers),
# uses the context from the process dictionary to add to an empty list of headers
headers = :otel_propagator_text_map.inject([])
# creates a context in the process dictionary from headers
:otel_propagator_text_map.extract(headers)
otel_propagator_text_map:inject/1
and otel_propagator_text_map:extract/1
use
globally registered propagators. To use a specific propagator
otel_propagator_text_map:inject/2
and otel_propagator_text_map:extract/2
can
be used with the first argument being the name of the propagator module to call.
Next steps
To learn more about propagation, read the Propagators API specification.
Feedback
Was this page helpful?
Thank you. Your feedback is appreciated!
Please let us know how we can improve this page. Your feedback is appreciated!