def create_span( self, name: str, parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN, kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL, ) -> "Span": """See `opentelemetry.trace.Tracer.create_span`.""" span_id = generate_span_id() if parent is Tracer.CURRENT_SPAN: parent = self.get_current_span() if parent is None: context = trace_api.SpanContext(generate_trace_id(), span_id) else: if isinstance(parent, trace_api.Span): parent_context = parent.get_context() elif isinstance(parent, trace_api.SpanContext): parent_context = parent else: raise TypeError context = trace_api.SpanContext( parent_context.trace_id, span_id, parent_context.trace_options, parent_context.trace_state, ) return Span( name=name, context=context, parent=parent, span_processor=self._active_span_processor, kind=kind, )
def start_span( # pylint: disable=too-many-locals self, name: str, parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN, kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL, attributes: Optional[types.Attributes] = None, links: Sequence[trace_api.Link] = (), start_time: Optional[int] = None, set_status_on_exception: bool = True, ) -> trace_api.Span: if parent is Tracer.CURRENT_SPAN: parent = trace_api.get_current_span() parent_context = parent if isinstance(parent_context, trace_api.Span): parent_context = parent.get_context() if parent_context is not None and not isinstance( parent_context, trace_api.SpanContext): raise TypeError("parent must be a Span, SpanContext or None.") if parent_context is None or not parent_context.is_valid: parent = parent_context = None trace_id = generate_trace_id() trace_flags = None trace_state = None else: trace_id = parent_context.trace_id trace_flags = parent_context.trace_flags trace_state = parent_context.trace_state context = trace_api.SpanContext( trace_id, generate_span_id(), is_remote=False, trace_flags=trace_flags, trace_state=trace_state, ) # The sampler decides whether to create a real or no-op span at the # time of span creation. No-op spans do not record events, and are not # exported. # The sampler may also add attributes to the newly-created span, e.g. # to include information about the sampling decision. sampling_decision = self.source.sampler.should_sample( parent_context, context.trace_id, context.span_id, name, attributes, links, ) if sampling_decision.sampled: options = context.trace_flags | trace_api.TraceFlags.SAMPLED context.trace_flags = trace_api.TraceFlags(options) if attributes is None: span_attributes = sampling_decision.attributes else: # apply sampling decision attributes after initial attributes span_attributes = attributes.copy() span_attributes.update(sampling_decision.attributes) # pylint:disable=protected-access span = Span( name=name, context=context, parent=parent_context, sampler=self.source.sampler, resource=self.source.resource, attributes=span_attributes, span_processor=self.source._active_span_processor, kind=kind, links=links, instrumentation_info=self.instrumentation_info, set_status_on_exception=set_status_on_exception, ) span.start(start_time=start_time) else: span = trace_api.DefaultSpan(context=context) return span
def create_span( self, name: str, parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN, kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL, ) -> "trace_api.Span": """See `opentelemetry.trace.Tracer.create_span`. If `parent` is null the new span will be created as a root span, i.e. a span with no parent context. By default, the new span will be created as a child of the current span in this tracer's context, or as a root span if no current span exists. """ span_id = generate_span_id() if parent is Tracer.CURRENT_SPAN: parent = self.get_current_span() parent_context = parent if isinstance(parent_context, trace_api.Span): parent_context = parent.get_context() if parent_context is not None and not isinstance( parent_context, trace_api.SpanContext): raise TypeError if parent_context is None or not parent_context.is_valid(): parent = parent_context = None trace_id = generate_trace_id() trace_options = None trace_state = None else: trace_id = parent_context.trace_id trace_options = parent_context.trace_options trace_state = parent_context.trace_state context = trace_api.SpanContext(trace_id, span_id, trace_options, trace_state) # The sampler decides whether to create a real or no-op span at the # time of span creation. No-op spans do not record events, and are not # exported. # The sampler may also add attributes to the newly-created span, e.g. # to include information about the sampling decision. sampling_decision = self.sampler.should_sample( parent_context, context.trace_id, context.span_id, name, {}, # TODO: links ) if sampling_decision.sampled: return Span( name=name, context=context, parent=parent, sampler=self.sampler, attributes=sampling_decision.attributes, span_processor=self._active_span_processor, kind=kind, ) return trace_api.DefaultSpan(context=context)
def start_span( self, name: str, parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN, kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL, attributes: Optional[types.Attributes] = None, links: Sequence[trace_api.Link] = (), start_time: Optional[int] = None, ) -> "Span": """See `opentelemetry.trace.Tracer.start_span`.""" if parent is Tracer.CURRENT_SPAN: parent = self.get_current_span() parent_context = parent if isinstance(parent_context, trace_api.Span): parent_context = parent.get_context() if parent_context is not None and not isinstance( parent_context, trace_api.SpanContext ): raise TypeError if parent_context is None or not parent_context.is_valid(): parent = parent_context = None trace_id = generate_trace_id() trace_options = None trace_state = None else: trace_id = parent_context.trace_id trace_options = parent_context.trace_options trace_state = parent_context.trace_state context = trace_api.SpanContext( trace_id, generate_span_id(), trace_options, trace_state ) # The sampler decides whether to create a real or no-op span at the # time of span creation. No-op spans do not record events, and are not # exported. # The sampler may also add attributes to the newly-created span, e.g. # to include information about the sampling decision. sampling_decision = self.sampler.should_sample( parent_context, context.trace_id, context.span_id, name, attributes, links, ) if sampling_decision.sampled: if attributes is None: span_attributes = sampling_decision.attributes else: # apply sampling decision attributes after initial attributes span_attributes = attributes.copy() span_attributes.update(sampling_decision.attributes) span = Span( name=name, context=context, parent=parent, sampler=self.sampler, attributes=span_attributes, span_processor=self._active_span_processor, kind=kind, links=links, ) span.start(start_time=start_time) else: span = trace_api.DefaultSpan(context=context) return span