def install()

in skywalking/plugins/sw_redis.py [0:0]


def install():
    from redis.connection import Connection

    _send_command = Connection.send_command

    def _sw_send_command(this: Connection, *args, **kwargs):
        peer = f'{this.host}:{this.port}'

        if len(args) == 1:
            cmd = args[0]
            key = ''
        elif len(args) > 1:
            cmd, key = args[0], args[1]
        else:  # just to be safe
            cmd = key = ''

        if cmd in OPERATIONS_WRITE:
            op = 'write'
        elif cmd in OPERATIONS_READ:
            op = 'read'
        else:
            op = ''

        context = get_context()
        with context.new_exit_span(op=f'Redis/{cmd}' or '/', peer=peer, component=Component.Redis) as span:
            span.layer = Layer.Cache

            res = _send_command(this, *args, **kwargs)
            span.tag(TagCacheType('Redis'))
            span.tag(TagCacheKey(key))
            span.tag(TagCacheCmd(cmd))
            span.tag(TagCacheOp(op))

            return res

    Connection.send_command = _sw_send_command