|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import re |
16 | 15 |
|
17 | 16 | from newrelic.api.datastore_trace import DatastoreTrace |
18 | 17 | from newrelic.api.transaction import current_transaction |
19 | 18 | from newrelic.common.object_wrapper import wrap_function_wrapper |
| 19 | +from newrelic.hooks.datastore_redis import _conn_attrs_to_dict, _instance_info, _redis_client_methods, _redis_multipart_commands, _redis_operation_re |
20 | 20 |
|
21 | | -_redis_client_methods = ( |
22 | | - "bgrewriteaof", |
23 | | - "bgsave", |
24 | | - "client_kill", |
25 | | - "client_list", |
26 | | - "client_getname", |
27 | | - "client_setname", |
28 | | - "config_get", |
29 | | - "config_set", |
30 | | - "config_resetstat", |
31 | | - "config_rewrite", |
32 | | - "dbsize", |
33 | | - "debug_object", |
34 | | - "echo", |
35 | | - "flushall", |
36 | | - "flushdb", |
37 | | - "info", |
38 | | - "lastsave", |
39 | | - "object", |
40 | | - "ping", |
41 | | - "save", |
42 | | - "sentinel", |
43 | | - "sentinel_get_master_addr_by_name", |
44 | | - "sentinel_master", |
45 | | - "sentinel_masters", |
46 | | - "sentinel_monitor", |
47 | | - "sentinel_remove", |
48 | | - "sentinel_sentinels", |
49 | | - "sentinel_set", |
50 | | - "sentinel_slaves", |
51 | | - "shutdown", |
52 | | - "slaveof", |
53 | | - "slowlog_get", |
54 | | - "slowlog_reset", |
55 | | - "time", |
56 | | - "append", |
57 | | - "bitcount", |
58 | | - "bitop", |
59 | | - "bitpos", |
60 | | - "decr", |
61 | | - "delete", |
62 | | - "dump", |
63 | | - "exists", |
64 | | - "expire", |
65 | | - "expireat", |
66 | | - "get", |
67 | | - "getbit", |
68 | | - "getrange", |
69 | | - "getset", |
70 | | - "incr", |
71 | | - "incrby", |
72 | | - "incrbyfloat", |
73 | | - "keys", |
74 | | - "mget", |
75 | | - "mset", |
76 | | - "msetnx", |
77 | | - "move", |
78 | | - "persist", |
79 | | - "pexpire", |
80 | | - "pexpireat", |
81 | | - "psetex", |
82 | | - "pttl", |
83 | | - "randomkey", |
84 | | - "rename", |
85 | | - "renamenx", |
86 | | - "restore", |
87 | | - "set", |
88 | | - "setbit", |
89 | | - "setex", |
90 | | - "setnx", |
91 | | - "setrange", |
92 | | - "strlen", |
93 | | - "substr", |
94 | | - "ttl", |
95 | | - "type", |
96 | | - "watch", |
97 | | - "unwatch", |
98 | | - "blpop", |
99 | | - "brpop", |
100 | | - "brpoplpush", |
101 | | - "lindex", |
102 | | - "linsert", |
103 | | - "llen", |
104 | | - "lpop", |
105 | | - "lpush", |
106 | | - "lpushx", |
107 | | - "lrange", |
108 | | - "lrem", |
109 | | - "lset", |
110 | | - "ltrim", |
111 | | - "rpop", |
112 | | - "rpoplpush", |
113 | | - "rpush", |
114 | | - "rpushx", |
115 | | - "sort", |
116 | | - "scan", |
117 | | - "scan_iter", |
118 | | - "sscan", |
119 | | - "sscan_iter", |
120 | | - "hscan", |
121 | | - "hscan_inter", |
122 | | - "zscan", |
123 | | - "zscan_iter", |
124 | | - "sadd", |
125 | | - "scard", |
126 | | - "sdiff", |
127 | | - "sdiffstore", |
128 | | - "sinter", |
129 | | - "sinterstore", |
130 | | - "sismember", |
131 | | - "smembers", |
132 | | - "smove", |
133 | | - "spop", |
134 | | - "srandmember", |
135 | | - "srem", |
136 | | - "sunion", |
137 | | - "sunionstore", |
138 | | - "zadd", |
139 | | - "zcard", |
140 | | - "zcount", |
141 | | - "zincrby", |
142 | | - "zinterstore", |
143 | | - "zlexcount", |
144 | | - "zrange", |
145 | | - "zrangebylex", |
146 | | - "zrangebyscore", |
147 | | - "zrank", |
148 | | - "zrem", |
149 | | - "zremrangebylex", |
150 | | - "zremrangebyrank", |
151 | | - "zremrangebyscore", |
152 | | - "zrevrange", |
153 | | - "zrevrangebyscore", |
154 | | - "zrevrank", |
155 | | - "zscore", |
156 | | - "zunionstore", |
157 | | - "pfadd", |
158 | | - "pfcount", |
159 | | - "pfmerge", |
160 | | - "hdel", |
161 | | - "hexists", |
162 | | - "hget", |
163 | | - "hgetall", |
164 | | - "hincrby", |
165 | | - "hincrbyfloat", |
166 | | - "hkeys", |
167 | | - "hlen", |
168 | | - "hset", |
169 | | - "hsetnx", |
170 | | - "hmset", |
171 | | - "hmget", |
172 | | - "hvals", |
173 | | - "publish", |
174 | | - "eval", |
175 | | - "evalsha", |
176 | | - "script_exists", |
177 | | - "script_flush", |
178 | | - "script_kill", |
179 | | - "script_load", |
180 | | - "setex", |
181 | | - "lrem", |
182 | | - "zadd", |
183 | | -) |
184 | | - |
185 | | -_redis_multipart_commands = set(["client", "cluster", "command", "config", "debug", "sentinel", "slowlog", "script"]) |
186 | | - |
187 | | -_redis_operation_re = re.compile(r"[-\s]+") |
188 | | - |
189 | | - |
190 | | -def _conn_attrs_to_dict(connection): |
191 | | - return { |
192 | | - "host": getattr(connection, "host", None), |
193 | | - "port": getattr(connection, "port", None), |
194 | | - "path": getattr(connection, "path", None), |
195 | | - "db": getattr(connection, "db", None), |
196 | | - } |
197 | | - |
198 | | - |
199 | | -def _instance_info(kwargs): |
200 | | - host = kwargs.get("host") or "localhost" |
201 | | - port_path_or_id = str(kwargs.get("port") or kwargs.get("path", "unknown")) |
202 | | - db = str(kwargs.get("db") or 0) |
203 | | - |
204 | | - return (host, port_path_or_id, db) |
| 21 | + |
| 22 | +def _wrap_Aredis_method_wrapper_(module, instance_class_name, operation): |
| 23 | + async def _nr_wrapper_Aredis_method_(wrapped, instance, args, kwargs): |
| 24 | + transaction = current_transaction() |
| 25 | + if transaction is None: |
| 26 | + return await wrapped(*args, **kwargs) |
| 27 | + |
| 28 | + dt = DatastoreTrace(product="Redis", target=None, operation=operation) |
| 29 | + |
| 30 | + transaction._nr_datastore_instance_info = (None, None, None) |
| 31 | + |
| 32 | + with dt: |
| 33 | + result = await wrapped(*args, **kwargs) |
| 34 | + |
| 35 | + host, port_path_or_id, db = transaction._nr_datastore_instance_info |
| 36 | + dt.host = host |
| 37 | + dt.port_path_or_id = port_path_or_id |
| 38 | + dt.database_name = db |
| 39 | + return result |
| 40 | + |
| 41 | + name = "%s.%s" % (instance_class_name, operation) |
| 42 | + wrap_function_wrapper(module, name, _nr_wrapper_Aredis_method_) |
| 43 | + |
| 44 | + |
| 45 | +def instrument_aredis_client(module): |
| 46 | + if hasattr(module, "StrictRedis"): |
| 47 | + for name in _redis_client_methods: |
| 48 | + if hasattr(module.StrictRedis, name): |
| 49 | + _wrap_Aredis_method_wrapper_(module, "StrictRedis", name) |
205 | 50 |
|
206 | 51 |
|
207 | 52 | async def _nr_Connection_send_command_wrapper_(wrapped, instance, args, kwargs): |
|
0 commit comments