Repository: redis/redis-py
File: redis/event.py (Python)

Code before:
    """
    def listen(self, event: AfterConnectionHandoffEvent):
        record_connection_handoff(
            pool_name=repr(event.connection_pool),
        )

class ExportPubSubMessageMetric(EventListenerInterface):
    """
    Listener that exports pubsub message metric.
    """
    def listen(self, event: OnPubSubMessageEvent):
        record_pubsub_message(
            direction=event.direction,
            channel=event.channel,
            sharded=event.sharded,
        )

class ExportStreamingLagMetric(EventListenerInterface):
    """
    Listener that exports streaming lag metric per stream message.
    """
    def listen(self, event: OnStreamMessageReceivedEvent):
        now = datetime.now().timestamp()

        # RESP3
        if type(event.response) == dict:
            for stream_name, stream_messages in event.response.items():
                for messages in stream_messages:
                    for message in messages:
                        message_id, message = message
                        message_id = str_if_bytes(message_id)
                        timestamp, _ = message_id.split("-")
                        lag_seconds = now - int(timestamp) / 1000

                        record_streaming_lag(
                            lag_seconds=lag_seconds,
                            stream_name=str_if_bytes(stream_name),
                            consumer_group=event.consumer_group,
                            consumer_name=event.consumer_name,
                        )
        else:
            # RESP 2
            for stream_entry in event.response:
                stream_name = str_if_bytes(stream_entry[0])

                for message in stream_entry[1]:
                    message_id, message = message
                    message_id = str_if_bytes(message_id)
                    timestamp, _ = message_id.split("-")
                    lag_seconds = now - int(timestamp) / 1000


Review comment (suggestion):
Using type() == dict for type checking is not recommended. Use isinstance(event.response, dict) instead. The isinstance() function is more Pythonic and handles inheritance correctly, whereas type() checks for exact type match only.
```suggestion
        if isinstance(event.response, dict):
```

Code after:
    """
    def listen(self, event: AfterConnectionHandoffEvent):
        record_connection_handoff(
            pool_name=repr(event.connection_pool),
        )

class ExportPubSubMessageMetric(EventListenerInterface):
    """
    Listener that exports pubsub message metric.
    """
    def listen(self, event: OnPubSubMessageEvent):
        record_pubsub_message(
            direction=event.direction,
            channel=event.channel,
            sharded=event.sharded,
        )

class ExportStreamingLagMetric(EventListenerInterface):
    """
    Listener that exports streaming lag metric per stream message.
    """
    def listen(self, event: OnStreamMessageReceivedEvent):
        now = datetime.now().timestamp()

        if not event.response:
            return

        # RESP3
        if isinstance(event.response, dict):
            for stream_name, stream_messages in event.response.items():
                for messages in stream_messages:
                    for message in messages:
                        message_id, message = message
                        message_id = str_if_bytes(message_id)
                        timestamp, _ = message_id.split("-")
                        lag_seconds = now - int(timestamp) / 1000

                        record_streaming_lag(
                            lag_seconds=lag_seconds,
                            stream_name=str_if_bytes(stream_name),
                            consumer_group=event.consumer_group,
                            consumer_name=event.consumer_name,
                        )
        else:
            # RESP 2
            for stream_entry in event.response:
                stream_name = str_if_bytes(stream_entry[0])

                for message in stream_entry[1]:
                    message_id, message = message
                    message_id = str_if_bytes(message_id)