Debian Codegraph @

aws_xray_sdk/ext/dbapi2.py

raw
1 import copy
2 import wrapt
3
4 from aws_xray_sdk.core import xray_recorder
5
6
7 class XRayTracedConn(wrapt.ObjectProxy):
8
9 _xray_meta = None
10
11 def __init__(self, conn, meta={}):
12
13 super(XRayTracedConn, self).__init__(conn)
14 self._xray_meta = meta
15
16 def cursor(self, *args, **kwargs):
17
18 cursor = self.__wrapped__.cursor(*args, **kwargs)
19 return XRayTracedCursor(cursor, self._xray_meta)
20
21
22 class XRayTracedCursor(wrapt.ObjectProxy):
23
24 _xray_meta = None
25
26 def __init__(self, cursor, meta={}):
27
28 super(XRayTracedCursor, self).__init__(cursor)
29 self._xray_meta = meta
30
31 # we preset database type if db is framework built-in
32 if not self._xray_meta.get('database_type'):
33 db_type = cursor.__class__.__module__.split('.')[0]
34 self._xray_meta['database_type'] = db_type
35
36 @xray_recorder.capture()
37 def execute(self, query, *args, **kwargs):
38
39 add_sql_meta(self._xray_meta)
40 return self.__wrapped__.execute(query, *args, **kwargs)
41
42 @xray_recorder.capture()
43 def executemany(self, query, *args, **kwargs):
44
45 add_sql_meta(self._xray_meta)
46 return self.__wrapped__.executemany(query, *args, **kwargs)
47
48 @xray_recorder.capture()
49 def callproc(self, proc, args):
50
51 add_sql_meta(self._xray_meta)
52 return self.__wrapped__.callproc(proc, args)
53
54
55 def add_sql_meta(meta):
56
57 subsegment = xray_recorder.current_subsegment()
58
59 if not subsegment:
60 return
61
62 if meta.get('name', None):
63 subsegment.name = meta['name']
64
65 sql_meta = copy.copy(meta)
66 if sql_meta.get('name', None):
67 del sql_meta['name']
68 subsegment.set_sql(sql_meta)
69 subsegment.namespace = 'remote'