def __init__(self, start_time, event, context): """ Initialize. :param start_time: event's start time (epoch) :param event: event dict from the entry point :param context: the context dict from the entry point """ super(SNSLambdaTrigger, self).__init__(start_time) self.event_id = str(event['Records'][0]['Sns']['MessageId']) self.resource['name'] = \ event['Records'][0]['EventSubscriptionArn'].split(':')[-2] self.resource['operation'] = str(event['Records'][0]['Sns']['Type']) self.resource['metadata'] = { 'Notification Subject': str(event['Records'][0]['Sns']['Subject']) } message = str(event['Records'][0]['Sns']['Message']) add_data_if_needed( self.resource['metadata'], 'Notification Message', message ) print_debug('Initialized SNS Lambda trigger')
def before_request(cls, wrapped, instance, args, kwargs): """ Runs when new request comes in. :param wrapped: wrapt's wrapped :param instance: wrapt's instance :param args: wrapt's args :param kwargs: wrapt's kwargs """ print_debug('before_request Tornado request') try: ignored = ignore_request('', instance.request.path) if not ignored and not is_ignored_endpoint(instance.request.path): unique_id = str(uuid.uuid4()) trace = epsagon.trace.trace_factory.get_or_create_trace( unique_id=unique_id) trace.prepare() setattr(instance, TORNADO_TRACE_ID, unique_id) cls.RUNNERS[unique_id] = (TornadoRunner( time.time(), instance.request)) trace.set_runner(cls.RUNNERS[unique_id]) print_debug('Created Tornado Runner') # Collect metadata in case this is a container. collect_container_metadata( cls.RUNNERS[unique_id].resource['metadata']) except Exception as instrumentation_exception: # pylint: disable=W0703 epsagon.trace.trace_factory.add_exception( instrumentation_exception, traceback.format_exc()) return wrapped(*args, **kwargs)
def after_request(cls, wrapped, instance, args, kwargs): """ Runs after first process of response. :param wrapped: wrapt's wrapped :param instance: wrapt's instance :param args: wrapt's args :param kwargs: wrapt's kwargs """ print_debug('after_request Tornado request') response_body = None try: response_body = getattr(instance, '_write_buffer', None) if response_body and isinstance(response_body, list): response_body = b''.join(response_body) except Exception as instrumentation_exception: # pylint: disable=W0703 epsagon.trace.trace_factory.add_exception( instrumentation_exception, traceback.format_exc()) res = wrapped(*args, **kwargs) trace = None is_trace_sent = False try: unique_id = getattr(instance, TORNADO_TRACE_ID, None) if not unique_id: return res tornado_runner = cls.RUNNERS.pop(unique_id) trace = epsagon.trace.trace_factory.switch_active_trace(unique_id) # Ignoring 404s if getattr(instance, '_status_code', None) == 404: epsagon.trace.trace_factory.pop_trace(trace=trace) print_debug('Ignoring 404 Tornado request') return res if trace: content = ( instance._headers.get( # pylint: disable=protected-access 'Content-Type', '')) ignored = ignore_request(content, '') if not ignored: tornado_runner.update_response(instance, response_body) epsagon.trace.trace_factory.send_traces(trace) is_trace_sent = True except Exception: # pylint: disable=W0703 if not is_trace_sent and trace: epsagon.trace.trace_factory.pop_trace(trace=trace) return res
def collect_exception(cls, wrapped, instance, args, kwargs): """ Runs after first process of response. :param wrapped: wrapt's wrapped :param _: wrapt's instance :param args: wrapt's args :param kwargs: wrapt's kwargs """ print_debug('collect_exception Tornado request') try: unique_id = getattr(instance, TORNADO_TRACE_ID, None) if unique_id and cls.RUNNERS.get(unique_id): _, exception, _ = args cls.RUNNERS[unique_id].set_exception(exception, traceback.format_exc()) except Exception as instrumentation_exception: # pylint: disable=W0703 epsagon.trace.trace_factory.add_exception( instrumentation_exception, traceback.format_exc()) return wrapped(*args, **kwargs)
def _wrapper(wrapped, instance, args, kwargs): """ General wrapper for AsyncHTTPClient instrumentation. :param wrapped: wrapt's wrapped :param instance: wrapt's instance :param args: wrapt's args :param kwargs: wrapt's kwargs :return: None """ print_debug('AsyncHTTPClient init') try: request, raise_error = _prepare_http_request(*args, **kwargs) except Exception: # pylint: disable=W0703 return wrapped(*args, **kwargs) print_debug('AsyncHTTPClient setting header') trace_header = get_epsagon_http_trace_id() if isinstance(request.headers, HTTPHeaders): if not request.headers.get(EPSAGON_HEADER): request.headers.add(EPSAGON_HEADER, trace_header) elif isinstance(request.headers, dict): if EPSAGON_HEADER not in request.headers: request.headers[EPSAGON_HEADER] = trace_header print_debug('AsyncHTTPClient running wrapper') return wrapper( TornadoClientEventFactory, wrapped, instance, (request, ), # new args {'raise_error': raise_error} # new kwargs )