diff --git a/CHANGELOG.md b/CHANGELOG.md index 483b2b3c..6a636a65 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 (#263) - 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..0dc95dda 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 = { @@ -27,6 +27,101 @@ } note = """""" +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'}) + def install(): from redis.connection import Connection @@ -35,15 +130,24 @@ 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 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' 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