def test_tracer_inject_map(tracer): span_context = OTSpanContext(trace_parent=TraceParent.from_string( "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01")) carrier = {} tracer.inject(span_context, Format.TEXT_MAP, carrier) assert carrier[ "elastic-apm-traceparent"] == b"00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"
def extract(self, format, carrier): if format in (Format.HTTP_HEADERS, Format.TEXT_MAP): trace_parent = disttracing.TraceParent.from_headers(carrier) if not trace_parent: raise SpanContextCorruptedException( "could not extract span context from carrier") return OTSpanContext(trace_parent=trace_parent) raise UnsupportedFormatException
def test_tracer_inject_map(tracer): span_context = OTSpanContext( trace_parent=TraceParent.from_string("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01") ) carrier = {} tracer.inject(span_context, Format.TEXT_MAP, carrier) assert carrier[constants.TRACEPARENT_HEADER_NAME] == b"00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01" if tracer._agent.config.use_elastic_traceparent_header: assert carrier[constants.TRACEPARENT_LEGACY_HEADER_NAME] == carrier[constants.TRACEPARENT_HEADER_NAME]
def extract(self, format, carrier): if format in (Format.HTTP_HEADERS, Format.TEXT_MAP): if constants.TRACEPARENT_HEADER_NAME not in carrier: raise SpanContextCorruptedException( "could not extract span context from carrier") trace_parent = disttracing.TraceParent.from_string( carrier[constants.TRACEPARENT_HEADER_NAME]) return OTSpanContext(trace_parent=trace_parent) raise UnsupportedFormatException
def start_span(self, operation_name=None, child_of=None, references=None, tags=None, start_time=None, ignore_active_span=False): if isinstance(child_of, OTSpanContext): parent_context = child_of elif isinstance(child_of, OTSpan): parent_context = child_of.context elif references and references[0].type == ReferenceType.CHILD_OF: parent_context = references[0].referenced_context else: parent_context = None transaction = traces.execution_context.get_transaction() if not transaction: trace_parent = parent_context.trace_parent if parent_context else None transaction = self._agent.begin_transaction( "custom", trace_parent=trace_parent) transaction.name = operation_name span_context = OTSpanContext(trace_parent=transaction.trace_parent) ot_span = OTSpan(self, span_context, transaction) else: # to allow setting an explicit parent span, we check if the parent_context is set # and if it is a span. In all other cases, the parent is found implicitly through the # execution context. parent_span_id = (parent_context.span.elastic_apm_ref.id if parent_context and parent_context.span and not parent_context.span.is_transaction else None) span = transaction._begin_span(operation_name, None, parent_span_id=parent_span_id) trace_parent = parent_context.trace_parent if parent_context else transaction.trace_parent span_context = OTSpanContext(trace_parent=trace_parent.copy_from( span_id=span.id)) ot_span = OTSpan(self, span_context, span) if tags: for k, v in compat.iteritems(tags): ot_span.set_tag(k, v) return ot_span
def test_tracer_inject_binary(tracer): span_context = OTSpanContext(trace_parent=TraceParent.from_string( "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01")) with pytest.raises(opentracing.UnsupportedFormatException): tracer.inject(span_context, Format.BINARY, {})