|
| 1 | +# stdlib |
| 2 | +import logging |
| 3 | +import sys |
| 4 | + |
| 5 | +# 3p |
| 6 | +import wrapt |
| 7 | +import pymemcache |
| 8 | +from pymemcache.client.base import Client |
| 9 | +from pymemcache.exceptions import ( |
| 10 | + MemcacheClientError, |
| 11 | + MemcacheServerError, |
| 12 | + MemcacheUnknownCommandError, |
| 13 | + MemcacheUnknownError, |
| 14 | + MemcacheIllegalInputError, |
| 15 | +) |
| 16 | + |
| 17 | +# project |
| 18 | +from ddtrace import Pin |
| 19 | +from ddtrace.compat import reraise |
| 20 | +from ddtrace.ext import net, memcached as memcachedx |
| 21 | + |
| 22 | +log = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +# keep a reference to the original unpatched clients |
| 26 | +_Client = Client |
| 27 | + |
| 28 | + |
| 29 | +class WrappedClient(wrapt.ObjectProxy): |
| 30 | + """Wrapper providing patched methods of a pymemcache Client. |
| 31 | +
|
| 32 | + Relevant connection information is obtained during initialization and |
| 33 | + attached to each span. |
| 34 | +
|
| 35 | + Keys are tagged in spans for methods that act upon a key. |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self, *args, **kwargs): |
| 39 | + c = _Client(*args, **kwargs) |
| 40 | + super(WrappedClient, self).__init__(c) |
| 41 | + |
| 42 | + # tags to apply to each span generated by this client |
| 43 | + tags = _get_address_tags(*args, **kwargs) |
| 44 | + |
| 45 | + parent_pin = Pin.get_from(pymemcache) |
| 46 | + |
| 47 | + if parent_pin: |
| 48 | + pin = parent_pin.clone(tags=tags) |
| 49 | + else: |
| 50 | + pin = Pin(tags=tags) |
| 51 | + |
| 52 | + # attach the pin onto this instance |
| 53 | + pin.onto(self) |
| 54 | + |
| 55 | + def set(self, *args, **kwargs): |
| 56 | + return self._traced_cmd("set", *args, **kwargs) |
| 57 | + |
| 58 | + def set_many(self, *args, **kwargs): |
| 59 | + return self._traced_cmd("set_many", *args, **kwargs) |
| 60 | + |
| 61 | + def add(self, *args, **kwargs): |
| 62 | + return self._traced_cmd("add", *args, **kwargs) |
| 63 | + |
| 64 | + def replace(self, *args, **kwargs): |
| 65 | + return self._traced_cmd("replace", *args, **kwargs) |
| 66 | + |
| 67 | + def append(self, *args, **kwargs): |
| 68 | + return self._traced_cmd("append", *args, **kwargs) |
| 69 | + |
| 70 | + def prepend(self, *args, **kwargs): |
| 71 | + return self._traced_cmd("prepend", *args, **kwargs) |
| 72 | + |
| 73 | + def cas(self, *args, **kwargs): |
| 74 | + return self._traced_cmd("cas", *args, **kwargs) |
| 75 | + |
| 76 | + def get(self, *args, **kwargs): |
| 77 | + return self._traced_cmd("get", *args, **kwargs) |
| 78 | + |
| 79 | + def get_many(self, *args, **kwargs): |
| 80 | + return self._traced_cmd("get_many", *args, **kwargs) |
| 81 | + |
| 82 | + def gets(self, *args, **kwargs): |
| 83 | + return self._traced_cmd("gets", *args, **kwargs) |
| 84 | + |
| 85 | + def gets_many(self, *args, **kwargs): |
| 86 | + return self._traced_cmd("gets_many", *args, **kwargs) |
| 87 | + |
| 88 | + def delete(self, *args, **kwargs): |
| 89 | + return self._traced_cmd("delete", *args, **kwargs) |
| 90 | + |
| 91 | + def delete_many(self, *args, **kwargs): |
| 92 | + return self._traced_cmd("delete_many", *args, **kwargs) |
| 93 | + |
| 94 | + def incr(self, *args, **kwargs): |
| 95 | + return self._traced_cmd("incr", *args, **kwargs) |
| 96 | + |
| 97 | + def decr(self, *args, **kwargs): |
| 98 | + return self._traced_cmd("decr", *args, **kwargs) |
| 99 | + |
| 100 | + def touch(self, *args, **kwargs): |
| 101 | + return self._traced_cmd("touch", *args, **kwargs) |
| 102 | + |
| 103 | + def stats(self, *args, **kwargs): |
| 104 | + return self._traced_cmd("stats", *args, **kwargs) |
| 105 | + |
| 106 | + def version(self, *args, **kwargs): |
| 107 | + return self._traced_cmd("version", *args, **kwargs) |
| 108 | + |
| 109 | + def flush_all(self, *args, **kwargs): |
| 110 | + return self._traced_cmd("flush_all", *args, **kwargs) |
| 111 | + |
| 112 | + def quit(self, *args, **kwargs): |
| 113 | + return self._traced_cmd("quit", *args, **kwargs) |
| 114 | + |
| 115 | + def set_multi(self, *args, **kwargs): |
| 116 | + """set_multi is an alias for set_many""" |
| 117 | + return self._traced_cmd("set_many", *args, **kwargs) |
| 118 | + |
| 119 | + def get_multi(self, *args, **kwargs): |
| 120 | + """set_multi is an alias for set_many""" |
| 121 | + return self._traced_cmd("get_many", *args, **kwargs) |
| 122 | + |
| 123 | + def _traced_cmd(self, method_name, *args, **kwargs): |
| 124 | + """Run and trace the given command. |
| 125 | +
|
| 126 | + Any pymemcache exception is caught and span error information is |
| 127 | + set. The exception is then reraised for the application to handle |
| 128 | + appropriately. |
| 129 | +
|
| 130 | + Relevant tags are set in the span. |
| 131 | + """ |
| 132 | + method = getattr(self.__wrapped__, method_name) |
| 133 | + p = Pin.get_from(self) |
| 134 | + |
| 135 | + # if the pin does not exist or is not enabled, shortcut |
| 136 | + if not p or not p.enabled(): |
| 137 | + return method(*args, **kwargs) |
| 138 | + |
| 139 | + with p.tracer.trace( |
| 140 | + memcachedx.CMD, |
| 141 | + service=p.service, |
| 142 | + resource=method_name, |
| 143 | + span_type=memcachedx.TYPE, |
| 144 | + ) as span: |
| 145 | + # try to set relevant tags, catch any exceptions so we don't mess |
| 146 | + # with the application |
| 147 | + try: |
| 148 | + span.set_tags(p.tags) |
| 149 | + vals = _get_query_string(args) |
| 150 | + query = "{}{}{}".format(method_name, " " if vals else "", vals) |
| 151 | + span.set_tag(memcachedx.QUERY, query) |
| 152 | + except Exception: |
| 153 | + log.debug("Error setting relevant pymemcache tags") |
| 154 | + |
| 155 | + try: |
| 156 | + return method(*args, **kwargs) |
| 157 | + except ( |
| 158 | + MemcacheClientError, |
| 159 | + MemcacheServerError, |
| 160 | + MemcacheUnknownCommandError, |
| 161 | + MemcacheUnknownError, |
| 162 | + MemcacheIllegalInputError, |
| 163 | + ): |
| 164 | + (typ, val, tb) = sys.exc_info() |
| 165 | + span.set_exc_info(typ, val, tb) |
| 166 | + reraise(typ, val, tb) |
| 167 | + |
| 168 | + |
| 169 | +def _get_address_tags(*args, **kwargs): |
| 170 | + """Attempt to get host and port from args passed to Client initializer.""" |
| 171 | + tags = {} |
| 172 | + try: |
| 173 | + if len(args): |
| 174 | + host, port = args[0] |
| 175 | + tags[net.TARGET_HOST] = host |
| 176 | + tags[net.TARGET_PORT] = port |
| 177 | + except Exception: |
| 178 | + log.debug("Error collecting client address tags") |
| 179 | + |
| 180 | + return tags |
| 181 | + |
| 182 | + |
| 183 | +def _get_query_string(args): |
| 184 | + """Return the query values given the arguments to a pymemcache command. |
| 185 | +
|
| 186 | + If there are multiple query values, they are joined together |
| 187 | + space-separated. |
| 188 | + """ |
| 189 | + keys = "" |
| 190 | + |
| 191 | + # shortcut if no args |
| 192 | + if not args: |
| 193 | + return keys |
| 194 | + |
| 195 | + # pull out the first arg which will contain any key |
| 196 | + arg = args[0] |
| 197 | + |
| 198 | + # if we get a dict, convert to list of keys |
| 199 | + if type(arg) is dict: |
| 200 | + arg = list(arg) |
| 201 | + |
| 202 | + if type(arg) is str: |
| 203 | + keys = arg |
| 204 | + elif type(arg) is bytes: |
| 205 | + keys = arg.decode() |
| 206 | + elif type(arg) is list and len(arg): |
| 207 | + if type(arg[0]) is str: |
| 208 | + keys = " ".join(arg) |
| 209 | + elif type(arg[0]) is bytes: |
| 210 | + keys = b" ".join(arg).decode() |
| 211 | + |
| 212 | + return keys |
0 commit comments