Skip to content

Commit 8294a6f

Browse files
committed
copy logging changes to eh
1 parent cd2cf68 commit 8294a6f

23 files changed

Lines changed: 769 additions & 255 deletions

sdk/eventhub/azure-eventhub/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
### Other Changes
1212

13+
- Updated network trace logging to include AMQP connection/session/link names for advanced usage.
14+
1315
## 5.11.6 (2024-02-12)
1416

1517
This version and all future versions will require Python 3.8+. Python 3.7 is no longer supported.

sdk/eventhub/azure-eventhub/azure/eventhub/_pyamqp/_connection.py

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ def _set_state(self, new_state: ConnectionState) -> None:
209209
previous_state = self.state
210210
self.state = new_state
211211
_LOGGER.info(
212-
"Connection state changed: %r -> %r",
212+
"[Connection:%s] Connection state changed: %r -> %r",
213+
self._network_trace_params["amqpConnection"],
213214
previous_state,
214-
new_state,
215-
extra=self._network_trace_params
215+
new_state
216216
)
217217
for session in self._outgoing_endpoints.values():
218218
session._on_connection_state_change() # pylint:disable=protected-access
@@ -321,7 +321,11 @@ def _send_frame(self, channel: int, frame: NamedTuple, **kwargs: Any) -> None:
321321
except Exception: # pylint:disable=try-except-raise
322322
raise
323323
else:
324-
_LOGGER.info("Cannot write frame in current state: %r", self.state, extra=self._network_trace_params)
324+
_LOGGER.info(
325+
"[Connection:%s] Cannot write frame in current state: %r",
326+
self._network_trace_params["amqpConnection"],
327+
self.state
328+
)
325329

326330
def _get_next_outgoing_channel(self) -> int:
327331
"""Get the next available outgoing channel number within the max channel limit.
@@ -338,7 +342,10 @@ def _get_next_outgoing_channel(self) -> int:
338342
def _outgoing_empty(self) -> None:
339343
"""Send an empty frame to prevent the connection from reaching an idle timeout."""
340344
if self._network_trace:
341-
_LOGGER.debug("-> EmptyFrame()", extra=self._network_trace_params)
345+
_LOGGER.debug(
346+
"[Connection:%s] -> EmptyFrame()",
347+
self._network_trace_params["amqpConnection"]
348+
)
342349
if self._error:
343350
raise self._error
344351

@@ -359,7 +366,11 @@ def _outgoing_header(self)-> None:
359366
"""Send the AMQP protocol header to initiate the connection."""
360367
self._last_frame_sent_time = time.time()
361368
if self._network_trace:
362-
_LOGGER.debug("-> Header(%r)", HEADER_FRAME, extra=self._network_trace_params)
369+
_LOGGER.debug(
370+
"[Connection:%s] -> Header(%r)",
371+
self._network_trace_params["amqpConnection"],
372+
HEADER_FRAME
373+
)
363374
self._transport.write(HEADER_FRAME)
364375

365376
def _incoming_header(self, _, frame: bytes) -> None:
@@ -369,7 +380,11 @@ def _incoming_header(self, _, frame: bytes) -> None:
369380
:param bytes frame: The incoming frame.
370381
"""
371382
if self._network_trace:
372-
_LOGGER.debug("<- Header(%r)", frame, extra=self._network_trace_params)
383+
_LOGGER.debug(
384+
"[Connection:%s] <- Header(%r)",
385+
self._network_trace_params["amqpConnection"],
386+
frame,
387+
)
373388
if self.state == ConnectionState.START:
374389
self._set_state(ConnectionState.HDR_RCVD)
375390
elif self.state == ConnectionState.HDR_SENT:
@@ -392,7 +407,11 @@ def _outgoing_open(self) -> None:
392407
properties=self._properties,
393408
)
394409
if self._network_trace:
395-
_LOGGER.debug("-> %r", open_frame, extra=self._network_trace_params)
410+
_LOGGER.debug(
411+
"[Connection:%s] -> %r",
412+
self._network_trace_params["amqpConnection"],
413+
open_frame
414+
)
396415
self._send_frame(0, open_frame)
397416

