fn build_with_context(&self, mut builder: SpanBuilder, parent_cx: &Context) -> Self::Span {
        let provider = self.provider();
        if provider.is_none() {
            return Span::new(
                SpanContext::empty_context(),
                None,
                self.clone(),
                SpanLimits::default(),
            );
        }

        let provider = provider.unwrap();
        let config = provider.config();
        let span_limits = config.span_limits;
        let span_id = builder
            .span_id
            .take()
            .unwrap_or_else(|| config.id_generator.new_span_id());
        let span_kind = builder.span_kind.take().unwrap_or(SpanKind::Internal);
        let mut attribute_options = builder.attributes.take().unwrap_or_default();
        let mut link_options = builder.links.take();
        let mut no_parent = true;
        let mut remote_parent = false;
        let mut parent_span_id = SpanId::INVALID;
        let mut parent_trace_flags = TraceFlags::default();
        let trace_id;

        let parent_span = if parent_cx.has_active_span() {
            Some(parent_cx.span())
        } else {
            None
        };

        // Build context for sampling decision
        if let Some(sc) = parent_span.as_ref().map(|parent| parent.span_context()) {
            no_parent = false;
            remote_parent = sc.is_remote();
            parent_span_id = sc.span_id();
            parent_trace_flags = sc.trace_flags();
            trace_id = sc.trace_id();
        } else {
            trace_id = builder
                .trace_id
                .unwrap_or_else(|| config.id_generator.new_trace_id());
        };

        // There are 3 paths for sampling.
        //
        // * Sampling has occurred elsewhere and is already stored in the builder
        // * There is no parent or a remote parent, in which case make decision now
        // * There is a local parent, in which case defer to the parent's decision
        let sampling_decision = if let Some(sampling_result) = builder.sampling_result.take() {
            self.process_sampling_result(sampling_result, parent_cx)
        } else if no_parent || remote_parent {
            self.make_sampling_decision(
                parent_cx,
                trace_id,
                &builder.name,
                &span_kind,
                &attribute_options,
                link_options.as_deref().unwrap_or(&[]),
                provider.config(),
                &self.instrumentation_lib,
            )
        } else {
            // has parent that is local: use parent if sampled, or don't record.
            parent_span
                .filter(|span| span.span_context().is_sampled())
                .map(|span| {
                    (
                        parent_trace_flags,
                        Vec::new(),
                        span.span_context().trace_state().clone(),
                    )
                })
        };

        let SpanBuilder {
            name,
            start_time,
            end_time,
            events,
            status,
            ..
        } = builder;

        // Build optional inner context, `None` if not recording.
        let mut span = if let Some((flags, extra_attrs, trace_state)) = sampling_decision {
            for extra_attr in extra_attrs {
                attribute_options.insert(extra_attr.key, extra_attr.value);
            }
            let mut attributes =
                EvictedHashMap::new(span_limits.max_attributes_per_span, attribute_options.len());
            for (key, value) in attribute_options {
                attributes.insert(KeyValue::new(key, value));
            }
            let mut links = EvictedQueue::new(span_limits.max_links_per_span);
            if let Some(link_options) = &mut link_options {
                let link_attributes_limit = span_limits.max_attributes_per_link as usize;
                for link in link_options.iter_mut() {
                    let dropped_attributes_count =
                        link.attributes.len().saturating_sub(link_attributes_limit);
                    link.attributes.truncate(link_attributes_limit);
                    link.dropped_attributes_count = dropped_attributes_count as u32;
                }
                links.append_vec(link_options);
            }
            let start_time = start_time.unwrap_or_else(opentelemetry_api::time::now);
            let end_time = end_time.unwrap_or(start_time);
            let mut events_queue = EvictedQueue::new(span_limits.max_events_per_span);
            if let Some(mut events) = events {
                let event_attributes_limit = span_limits.max_attributes_per_event as usize;
                for event in events.iter_mut() {
                    let dropped_attributes_count = event
                        .attributes
                        .len()
                        .saturating_sub(event_attributes_limit);
                    event.attributes.truncate(event_attributes_limit);
                    event.dropped_attributes_count = dropped_attributes_count as u32;
                }
                events_queue.append_vec(&mut events);
            }

            let span_context = SpanContext::new(trace_id, span_id, flags, false, trace_state);
            Span::new(
                span_context,
                Some(SpanData {
                    parent_span_id,
                    span_kind,
                    name,
                    start_time,
                    end_time,
                    attributes,
                    events: events_queue,
                    links,
                    status,
                }),
                self.clone(),
                span_limits,
            )
        } else {
            let span_context = SpanContext::new(
                trace_id,
                span_id,
                TraceFlags::default(),
                false,
                Default::default(),
            );
            Span::new(span_context, None, self.clone(), span_limits)
        };

        // Call `on_start` for all processors
        for processor in provider.span_processors() {
            processor.on_start(&mut span, parent_cx)
        }

        span
    }
