|
| 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) |
0 commit comments