Beispiel #1
0
        def call_processor_with_span_context(self: Any, seqid: int,
                                             iprot: TProtocolBase,
                                             oprot: TProtocolBase) -> Any:
            context = baseplate.make_context_object()

            # Allow case-insensitivity for THeader headers
            headers: Mapping[bytes,
                             bytes] = CaseInsensitiveDict(  # type: ignore
                                 data=iprot.get_headers())

            trace_info: Optional[TraceInfo]
            try:
                sampled = bool(headers.get(b"Sampled") == b"1")
                flags = headers.get(b"Flags", None)
                trace_info = TraceInfo.from_upstream(
                    headers[b"Trace"].decode(),
                    headers[b"Parent"].decode(),
                    headers[b"Span"].decode(),
                    sampled,
                    int(flags) if flags is not None else None,
                )
            except (KeyError, ValueError):
                trace_info = None

            edge_payload = headers.get(b"Edge-Request", None)
            context.raw_edge_context = edge_payload
            if edge_context_factory:
                context.edge_context = edge_context_factory.from_upstream(
                    edge_payload)

            try:
                raw_deadline_budget = headers[b"Deadline-Budget"].decode()
                context.deadline_budget = float(raw_deadline_budget) / 1000
            except (KeyError, ValueError):
                context.deadline_budget = None

            span = baseplate.make_server_span(context,
                                              name=fn_name,
                                              trace_info=trace_info)

            try:
                service_name = headers[b"User-Agent"].decode()
            except (KeyError, UnicodeDecodeError):
                pass
            else:
                span.set_tag("peer.service", service_name)

            context.headers = headers

            handler = processor._handler
            context_aware_handler = _ContextAwareHandler(
                handler, context, logger)
            context_aware_processor = processor.__class__(
                context_aware_handler)
            return processor_fn(context_aware_processor, seqid, iprot, oprot)
Beispiel #2
0
        def call_processor_with_span_context(self: Any, seqid: int,
                                             iprot: TProtocolBase,
                                             oprot: TProtocolBase) -> Any:
            context = baseplate.make_context_object()

            # Allow case-insensitivity for THeader headers
            headers: Mapping[bytes,
                             bytes] = CaseInsensitiveDict(  # type: ignore
                                 data=iprot.get_headers())

            trace_info: Optional[TraceInfo]
            try:
                sampled = bool(headers.get(b"Sampled") == b"1")
                flags = headers.get(b"Flags", None)
                trace_info = TraceInfo.from_upstream(
                    int(headers[b"Trace"]),
                    int(headers[b"Parent"]),
                    int(headers[b"Span"]),
                    sampled,
                    int(flags) if flags is not None else None,
                )
            except (KeyError, ValueError):
                trace_info = None

            edge_payload = headers.get(b"Edge-Request", None)
            if edge_context_factory:
                edge_context = edge_context_factory.from_upstream(edge_payload)
                edge_context.attach_context(context)
            else:
                # just attach the raw context so it gets passed on
                # downstream even if we don't know how to handle it.
                context.raw_request_context = edge_payload

            baseplate.make_server_span(context,
                                       name=fn_name,
                                       trace_info=trace_info)

            context.headers = headers

            handler = processor._handler
            context_aware_handler = _ContextAwareHandler(
                handler, context, logger)
            context_aware_processor = processor.__class__(
                context_aware_handler)
            return processor_fn(context_aware_processor, seqid, iprot, oprot)