Example #1
0
def test_get_task_main():
    # type: (...) -> None
    if _task._gevent_tracer is None:
        assert _task.get_task(nogevent.main_thread_id) == (None, None, None)
    else:
        assert _task.get_task(
            nogevent.main_thread_id) == (compat.main_thread.ident,
                                         "MainThread", None)
Example #2
0
    def acquire(self, *args, **kwargs):
        if not self._self_capture_sampler.capture():
            return self.__wrapped__.acquire(*args, **kwargs)

        start = compat.monotonic_ns()
        try:
            return self.__wrapped__.acquire(*args, **kwargs)
        finally:
            try:
                end = self._self_acquired_at = compat.monotonic_ns()
                thread_id, thread_name = _current_thread()
                frames, nframes = _traceback.pyframe_to_frames(sys._getframe(1), self._self_max_nframes)
                task_id, task_name = _task.get_task(thread_id)
                event = LockAcquireEvent(
                    lock_name=self._self_name,
                    frames=frames,
                    nframes=nframes,
                    thread_id=thread_id,
                    thread_name=thread_name,
                    task_id=task_id,
                    task_name=task_name,
                    wait_time_ns=end - start,
                    sampling_pct=self._self_capture_sampler.capture_pct,
                )

                if self._self_tracer is not None:
                    event.set_trace_info(self._self_tracer.current_span(), self._self_endpoint_collection_enabled)

                self._self_recorder.push_event(event)
            except Exception:
                pass
Example #3
0
    def release(self, *args, **kwargs):
        try:
            return self.__wrapped__.release(*args, **kwargs)
        finally:
            try:
                if hasattr(self, "_self_acquired_at"):
                    try:
                        end = compat.monotonic_ns()
                        frames, nframes = _traceback.pyframe_to_frames(sys._getframe(1), self._self_max_nframes)
                        thread_id, thread_name = _current_thread()
                        task_id, task_name = _task.get_task(thread_id)
                        event = LockReleaseEvent(
                            lock_name=self._self_name,
                            frames=frames,
                            nframes=nframes,
                            thread_id=thread_id,
                            thread_name=thread_name,
                            task_id=task_id,
                            task_name=task_name,
                            locked_for_ns=end - self._self_acquired_at,
                            sampling_pct=self._self_capture_sampler.capture_pct,
                        )

                        if self._self_tracer is not None:
                            event.set_trace_info(
                                self._self_tracer.current_span(), self._self_endpoint_collection_enabled
                            )

                        self._self_recorder.push_event(event)
                    finally:
                        del self._self_acquired_at
            except Exception:
                pass
Example #4
0
    def release(
            self,
            *args,  # type: typing.Any
            **kwargs  # type: typing.Any
    ):
        # type: (...) -> None
        try:
            return self.__wrapped__.release(*args, **kwargs)
        finally:
            try:
                if hasattr(self, "_self_acquired_at"):
                    try:
                        end = compat.monotonic_ns()
                        thread_id, thread_name = _current_thread()
                        task_id, task_name, task_frame = _task.get_task(
                            thread_id)

                        if task_frame is None:
                            frame = sys._getframe(1)
                        else:
                            frame = task_frame

                        frames, nframes = _traceback.pyframe_to_frames(
                            frame, self._self_max_nframes)

                        event = self.RELEASE_EVENT_CLASS(  # type: ignore[call-arg]
                            lock_name=self._self_name,
                            frames=frames,
                            nframes=nframes,
                            thread_id=thread_id,
                            thread_name=thread_name,
                            task_id=task_id,
                            task_name=task_name,
                            locked_for_ns=end - self._self_acquired_at,
                            sampling_pct=self._self_capture_sampler.
                            capture_pct,
                        )

                        if self._self_tracer is not None:
                            event.set_trace_info(
                                self._self_tracer.current_span(),
                                self._self_endpoint_collection_enabled)

                        self._self_recorder.push_event(event)
                    finally:
                        del self._self_acquired_at
            except Exception:
                pass