Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit f103f3a

Browse files
authored
feat(pyqldb): add support for qldb driver cs 273 (#417)
1 parent 5fb0295 commit f103f3a

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

epsagon/events/pyqldb.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
pyqldb events module.
3+
"""
4+
5+
from __future__ import absolute_import
6+
from uuid import uuid4
7+
import traceback
8+
9+
from epsagon.utils import add_data_if_needed
10+
from ..event import BaseEvent
11+
from ..trace import trace_factory
12+
13+
14+
class QldbEvent(BaseEvent):
15+
"""
16+
Represents base pyqldb event.
17+
"""
18+
19+
ORIGIN = 'qldb'
20+
RESOURCE_TYPE = 'qldb'
21+
22+
#pylint: disable=W0613
23+
def __init__(self, wrapped, instance, args, kwargs, start_time, response,
24+
exception):
25+
"""
26+
Initialize.
27+
:param wrapped: wrapt's wrapped
28+
:param instance: wrapt's instance
29+
:param args: wrapt's args
30+
:param kwargs: wrapt's kwargs
31+
:param start_time: Start timestamp (epoch)
32+
:param response: response data
33+
:param exception: Exception (if happened)
34+
"""
35+
super(QldbEvent, self).__init__(start_time)
36+
37+
self.event_id = 'qldb-{}'.format(str(uuid4()))
38+
self.resource['name'] = \
39+
getattr(instance.__getattribute__('_transaction')._session,# pylint: disable=W0212
40+
'_ledger_name')
41+
self.resource['operation'] = wrapped.__func__.__name__
42+
43+
self.resource['metadata']['query'] = args[0]
44+
add_data_if_needed(self.resource['metadata'], 'parameters',
45+
[args[i] for i in range(1, len(args))])
46+
47+
add_data_if_needed(self.resource['metadata'], 'transaction_id',
48+
getattr(instance, 'transaction_id'))
49+
50+
if response is not None:
51+
self.update_response(response)
52+
53+
if exception is not None:
54+
self.set_exception(exception, traceback.format_exc())
55+
56+
57+
def update_response(self, response):
58+
"""
59+
Adds response data to event.
60+
:param response: Response from botocore
61+
:return: None
62+
"""
63+
64+
self.resource['metadata']['Results'] = [str(x) for x in response]
65+
self.resource['metadata']['response.consumed_information'] = \
66+
response.get_consumed_ios()
67+
self.resource['metadata']['response.timing_information'] = \
68+
response.get_timing_information()
69+
70+
71+
72+
class QldbEventFactory(object):
73+
"""
74+
Factory class, generates Qldb event.
75+
"""
76+
77+
@staticmethod
78+
def create_event(wrapped, instance, args, kwargs, start_time, response,
79+
exception):
80+
"""
81+
Create a Qldb event.
82+
:param wrapped:
83+
:param instance:
84+
:param args:
85+
:param kwargs:
86+
:param start_time:
87+
:param response:
88+
:param exception:
89+
:return:
90+
"""
91+
event = QldbEvent(
92+
wrapped,
93+
instance,
94+
args,
95+
kwargs,
96+
start_time,
97+
response,
98+
exception
99+
)
100+
trace_factory.add_event(event)

epsagon/modules/pyqldb.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
pyqldb patcher module
3+
"""
4+
from __future__ import absolute_import
5+
import wrapt
6+
from epsagon.modules.general_wrapper import wrapper
7+
from ..events.pyqldb import QldbEventFactory
8+
9+
10+
def _wrapper(wrapped, instance, args, kwargs):
11+
"""
12+
General wrapper for Pyqldb instrumentation.
13+
:param wrapped: wrapt's wrapped
14+
:param instance: wrapt's instance
15+
:param args: wrapt's args
16+
:param kwargs: wrapt's kwargs
17+
:return: None
18+
"""
19+
return wrapper(QldbEventFactory, wrapped, instance, args, kwargs)
20+
21+
22+
def patch():
23+
"""
24+
patch module.
25+
:return: None
26+
"""
27+
wrapt.wrap_function_wrapper(
28+
'pyqldb.execution.executor',
29+
'Executor.execute_statement',
30+
_wrapper
31+
)

scripts/run_acceptance_tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
if [ -z $AWS_ACCESS_KEY_ID ] || [ -z $AWS_SECRET_ACCESS_KEY ]; then
33
echo "AWS credentials must be set in order to run acceptance tests"
44
exit 1
5-
elif [ $TRAVIS_PYTHON_VERSION != "2.7" ]; then
5+
elif [ $TRAVIS_PYTHON_VERSION != "2.7" ] && [ $TRAVIS_PYTHON_VERSION != "3.6" ]; then
66
npm install && export PATH=$(pwd)/node_modules/.bin:$PATH
77
./acceptance/run.sh $TRAVIS_BUILD_NUMBER $TRAVIS_PYTHON_VERSION
88
fi

0 commit comments

Comments
 (0)