def call(self, module, method, wrapped, instance, args, kwargs): # Late import to avoid ImportErrors from tornado.web import Finish, HTTPError from elasticapm.contrib.tornado.utils import get_data_from_request e = args[0] if isinstance(e, Finish): # Not an error; Finish is an exception that ends a request without an error response return wrapped(*args, **kwargs) client = instance.application.elasticapm_client request = instance.request client.capture_exception( context={ "request": get_data_from_request(instance, request, client.config, constants.ERROR) }) if isinstance(e, HTTPError): elasticapm.set_transaction_result("HTTP {}xx".format( int(e.status_code / 100)), override=False) elasticapm.set_context({"status_code": e.status_code}, "response") else: elasticapm.set_transaction_result("HTTP 5xx", override=False) elasticapm.set_context({"status_code": 500}, "response") return wrapped(*args, **kwargs)
async def call(self, module, method, wrapped, instance, args, kwargs): # Late import to avoid ImportErrors from elasticapm.contrib.tornado.utils import get_data_from_request, get_data_from_response request = instance.request trace_parent = TraceParent.from_headers(request.headers) client = instance.application.elasticapm_client client.begin_transaction("request", trace_parent=trace_parent) elasticapm.set_context( lambda: get_data_from_request(instance, request, client.config, constants.TRANSACTION), "request") # TODO: Can we somehow incorporate the routing rule itself here? elasticapm.set_transaction_name("{} {}".format( request.method, type(instance).__name__), override=False) ret = await wrapped(*args, **kwargs) elasticapm.set_context( lambda: get_data_from_response(instance, client.config, constants. TRANSACTION), "response") result = "HTTP {}xx".format(instance.get_status() // 100) elasticapm.set_transaction_result(result, override=False) client.end_transaction() return ret
def call(self, module, method, wrapped, instance, args, kwargs): if not hasattr(instance.application, "elasticapm_client"): # If tornado was instrumented but not as the main framework # (i.e. in Flower), we should skip it. return wrapped(*args, **kwargs) # Late import to avoid ImportErrors from tornado.web import Finish, HTTPError from elasticapm.contrib.tornado.utils import get_data_from_request e = args[0] if isinstance(e, Finish): # Not an error; Finish is an exception that ends a request without an error response return wrapped(*args, **kwargs) client = instance.application.elasticapm_client request = instance.request client.capture_exception( context={ "request": get_data_from_request(instance, request, client.config, constants.ERROR) }) elasticapm.set_transaction_outcome(constants.OUTCOME.FAILURE) if isinstance(e, HTTPError): elasticapm.set_transaction_result("HTTP {}xx".format( int(e.status_code / 100)), override=False) elasticapm.set_context({"status_code": e.status_code}, "response") else: elasticapm.set_transaction_result("HTTP 5xx", override=False) elasticapm.set_context({"status_code": 500}, "response") return wrapped(*args, **kwargs)
async def call(self, module, method, wrapped, instance, args, kwargs): if not hasattr(instance.application, "elasticapm_client"): # If tornado was instrumented but not as the main framework # (i.e. in Flower), we should skip it. return await wrapped(*args, **kwargs) # Late import to avoid ImportErrors from elasticapm.contrib.tornado.utils import get_data_from_request, get_data_from_response request = instance.request trace_parent = TraceParent.from_headers(request.headers) client = instance.application.elasticapm_client client.begin_transaction("request", trace_parent=trace_parent) elasticapm.set_context( lambda: get_data_from_request(instance, request, client.config, constants.TRANSACTION), "request") # TODO: Can we somehow incorporate the routing rule itself here? elasticapm.set_transaction_name("{} {}".format( request.method, type(instance).__name__), override=False) ret = await wrapped(*args, **kwargs) elasticapm.set_context( lambda: get_data_from_response(instance, client.config, constants. TRANSACTION), "response") status = instance.get_status() result = "HTTP {}xx".format(status // 100) elasticapm.set_transaction_result(result, override=False) elasticapm.set_transaction_outcome(http_status_code=status) client.end_transaction() return ret
def on_finish(self): apm_elastic = self.settings.get("apm_elastic") name_trasaction = '{} {}'.format(self.request.method, self.get_url()) status = self.get_status() result = 'HTTP {}xx'.format(status // 100) data_request = get_data_from_request(self.request) data_response = get_data_from_response(self) elasticapm.set_context(lambda: data_request, "request") elasticapm.set_context(lambda: data_response, "response") elasticapm.set_transaction_name(name_trasaction, override=False) elasticapm.set_transaction_result(result, override=False) apm_elastic.client.end_transaction()
def capture_exception(self): apm_elastic = self.settings.get("apm_elastic") apm_elastic.client.capture_exception( context={"request": get_data_from_request(self.request)}, handled=False, )