Ejemplo n.º 1
0
    def process_response(self, req, resp, resource, req_succeeded=None):
        if self.client and req.user_agent != 'mcod-heartbeat':
            rule = route_to_name(req.uri_template,
                                 prefix='api',
                                 method=req.method)
            elasticapm.set_context(
                lambda: get_data_from_request(
                    req,
                    capture_body=self.client.config.capture_body in
                    ("transactions", "all"),
                    capture_headers=self.client.config.capture_headers,
                ),
                "request",
            )
            elasticapm.set_context(
                lambda: get_data_from_response(
                    resp, capture_headers=self.client.config.capture_headers),
                "response")

            result = resp.status
            elasticapm.set_transaction_name(rule, override=False)
            if hasattr(req, 'user') and req.user.is_authenticated:
                elasticapm.set_user_context(email=req.user.email,
                                            user_id=req.user.id)

            elasticapm.set_transaction_result(result, override=False)
            # Instead of calling end_transaction here, we defer the call until the response is closed.
            # This ensures that we capture things that happen until the WSGI server closes the response.
            self.client.end_transaction(rule, result)
Ejemplo n.º 2
0
def apm(request):
    if request.resolver_match and hasattr(request.resolver_match, 'route'):
        route = request.resolver_match.route
    else:
        route = request.path

    return {
        'admin_amp_transaction_name': route_to_name(route, prefix='admin', method=request.method)
    }
Ejemplo n.º 3
0
    def process_response(self, request, response):
        if django_settings.DEBUG and not self.client.config.debug:
            return response
        if request.META.get('HTTP_USER_AGENT') == 'mcod-heartbeat':
            return response
        try:
            if hasattr(response, "status_code"):
                try:
                    route = request.resolver_match.route
                except AttributeError:
                    route = request.path

                transaction_name = route_to_name(route,
                                                 prefix='admin',
                                                 method=request.method)
                if transaction_name:
                    elasticapm.set_transaction_name(transaction_name,
                                                    override=False)

                elasticapm.set_context(
                    lambda: self.client.get_data_from_request(
                        request,
                        capture_body=self.client.config.capture_body in
                        ("all", "transactions")),
                    "request",
                )
                elasticapm.set_context(
                    lambda: self.client.get_data_from_response(response),
                    "response")
                elasticapm.set_context(
                    lambda: self.client.get_user_info(request), "user")
                elasticapm.set_transaction_result("HTTP {}xx".format(
                    response.status_code // 100),
                                                  override=False)
        except Exception:
            self.client.error_logger.error(
                "Exception during timing of request", exc_info=True)
        return response