Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance redis plugin to adapt Virtual Cache #263

Merged
merged 5 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
116 changes: 110 additions & 6 deletions skywalking/plugins/sw_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -27,6 +27,101 @@
}
note = """"""

OPERATIONS_WRITE = set({'GETSET',
Jedore marked this conversation as resolved.
Show resolved Hide resolved
'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
Expand All @@ -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

Expand Down
16 changes: 16 additions & 0 deletions skywalking/trace/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
24 changes: 14 additions & 10 deletions tests/plugin/data/sw_redis/expected.data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down