/// Starts a span from a `SpanBuilder`.
///
/// Each span has zero or one parent spans and zero or more child spans, which
/// represent causally related operations. A tree of related spans comprises a
/// trace. A span is said to be a _root span_ if it does not have a parent. Each
/// trace includes a single root span, which is the shared ancestor of all other
/// spans in the trace.
Starts a span from a `SpanBuilder`.
Each span has zero or one parent spans and zero or more child spans, which
represent causally related operations. A tree of related spans comprises a
trace. A span is said to be a _root span_ if it does not have a parent. Each
trace includes a single root span, which is the shared ancestor of all other
spans in the trace.
Starts a span from a ` SpanBuilder ` . Each span has zero or one parent spans and zero or more child spans which represent causally related operations . A tree of related spans comprises a trace . A span is said to be a _root span_ if it does not have a parent . Each trace includes a single root span which is the shared ancestor of all other spans in the trace .
fn build_with_context(&self, mut builder: SpanBuilder, parent_cx: &Context) -> Self::Span {
        let provider = self.provider();
        if provider.is_none() {
            return Span::new(
                SpanContext::empty_context(),
                None,
                self.clone(),
                SpanLimits::default(),
            );
        }
        let provider = provider.unwrap();
        let config = provider.config();
        let span_limits = config.span_limits;
        let span_id = builder
            .span_id
            .take()
            .unwrap_or_else(|| config.id_generator.new_span_id());
        let span_kind = builder.span_kind.take().unwrap_or(SpanKind::Internal);
        let mut attribute_options = builder.attributes.take().unwrap_or_default();
        let mut link_options = builder.links.take();
        let mut no_parent = true;
        let mut remote_parent = false;
        let mut parent_span_id = SpanId::INVALID;
        let mut parent_trace_flags = TraceFlags::default();
        let trace_id;
        let parent_span = if parent_cx.has_active_span() {
            Some(parent_cx.span())
        } else {
            None
        };
        // Build context for sampling decision
        if let Some(sc) = parent_span.as_ref().map(|parent| parent.span_context()) {
            no_parent = false;
            remote_parent = sc.is_remote();
            parent_span_id = sc.span_id();
            parent_trace_flags = sc.trace_flags();
            trace_id = sc.trace_id();
        } else {
            trace_id = builder
                .trace_id
                .unwrap_or_else(|| config.id_generator.new_trace_id());
        };
        // There are 3 paths for sampling.
        //
        // * Sampling has occurred elsewhere and is already stored in the builder
        // * There is no parent or a remote parent, in which case make decision now
        // * There is a local parent, in which case defer to the parent's decision
        let sampling_decision = if let Some(sampling_result) = builder.sampling_result.take() {
            self.process_sampling_result(sampling_result, parent_cx)
        } else if no_parent || remote_parent {
            self.make_sampling_decision(
                parent_cx,
                trace_id,
                &builder.name,
                &span_kind,
                &attribute_options,
                link_options.as_deref().unwrap_or(&[]),
                provider.config(),
                &self.instrumentation_lib,
            )
        } else {
            // has parent that is local: use parent if sampled, or don't record.
            parent_span
                .filter(|span| span.span_context().is_sampled())
                .map(|span| {
                    (
                        parent_trace_flags,
                        Vec::new(),
                        span.span_context().trace_state().clone(),
                    )
                })
        };
        let SpanBuilder {
            name,
            start_time,
            end_time,
            events,
            status,
            ..
        } = builder;
        // Build optional inner context, `None` if not recording.
        let mut span = if let Some((flags, extra_attrs, trace_state)) = sampling_decision {
            for extra_attr in extra_attrs {
                attribute_options.insert(extra_attr.key, extra_attr.value);
            }
            let mut attributes =
                EvictedHashMap::new(span_limits.max_attributes_per_span, attribute_options.len());
            for (key, value) in attribute_options {
                attributes.insert(KeyValue::new(key, value));
            }
            let mut links = EvictedQueue::new(span_limits.max_links_per_span);
            if let Some(link_options) = &mut link_options {
                let link_attributes_limit = span_limits.max_attributes_per_link as usize;
                for link in link_options.iter_mut() {
                    let dropped_attributes_count =
                        link.attributes.len().saturating_sub(link_attributes_limit);
                    link.attributes.truncate(link_attributes_limit);
                    link.dropped_attributes_count = dropped_attributes_count as u32;
                }
                links.append_vec(link_options);
            }
            let start_time = start_time.unwrap_or_else(opentelemetry_api::time::now);
            let end_time = end_time.unwrap_or(start_time);
            let mut events_queue = EvictedQueue::new(span_limits.max_events_per_span);
            if let Some(mut events) = events {
                let event_attributes_limit = span_limits.max_attributes_per_event as usize;
                for event in events.iter_mut() {
                    let dropped_attributes_count = event
                        .attributes
                        .len()
                        .saturating_sub(event_attributes_limit);
                    event.attributes.truncate(event_attributes_limit);
                    event.dropped_attributes_count = dropped_attributes_count as u32;
                }
                events_queue.append_vec(&mut events);
            }
            let span_context = SpanContext::new(trace_id, span_id, flags, false, trace_state);
            Span::new(
                span_context,
                Some(SpanData {
                    parent_span_id,
                    span_kind,
                    name,
                    start_time,
                    end_time,
                    attributes,
                    events: events_queue,
                    links,
                    status,
                }),
                self.clone(),
                span_limits,
            )
        } else {
            let span_context = SpanContext::new(
                trace_id,
                span_id,
                TraceFlags::default(),
                false,
                Default::default(),
            );
            Span::new(span_context, None, self.clone(), span_limits)
        };
        // Call `on_start` for all processors
        for processor in provider.span_processors() {
            processor.on_start(&mut span, parent_cx)
        }
        span
    }
