Skip to content

Commit 3fc263e

Browse files
refactor: extract set_send_span and set_server_span logic to helpers
1 parent ddfb5e6 commit 3fc263e

File tree

1 file changed

+70
-43
lines changed
  • instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi

1 file changed

+70
-43
lines changed

instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

Lines changed: 70 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,66 @@ async def otel_receive():
829829

830830
return otel_receive
831831

832+
def _set_send_span(
833+
self,
834+
server_span_name,
835+
scope,
836+
send,
837+
message,
838+
status_code,
839+
expecting_trailers,
840+
):
841+
"""Set send span attributes and status code."""
842+
with self.tracer.start_as_current_span(
843+
" ".join((server_span_name, scope["type"], "send"))
844+
) as send_span:
845+
if callable(self.client_response_hook):
846+
self.client_response_hook(send_span, scope, message)
847+
848+
if send_span.is_recording():
849+
if message["type"] == "http.response.start":
850+
expecting_trailers = message.get("trailers", False)
851+
send_span.set_attribute("asgi.event.type", message["type"])
852+
853+
if status_code:
854+
set_status_code(
855+
send_span,
856+
status_code,
857+
None,
858+
self._sem_conv_opt_in_mode,
859+
)
860+
return expecting_trailers
861+
862+
def _set_server_span(
863+
self, server_span, message, status_code, duration_attrs
864+
):
865+
"""Set server span attributes and status code."""
866+
if (
867+
server_span.is_recording()
868+
and server_span.kind == trace.SpanKind.SERVER
869+
and "headers" in message
870+
):
871+
custom_response_attributes = (
872+
collect_custom_headers_attributes(
873+
message,
874+
self.http_capture_headers_sanitize_fields,
875+
self.http_capture_headers_server_response,
876+
normalise_response_header_name,
877+
)
878+
if self.http_capture_headers_server_response
879+
else {}
880+
)
881+
if len(custom_response_attributes) > 0:
882+
server_span.set_attributes(custom_response_attributes)
883+
884+
if status_code:
885+
set_status_code(
886+
server_span,
887+
status_code,
888+
duration_attrs,
889+
self._sem_conv_opt_in_mode,
890+
)
891+
832892
def _get_otel_send(
833893
self,
834894
server_span,
@@ -850,52 +910,19 @@ async def otel_send(message: dict[str, Any]):
850910
status_code = 200
851911

852912
if not self.exclude_send_span:
853-
with self.tracer.start_as_current_span(
854-
" ".join((server_span_name, scope["type"], "send"))
855-
) as send_span:
856-
if callable(self.client_response_hook):
857-
self.client_response_hook(send_span, scope, message)
858-
859-
if send_span.is_recording():
860-
if message["type"] == "http.response.start":
861-
expecting_trailers = message.get("trailers", False)
862-
send_span.set_attribute(
863-
"asgi.event.type", message["type"]
864-
)
865-
if status_code:
866-
set_status_code(
867-
send_span,
868-
status_code,
869-
None,
870-
self._sem_conv_opt_in_mode,
871-
)
872-
873-
if (
874-
server_span.is_recording()
875-
and server_span.kind == trace.SpanKind.SERVER
876-
and "headers" in message
877-
):
878-
custom_response_attributes = (
879-
collect_custom_headers_attributes(
880-
message,
881-
self.http_capture_headers_sanitize_fields,
882-
self.http_capture_headers_server_response,
883-
normalise_response_header_name,
884-
)
885-
if self.http_capture_headers_server_response
886-
else {}
887-
)
888-
if len(custom_response_attributes) > 0:
889-
server_span.set_attributes(custom_response_attributes)
890-
891-
if status_code:
892-
set_status_code(
893-
server_span,
913+
expecting_trailers = self._set_send_span(
914+
server_span_name,
915+
scope,
916+
send,
917+
message,
894918
status_code,
895-
duration_attrs,
896-
self._sem_conv_opt_in_mode,
919+
expecting_trailers,
897920
)
898921

922+
self._set_server_span(
923+
server_span, message, status_code, duration_attrs
924+
)
925+
899926
propagator = get_global_response_propagator()
900927
if propagator:
901928
propagator.inject(

0 commit comments

Comments
 (0)