Skip to content

Commit 356cc4a

Browse files
Instrument _ByteProducer with tracing to measure potential dead time while writing bytes to the request (#18804)
This will allow to easily see how much time is taken up by being able to filter by the `write_bytes_to_request` operation in Jaeger. Spawning from #17722 The `write_bytes_to_request` span won't show up in the trace until #18849 is merged. Note: It's totally fine for a span child to finish after the parent. See https://opentracing.io/specification/#references-between-spans which shows "Child Span D" outliving the "Parent Span"
1 parent 27fc338 commit 356cc4a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

changelog.d/18804.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Instrument `_ByteProducer` with tracing to measure potential dead time while writing bytes to the request.

synapse/http/server.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,10 @@ def __init__(
702702
self._request: Optional[Request] = request
703703
self._iterator = iterator
704704
self._paused = False
705+
self.tracing_scope = start_active_span(
706+
"write_bytes_to_request",
707+
)
708+
self.tracing_scope.__enter__()
705709

706710
try:
707711
self._request.registerProducer(self, True)
@@ -712,8 +716,8 @@ def __init__(
712716
logger.info("Connection disconnected before response was written: %r", e)
713717

714718
# We drop our references to data we'll not use.
715-
self._request = None
716719
self._iterator = iter(())
720+
self.tracing_scope.__exit__(type(e), None, e.__traceback__)
717721
else:
718722
# Start producing if `registerProducer` was successful
719723
self.resumeProducing()
@@ -727,6 +731,9 @@ def _send_data(self, data: List[bytes]) -> None:
727731
self._request.write(b"".join(data))
728732

729733
def pauseProducing(self) -> None:
734+
opentracing_span = active_span()
735+
if opentracing_span is not None:
736+
opentracing_span.log_kv({"event": "producer_paused"})
730737
self._paused = True
731738

732739
def resumeProducing(self) -> None:
@@ -737,6 +744,10 @@ def resumeProducing(self) -> None:
737744

738745
self._paused = False
739746

747+
opentracing_span = active_span()
748+
if opentracing_span is not None:
749+
opentracing_span.log_kv({"event": "producer_resumed"})
750+
740751
# Write until there's backpressure telling us to stop.
741752
while not self._paused:
742753
# Get the next chunk and write it to the request.
@@ -771,6 +782,7 @@ def resumeProducing(self) -> None:
771782
def stopProducing(self) -> None:
772783
# Clear a circular reference.
773784
self._request = None
785+
self.tracing_scope.__exit__(None, None, None)
774786

775787

776788
def _encode_json_bytes(json_object: object) -> bytes:

0 commit comments

Comments
 (0)