Skip to content

Refactoring asgi interceptor to use _start_internal_or_server_span function. #898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,13 @@ def client_response_hook(span: Span, message: dict):
from opentelemetry.instrumentation.propagators import (
get_global_response_propagator,
)
from opentelemetry.instrumentation.utils import http_status_to_status_code
from opentelemetry.propagate import extract
from opentelemetry.instrumentation.utils import (
_start_internal_or_server_span,
http_status_to_status_code,
)
from opentelemetry.propagators.textmap import Getter, Setter
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import (
INVALID_SPAN,
Span,
SpanKind,
set_span_in_context,
)
from opentelemetry.trace import Span, set_span_in_context
from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.util.http import remove_url_credentials

Expand Down Expand Up @@ -327,24 +324,18 @@ async def __call__(self, scope, receive, send):
if self.excluded_urls and self.excluded_urls.url_disabled(url):
return await self.app(scope, receive, send)

token = ctx = span_kind = None

if trace.get_current_span() is INVALID_SPAN:
ctx = extract(scope, getter=asgi_getter)
token = context.attach(ctx)
span_kind = SpanKind.SERVER
else:
ctx = context.get_current()
span_kind = SpanKind.INTERNAL

span_name, additional_attributes = self.default_span_details(scope)

span, token = _start_internal_or_server_span(
tracer=self.tracer,
span_name=span_name,
start_time=None,
context_carrier=scope,
context_getter=asgi_getter,
)

try:
with self.tracer.start_as_current_span(
span_name,
context=ctx,
kind=span_kind,
) as current_span:
with trace.use_span(span, end_on_exit=True) as current_span:
if current_span.is_recording():
attributes = collect_request_attributes(scope)
attributes.update(additional_attributes)
Expand Down