fn build_with_context ( & self , mut builder : SpanBuilder , parent_cx : & Context ) -> Self :: Span { let provider = self . provider ( ) ; if provider . is_none ( ) { return Span :: new ( SpanContext :: empty_context ( ) , None , self . clone ( ) , SpanLimits :: default ( ) , ) ; } let provider = provider . unwrap ( ) ; let config = provider . config ( ) ; let span_limits = config . span_limits ; let span_id = builder . span_id . take ( ) . unwrap_or_else ( | | config . id_generator . new_span_id ( ) ) ; let span_kind = builder . span_kind . take ( ) . unwrap_or ( SpanKind :: Internal ) ; let mut attribute_options = builder . attributes . take ( ) . unwrap_or_default ( ) ; let mut link_options = builder . links . take ( ) ; let mut no_parent = true ; let mut remote_parent = false ; let mut parent_span_id = SpanId :: INVALID ; let mut parent_trace_flags = TraceFlags :: default ( ) ; let trace_id ; let parent_span = if parent_cx . has_active_span ( ) { Some ( parent_cx . span ( ) ) } else { None } ; // Build context for sampling decision if let Some ( sc ) = parent_span . as_ref ( ) . map ( | parent | parent . span_context ( ) ) { no_parent = false ; remote_parent = sc . is_remote ( ) ; parent_span_id = sc . span_id ( ) ; parent_trace_flags = sc . trace_flags ( ) ; trace_id = sc . trace_id ( ) ; } else { trace_id = builder . trace_id . unwrap_or_else ( | | config . id_generator . new_trace_id ( ) ) ; } ; // There are 3 paths for sampling. // // * Sampling has occurred elsewhere and is already stored in the builder // * There is no parent or a remote parent, in which case make decision now // * There is a local parent, in which case defer to the parent's decision let sampling_decision = if let Some ( sampling_result ) = builder . sampling_result . take ( ) { self . process_sampling_result ( sampling_result , parent_cx ) } else if no_parent || remote_parent { self . make_sampling_decision ( parent_cx , trace_id , & builder . name , & span_kind , & attribute_options , link_options . as_deref ( ) . unwrap_or ( & [ ] ) , provider . config ( ) , & self . instrumentation_lib , ) } else { // has parent that is local: use parent if sampled, or don't record. parent_span . filter ( | span | span . span_context ( ) . is_sampled ( ) ) . map ( | span | { ( parent_trace_flags , Vec :: new ( ) , span . span_context ( ) . trace_state ( ) . clone ( ) , ) } ) } ; let SpanBuilder { name , start_time , end_time , events , status , .. } = builder ; // Build optional inner context, `None` if not recording. let mut span = if let Some ( ( flags , extra_attrs , trace_state ) ) = sampling_decision { for extra_attr in extra_attrs { attribute_options . insert ( extra_attr . key , extra_attr . value ) ; } let mut attributes = EvictedHashMap :: new ( span_limits . max_attributes_per_span , attribute_options . len ( ) ) ; for ( key , value ) in attribute_options { attributes . insert ( KeyValue :: new ( key , value ) ) ; } let mut links = EvictedQueue :: new ( span_limits . max_links_per_span ) ; if let Some ( link_options ) = & mut link_options { let link_attributes_limit = span_limits . max_attributes_per_link as usize ; for link in link_options . iter_mut ( ) { let dropped_attributes_count = link . attributes . len ( ) . saturating_sub ( link_attributes_limit ) ; link . attributes . truncate ( link_attributes_limit ) ; link . dropped_attributes_count = dropped_attributes_count as u32 ; } links . append_vec ( link_options ) ; } let start_time = start_time . unwrap_or_else ( opentelemetry_api :: time :: now ) ; let end_time = end_time . unwrap_or ( start_time ) ; let mut events_queue = EvictedQueue :: new ( span_limits . max_events_per_span ) ; if let Some ( mut events ) = events { let event_attributes_limit = span_limits . max_attributes_per_event as usize ; for event in events . iter_mut ( ) { let dropped_attributes_count = event . attributes . len ( ) . saturating_sub ( event_attributes_limit ) ; event . attributes . truncate ( event_attributes_limit ) ; event . dropped_attributes_count = dropped_attributes_count as u32 ; } events_queue . append_vec ( & mut events ) ; } let span_context = SpanContext :: new ( trace_id , span_id , flags , false , trace_state ) ; Span :: new ( span_context , Some ( SpanData { parent_span_id , span_kind , name , start_time , end_time , attributes , events : events_queue , links , status , } ) , self . clone ( ) , span_limits , ) } else { let span_context = SpanContext :: new ( trace_id , span_id , TraceFlags :: default ( ) , false , Default :: default ( ) , ) ; Span :: new ( span_context , None , self . clone ( ) , span_limits ) } ; // Call `on_start` for all processors for processor in provider . span_processors ( ) { processor . on_start ( & mut span , parent_cx ) } span }
Starts a span from a `SpanBuilder`.
Starts a span from a ` SpanBuilder ` .
&self
builder
SpanBuilder
parent_cx
&Context
&self
builder
SpanBuilder
parent_cx
&Context