Esempio n. 1
0
def events():
    rec = Recorder()
    rec.clear()
    rec.enabled = True
    yield rec.events
    rec.enabled = False
    rec.clear()
Esempio n. 2
0
class Middleware:
    def __init__(self, get_response):
        self.get_response = get_response
        self.recorder = Recorder()

    def __call__(self, request):
        if not Env.current.enabled:
            return self.get_response(request)

        if request.path_info == '/_appmap/record':
            return self.recording(request)
        if self.recorder.enabled:
            add_metadata()
            start = time.monotonic()
            params = request_params(request)
            try:
                resolved = resolve(request.path_info)
                params.update(resolved.kwargs)
                normalized_path_info = normalize_path_info(
                    request.path_info, resolved)
            except Resolver404:
                # If the request was for a bad path (e.g. when an app
                # is testing 404 handling), resolving will fail.
                normalized_path_info = None

            call_event = HttpServerRequestEvent(
                request_method=request.method,
                path_info=request.path_info,
                message_parameters=params,
                normalized_path_info=normalized_path_info,
                protocol=request.META['SERVER_PROTOCOL'],
                headers=request.headers)
            self.recorder.add_event(call_event)

        try:
            response = self.get_response(request)
        except:
            if self.recorder.enabled:
                duration = time.monotonic() - start
                exception_event = ExceptionEvent(parent_id=call_event.id,
                                                 elapsed=duration,
                                                 exc_info=sys.exc_info())
                self.recorder.add_event(exception_event)
            raise

        if self.recorder.enabled:
            duration = time.monotonic() - start
            return_event = HttpServerResponseEvent(
                parent_id=call_event.id,
                elapsed=duration,
                status_code=response.status_code,
                headers=dict(response.items()))
            self.recorder.add_event(return_event)

        return response

    def recording(self, request):
        """Handle recording requests."""
        if request.method == 'GET':
            return JsonResponse({'enabled': self.recorder.enabled})
        if request.method == 'POST':
            if self.recorder.enabled:
                return HttpResponse('Recording is already in progress',
                                    status=409)
            self.recorder.clear()
            self.recorder.start_recording()
            return HttpResponse()

        if request.method == 'DELETE':
            if not self.recorder.enabled:
                return HttpResponse('No recording is in progress', status=404)

            self.recorder.stop_recording()
            return HttpResponse(generation.dump(self.recorder),
                                content_type='application/json')

        return HttpResponseBadRequest()