opentelemetry_spanprocessor_any/propagation/text_map_propagator.rs
1//! # Text Propagator
2//!
3//! `TextMapPropagator` is a formatter to serialize and deserialize a value into a
4//! text format.
5use crate::{
6 propagation::{Extractor, Injector},
7 Context,
8};
9use std::fmt::Debug;
10use std::slice;
11
12/// Methods to inject and extract a value as text into injectors and extractors that travel
13/// in-band across process boundaries.
14pub trait TextMapPropagator: Debug {
15 /// Properly encodes the values of the current [`Context`] and injects them into
16 /// the [`Injector`].
17 ///
18 /// [`Context`]: crate::Context
19 /// [`Injector`]: crate::propagation::Injector
20 fn inject(&self, injector: &mut dyn Injector) {
21 self.inject_context(&Context::current(), injector)
22 }
23
24 /// Properly encodes the values of the [`Context`] and injects them into the
25 /// [`Injector`].
26 ///
27 /// [`Context`]: crate::Context
28 /// [`Injector`]: crate::propagation::Injector
29 fn inject_context(&self, cx: &Context, injector: &mut dyn Injector);
30
31 /// Retrieves encoded data using the provided [`Extractor`]. If no data for this
32 /// format was retrieved OR if the retrieved data is invalid, then the current
33 /// [`Context`] is returned.
34 ///
35 /// [`Context`]: crate::Context
36 /// [`Injector`]: crate::propagation::Extractor
37 fn extract(&self, extractor: &dyn Extractor) -> Context {
38 self.extract_with_context(&Context::current(), extractor)
39 }
40
41 /// Retrieves encoded data using the provided [`Extractor`]. If no data for this
42 /// format was retrieved OR if the retrieved data is invalid, then the given
43 /// [`Context`] is returned.
44 ///
45 /// [`Context`]: crate::Context
46 /// [`Injector`]: crate::propagation::Extractor
47 fn extract_with_context(&self, cx: &Context, extractor: &dyn Extractor) -> Context;
48
49 /// Returns iter of fields used by [`TextMapPropagator`]
50 ///
51 fn fields(&self) -> FieldIter<'_>;
52}
53
54/// An iterator over fields of a [`TextMapPropagator`]
55///
56#[derive(Debug)]
57pub struct FieldIter<'a>(slice::Iter<'a, String>);
58
59impl<'a> FieldIter<'a> {
60 /// Create a new `FieldIter` from a slice of propagator fields
61 pub fn new(fields: &'a [String]) -> Self {
62 FieldIter(fields.iter())
63 }
64}
65
66impl<'a> Iterator for FieldIter<'a> {
67 type Item = &'a str;
68 fn next(&mut self) -> Option<Self::Item> {
69 self.0.next().map(|field| field.as_str())
70 }
71}