From 1bf7af103f3d047fcd1de66ddd6d490aec2e382f Mon Sep 17 00:00:00 2001 From: Jedore Date: Thu, 29 Dec 2022 14:08:24 +0800 Subject: [PATCH 1/5] Enhance redis plugin to adapt Virtual Cache --- CHANGELOG.md | 1 + skywalking/plugins/sw_redis.py | 117 +++++++++++++++++++++++++++++++-- skywalking/trace/tags.py | 16 +++++ 3 files changed, 128 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 483b2b3c..8997774c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Add reporter for PVM runtime metrics (default:disabled) (#238, #247) - Add Greenlet profiler (#246) - Add test and support for Python Slim base images (#249) + - Add support for the tags of Virtual Cache for Redis (#10212) - Plugins: - Add aioredis, aiormq, amqp, asyncpg, aio-pika, kombu RMQ plugins (#230 Missing test coverage) diff --git a/skywalking/plugins/sw_redis.py b/skywalking/plugins/sw_redis.py index 620e374d..36ea03c6 100644 --- a/skywalking/plugins/sw_redis.py +++ b/skywalking/plugins/sw_redis.py @@ -17,7 +17,7 @@ from skywalking import Layer, Component from skywalking.trace.context import get_context -from skywalking.trace.tags import TagDbType, TagDbInstance, TagDbStatement +from skywalking.trace.tags import TagCacheType, TagCacheOp, TagCacheCmd, TagCacheKey link_vector = ['https://github.com/andymccurdy/redis-py/'] support_matrix = { @@ -35,16 +35,121 @@ def install(): def _sw_send_command(this: Connection, *args, **kwargs): peer = f'{this.host}:{this.port}' - op = args[0] + cmd, key = args[0], args[1] + + 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/{op}' or '/', peer=peer, component=Component.Redis) as span: + 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(TagDbType('Redis')) - span.tag(TagDbInstance(this.db)) - span.tag(TagDbStatement(op)) + span.tag(TagCacheType('Redis')) + span.tag(TagCacheKey(key)) + span.tag(TagCacheCmd(cmd)) + span.tag(TagCacheOp(op)) return res Connection.send_command = _sw_send_command + + +OPERATIONS_WRITE = set({'GETSET', + 'SET', + 'SETBIT', + 'SETEX ', + 'SETNX ', + 'SETRANGE', + 'STRLEN ', + 'MSET', + 'MSETNX ', + 'PSETEX', + 'INCR ', + 'INCRBY ', + 'INCRBYFLOAT', + 'DECR ', + 'DECRBY ', + 'APPEND ', + 'HMSET', + 'HSET', + 'HSETNX ', + 'HINCRBY', + 'HINCRBYFLOAT', + 'HDEL', + 'RPOPLPUSH', + 'RPUSH', + 'RPUSHX', + 'LPUSH', + 'LPUSHX', + 'LREM', + 'LTRIM', + 'LSET', + 'BRPOPLPUSH', + 'LINSERT', + 'SADD', + 'SDIFF', + 'SDIFFSTORE', + 'SINTERSTORE', + 'SISMEMBER', + 'SREM', + 'SUNION', + 'SUNIONSTORE', + 'SINTER', + 'ZADD', + 'ZINCRBY', + 'ZINTERSTORE', + 'ZRANGE', + 'ZRANGEBYLEX', + 'ZRANGEBYSCORE', + 'ZRANK', + 'ZREM', + 'ZREMRANGEBYLEX', + 'ZREMRANGEBYRANK', + 'ZREMRANGEBYSCORE', + 'ZREVRANGE', + 'ZREVRANGEBYSCORE', + 'ZREVRANK', + 'ZUNIONSTORE', + 'XADD', + 'XDEL', + 'DEL', + 'xtrim'}) + +OPERATIONS_READ = set({'GETRANGE', + 'GETBIT ', + 'MGET', + 'HVALS', + 'HKEYS', + 'HLEN', + 'HEXISTS', + 'HGET', + 'HGETALL', + 'HMGET', + 'BLPOP', + 'BRPOP', + 'LINDEX', + 'LLEN', + 'LPOP', + 'LRANGE', + 'RPOP', + 'SCARD', + 'SRANDMEMBER', + 'SPOP', + 'SSCAN', + 'SMOVE', + 'ZLEXCOUNT', + 'ZSCORE', + 'ZSCAN', + 'ZCARD', + 'ZCOUNT', + 'XGET', + 'GET', + 'XREAD', + 'XLEN', + 'XRANGE', + 'XREVRANGE'}) diff --git a/skywalking/trace/tags.py b/skywalking/trace/tags.py index f1b61820..a1288c40 100644 --- a/skywalking/trace/tags.py +++ b/skywalking/trace/tags.py @@ -63,6 +63,22 @@ class TagDbSqlParameters(Tag): overridable = False +class TagCacheType(Tag): + key = 'cache.type' + + +class TagCacheOp(Tag): + key = 'cache.op' + + +class TagCacheCmd(Tag): + key = 'cache.cmd' + + +class TagCacheKey(Tag): + key = 'cache.key' + + class TagMqBroker(Tag): key = 'mq.broker' From 7d985d115bbbea827499e08eaaffb03269e257b0 Mon Sep 17 00:00:00 2001 From: Jedore Date: Fri, 30 Dec 2022 13:19:51 +0800 Subject: [PATCH 2/5] Complement test expected.data.yml for redis enhancement --- tests/plugin/data/sw_redis/expected.data.yml | 24 ++++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/plugin/data/sw_redis/expected.data.yml b/tests/plugin/data/sw_redis/expected.data.yml index d6902002..db09586f 100644 --- a/tests/plugin/data/sw_redis/expected.data.yml +++ b/tests/plugin/data/sw_redis/expected.data.yml @@ -26,12 +26,14 @@ segmentItems: spanId: 1 spanLayer: Cache tags: - - key: db.type + - key: cache.type value: Redis - - key: db.instance - value: '0' - - key: db.statement - value: 'SET' + - key: cache.key + value: foo + - key: cache.cmd + value: SET + - key: cache.op + value: write startTime: gt 0 endTime: gt 0 componentId: 7 @@ -43,12 +45,14 @@ segmentItems: spanId: 2 spanLayer: Cache tags: - - key: db.type + - key: cache.type value: Redis - - key: db.instance - value: '0' - - key: db.statement - value: 'GET' + - key: cache.key + value: foo + - key: cache.cmd + value: GET + - key: cache.op + value: read startTime: gt 0 endTime: gt 0 componentId: 7 From fd991ac2b2eabfe271d58543869c2b28d5bdc13d Mon Sep 17 00:00:00 2001 From: Jedore Date: Sun, 1 Jan 2023 21:40:33 +0800 Subject: [PATCH 3/5] Update CHANGELOG.md Co-authored-by: Superskyyy (COVID) --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8997774c..6a636a65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ - Add reporter for PVM runtime metrics (default:disabled) (#238, #247) - Add Greenlet profiler (#246) - Add test and support for Python Slim base images (#249) - - Add support for the tags of Virtual Cache for Redis (#10212) + - Add support for the tags of Virtual Cache for Redis (#263) - Plugins: - Add aioredis, aiormq, amqp, asyncpg, aio-pika, kombu RMQ plugins (#230 Missing test coverage) From 6504e262f83a2b860c4c50da92a7f6c46c8c2944 Mon Sep 17 00:00:00 2001 From: Jedore Date: Sun, 1 Jan 2023 21:46:34 +0800 Subject: [PATCH 4/5] Move the globals location --- skywalking/plugins/sw_redis.py | 63 +++++++++++++++++----------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/skywalking/plugins/sw_redis.py b/skywalking/plugins/sw_redis.py index 36ea03c6..cd5f7ed7 100644 --- a/skywalking/plugins/sw_redis.py +++ b/skywalking/plugins/sw_redis.py @@ -27,38 +27,6 @@ } note = """""" - -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}' - cmd, key = args[0], args[1] - - 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 - - OPERATIONS_WRITE = set({'GETSET', 'SET', 'SETBIT', @@ -153,3 +121,34 @@ def _sw_send_command(this: Connection, *args, **kwargs): 'XLEN', 'XRANGE', 'XREVRANGE'}) + + +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}' + cmd, key = args[0], args[1] + + 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 From 85a029f77031d9a1927e56f678fc30f719bbc649 Mon Sep 17 00:00:00 2001 From: Jedore Date: Mon, 2 Jan 2023 02:05:13 +0800 Subject: [PATCH 5/5] Update sw_redis.py --- skywalking/plugins/sw_redis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skywalking/plugins/sw_redis.py b/skywalking/plugins/sw_redis.py index cd5f7ed7..0dc95dda 100644 --- a/skywalking/plugins/sw_redis.py +++ b/skywalking/plugins/sw_redis.py @@ -86,7 +86,7 @@ 'XADD', 'XDEL', 'DEL', - 'xtrim'}) + 'XTRIM'}) OPERATIONS_READ = set({'GETRANGE', 'GETBIT ',