398417
def _incoming_open(self, channel: int, frame) -> None:
@@ -418,17 +437,27 @@ def _incoming_open(self, channel: int, frame) -> None:
418437
"""
419438
# TODO: Add type hints for full frame tuple contents.
420439
if self._network_trace:
421-
_LOGGER.debug("<- %r", OpenFrame(*frame), extra=self._network_trace_params)
440+
_LOGGER.debug(
441+
"[Connection:%s] <- %r",
442+
self._network_trace_params["amqpConnection"],
443+
OpenFrame(*frame)
444+
)
422445
if channel != 0:
423-
_LOGGER.error("OPEN frame received on a channel that is not 0.", extra=self._network_trace_params)
446+
_LOGGER.error(
447+
"[Connection:%s] OPEN frame received on a channel that is not 0.",
448+
self._network_trace_params["amqpConnection"]
449+
)
424450
self.close(
425451
error=AMQPError(
426452
condition=ErrorCondition.NotAllowed, description="OPEN frame received on a channel that is not 0."
427453
)
428454
)
429455
self._set_state(ConnectionState.END)
430456
if self.state == ConnectionState.OPENED:
431-
_LOGGER.error("OPEN frame received in the OPENED state.", extra=self._network_trace_params)
457+
_LOGGER.error(
458+
"[Connection:%s] OPEN frame received in the OPENED state.",
459+
self._network_trace_params["amqpConnection"]
460+
)
432461
self.close()
433462
if frame[4]:
434463
self._remote_idle_timeout = cast(float, frame[4] / 1000) # Convert to seconds
@@ -447,8 +476,8 @@ def _incoming_open(self, channel: int, frame) -> None:
447476
)
448477
)
449478
_LOGGER.error(
450-
"Failed parsing OPEN frame: Max frame size is less than supported minimum.",
451-
extra=self._network_trace_params
479+
"[Connection:%s] Failed parsing OPEN frame: Max frame size is less than supported minimum.",
480+
self._network_trace_params["amqpConnection"]
452481
)
453482
return
454483
self._remote_max_frame_size = frame[2]
@@ -466,15 +495,23 @@ def _incoming_open(self, channel: int, frame) -> None:
466495
description=f"connection is an illegal state: {self.state}",
467496
)
468497
)
469-
_LOGGER.error("Connection is an illegal state: %r", self.state, extra=self._network_trace_params)
498+
_LOGGER.error(
499+
"[Connection:%s] Connection is an illegal state: %r",
500+
self._network_trace_params["amqpConnection"],
501+
self.state
502+
)
470503

471504
def _outgoing_close(self, error: Optional[AMQPError] = None) -> None:
472505
"""Send a Close frame to shutdown connection with optional error information.
473506
:param ~pyamqp.error.AMQPError or None error: Optional error information.
474507
"""
475508
close_frame = CloseFrame(error=error)
476509
if self._network_trace:
477-
_LOGGER.debug("-> %r", close_frame, extra=self._network_trace_params)
510+
_LOGGER.debug(
511+
"[Connection:%s] -> %r",
512+
self._network_trace_params["amqpConnection"],
513+
close_frame
514+
)
478515
self._send_frame(0, close_frame)
479516

480517
def _incoming_close(self, channel: int, frame: Tuple[Any, ...]) -> None:
@@ -488,7 +525,11 @@ def _incoming_close(self, channel: int, frame: Tuple[Any, ...]) -> None:
488525
:param tuple frame: The incoming Close frame.
489526
"""
490527
if self._network_trace:
491-
_LOGGER.debug("<- %r", CloseFrame(*frame), extra=self._network_trace_params)
528+
_LOGGER.debug(
529+
"[Connection:%s] <- %r",
530+
self._network_trace_params["amqpConnection"],
531+
CloseFrame(*frame),
532+
)
492533
disconnect_states = [
493534
ConnectionState.HDR_RCVD,
494535
ConnectionState.HDR_EXCH,
@@ -503,8 +544,8 @@ def _incoming_close(self, channel: int, frame: Tuple[Any, ...]) -> None:
503544
close_error = None
504545
if channel > self._channel_max:
505546
_LOGGER.error(
506-
"CLOSE frame received on a channel greated than support max.",
507-
extra=self._network_trace_params
547+
"[Connection:%s] CLOSE frame received on a channel greated than support max.",
548+
self._network_trace_params["amqpConnection"]
508549
)
509550
close_error = AMQPError(condition=ErrorCondition.InvalidField, description="Invalid channel", info=None)
510551

@@ -517,8 +558,9 @@ def _incoming_close(self, channel: int, frame: Tuple[Any, ...]) -> None:
517558
condition=frame[0][0], description=frame[0][1], info=frame[0][2]
518559
)
519560
_LOGGER.error(
520-
"Connection closed with error: %r", frame[0],
521-
extra=self._network_trace_params
561+
"[Connection:%s] Connection closed with error: %r",
562+
self._network_trace_params["amqpConnection"],
563+
frame[0],
522564
)
523565

524566

@@ -577,8 +619,8 @@ def _incoming_end(self, channel: int, frame: Tuple[Any, ...]) -> None:
577619
description="Invalid channel number received"
578620
))
579621
_LOGGER.error(
580-
"END frame received on invalid channel. Closing connection.",
581-
extra=self._network_trace_params
622+
"[Connection:%s] END frame received on invalid channel. Closing connection.",
623+
self._network_trace_params["amqpConnection"]
582624
)
583625
return
584626

@@ -643,7 +685,11 @@ def _process_incoming_frame(self, channel: int, frame: Optional[Union[bytes, Tup
643685
return True
644686
if performative == 1:
645687
return False
646-
_LOGGER.error("Unrecognized incoming frame: %r", frame, extra=self._network_trace_params)
688+
_LOGGER.error(
689+
"[Connection:%s] Unrecognized incoming frame: %r",
690+
self._network_trace_params["amqpConnection"],
691+
frame
692+
)
647693
return True
648694
except KeyError:
649695
return True # TODO: channel error
@@ -675,8 +721,8 @@ def _process_outgoing_frame(self, channel: int, frame) -> None:
675721
cast(float, self._last_frame_received_time),
676722
) or self._get_remote_timeout(now):
677723
_LOGGER.info(
678-
"No frame received for the idle timeout. Closing connection.",
679-
extra=self._network_trace_params
724+
"[Connection:%s] No frame received for the idle timeout. Closing connection.",
725+
self._network_trace_params["amqpConnection"]
680726
)
681727
self.close(
682728
error=AMQPError(
@@ -755,8 +801,8 @@ def listen(self, wait: Union[float, bool] = False, batch: int = 1, **kwargs: Any
755801
now
756802
):
757803
_LOGGER.info(
758-
"No frame received for the idle timeout. Closing connection.",
759-
extra=self._network_trace_params
804+
"[Connection:%s] No frame received for the idle timeout. Closing connection.",
805+
self._network_trace_params["amqpConnection"]
760806
)
761807
self.close(
762808
error=AMQPError(
@@ -777,9 +823,9 @@ def listen(self, wait: Union[float, bool] = False, batch: int = 1, **kwargs: Any
777823
break
778824
else:
779825
_LOGGER.info(
780-
"Connection cannot read frames in this state: %r",
781-
self.state,
782-
extra=self._network_trace_params
826+
"[Connection:%s] Connection cannot read frames in this state: %r",
827+
self._network_trace_params["amqpConnection"],
828+
self.state
783829
)
784830
break
785831
except (OSError, IOError, SSLError, socket.error) as exc:
@@ -906,7 +952,11 @@ def close(self, error: Optional[AMQPError] = None, wait: bool = False) -> None:
906952
self._wait_for_response(wait, ConnectionState.END)
907953
except Exception as exc: # pylint:disable=broad-except
908954
# If error happened during closing, ignore the error and set state to END
909-
_LOGGER.info("An error occurred when closing the connection: %r", exc, extra=self._network_trace_params)
955+
_LOGGER.info(
956+
"[Connection:%s] An error occurred when closing the connection: %r",
957+
self._network_trace_params["amqpConnection"],
958+
exc
959+
)
910960
self._set_state(ConnectionState.END)
911961
finally:
912962
self._disconnect()

sdk/eventhub/azure-eventhub/azure/eventhub/_pyamqp/_transport.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,13 @@ def connect(self):
201201
# has _not_ been sent
202202
self.connected = True
203203
except (OSError, IOError, SSLError) as e:
204-
_LOGGER.info("Transport connection failed: %r", e, extra=self.network_trace_params)
204+
_LOGGER.info(
205+
"[Connection:%s, Session:%s, Link:%s] Transport connection failed: %r",
206+
self.network_trace_params["amqpConnection"],
207+
self.network_trace_params["amqpSession"],
208+
self.network_trace_params["amqpLink"],
209+
e
210+
)
205211
# if not fully connected, close socket, and reraise error
206212
if self.sock and not self.connected:
207213
self.sock.close()
@@ -396,9 +402,11 @@ def close(self):
396402
# TODO: shutdown could raise OSError, Transport endpoint is not connected if the endpoint is already
397403
# disconnected. can we safely ignore the errors since the close operation is initiated by us.
398404
_LOGGER.debug(
399-
"Transport endpoint is already disconnected: %r",
400-
exc,
401-
extra=self.network_trace_params
405+
"[Connection:%s, Session:%s, Link:%s] Transport endpoint is already disconnected: %r",
406+
self.network_trace_params["amqpConnection"],
407+
self.network_trace_params["amqpSession"],
408+
self.network_trace_params["amqpLink"],
409+
exc
402410
)
403411
self.sock.close()
404412
self.sock = None
@@ -421,10 +429,12 @@ def read(self, verify_frame_type=0):
421429
frame_type = frame_header[5]
422430
if verify_frame_type is not None and frame_type != verify_frame_type:
423431
_LOGGER.debug(
424-
"Received invalid frame type: %r, expected: %r",
432+
"[Connection:%s, Session:%s, Link:%s] Received invalid frame type: %r, expected: %r",
433+
self.network_trace_params["amqpConnection"],
434+
self.network_trace_params["amqpSession"],
435+
self.network_trace_params["amqpLink"],
425436
frame_type,
426-
verify_frame_type,
427-
extra=self.network_trace_params
437+
verify_frame_type
428438
)
429439
raise ValueError(
430440
f"Received invalid frame type: {frame_type}, expected: {verify_frame_type}"
@@ -453,7 +463,13 @@ def read(self, verify_frame_type=0):
453463
raise socket.timeout()
454464
if get_errno(exc) not in _UNAVAIL:
455465
self.connected = False
456-
_LOGGER.debug("Transport read failed: %r", exc, extra=self.network_trace_params)
466+
_LOGGER.debug(
467+
"[Connection:%s, Session:%s, Link:%s] Transport read failed: %r",
468+
self.network_trace_params["amqpConnection"],
469+
self.network_trace_params["amqpSession"],
470+
self.network_trace_params["amqpLink"],
471+
exc
472+
)
457473
raise
458474
offset -= 2
459475
return frame_header, channel, payload[offset:]
@@ -465,7 +481,13 @@ def write(self, s):
465481
except socket.timeout:
466482
raise
467483
except (OSError, IOError, socket.error) as exc:
468-
_LOGGER.debug("Transport write failed: %r", exc, extra=self.network_trace_params)
484+
_LOGGER.debug(
485+
"[Connection:%s, Session:%s, Link:%s] Transport write failed: %r",
486+
self.network_trace_params["amqpConnection"],
487+
self.network_trace_params["amqpSession"],
488+
self.network_trace_params["amqpLink"],
489+
exc
490+
)
469491
if get_errno(exc) not in _UNAVAIL:
470492
self.connected = False
471493
raise
@@ -753,7 +775,13 @@ def connect(self):
753775
self.close()
754776
raise ConnectionError("Websocket failed to establish connection: %r" % exc) from exc
755777
except (OSError, IOError, SSLError) as e:
756-
_LOGGER.info("Websocket connection failed: %r", e, extra=self.network_trace_params)
778+
_LOGGER.info(
779+
"[Connection:%s, Session:%s, Link:%s] Websocket connection failed: %r",
780+
self.network_trace_params["amqpConnection"],
781+
self.network_trace_params["amqpSession"],
782+
self.network_trace_params["amqpLink"],
783+
e
784+
)
757785
self.close()
758786
raise
759787

0 commit comments

Comments
 (0)