async def _run_app(self, scope, callback): # type: (Any, Any) -> Any if _asgi_middleware_applied.get(False): return await callback() _asgi_middleware_applied.set(True) try: hub = Hub(Hub.current) with hub: with hub.configure_scope() as sentry_scope: sentry_scope.clear_breadcrumbs() sentry_scope._name = "asgi" processor = functools.partial(self.event_processor, asgi_scope=scope) sentry_scope.add_event_processor(processor) if scope["type"] in ("http", "websocket"): span = Span.continue_from_headers(dict(scope["headers"])) span.op = "{}.server".format(scope["type"]) else: span = Span() span.op = "asgi.server" span.set_tag("asgi.type", scope["type"]) span.transaction = "generic ASGI request" with hub.start_span(span) as span: try: return await callback() except Exception as exc: _capture_exception(hub, exc) raise exc from None finally: _asgi_middleware_applied.set(False)
def _inner(*args, **kwargs): # type: (*Any, **Any) -> Any hub = Hub.current if hub.get_integration(CeleryIntegration) is None: return f(*args, **kwargs) with hub.push_scope() as scope: scope._name = "celery" scope.clear_breadcrumbs() scope.add_event_processor(_make_event_processor(task, *args, **kwargs)) span = Span.continue_from_headers(args[3].get("headers") or {}) span.op = "celery.task" span.transaction = "unknown celery task" # Could possibly use a better hook than this one span.set_status("ok") with capture_internal_exceptions(): # Celery task objects are not a thing to be trusted. Even # something such as attribute access can fail. span.transaction = task.name with hub.start_span(span): return f(*args, **kwargs)
def sentry_patched_perform_job(self, job, *args, **kwargs): # type: (Any, Job, *Queue, **Any) -> bool hub = Hub.current integration = hub.get_integration(RqIntegration) if integration is None: return old_perform_job(self, job, *args, **kwargs) client = hub.client assert client is not None with hub.push_scope() as scope: scope.clear_breadcrumbs() scope.add_event_processor( _make_event_processor(weakref.ref(job))) span = Span.continue_from_headers( job.meta.get("_sentry_trace_headers") or {}) with capture_internal_exceptions(): span.transaction = job.func_name with hub.span(span): rv = old_perform_job(self, job, *args, **kwargs) if self.is_horse: # We're inside of a forked process and RQ is # about to call `os._exit`. Make sure that our # events get sent out. client.flush() return rv
async def inner(): # type: () -> Any hub = Hub.current if hub.get_integration(AioHttpIntegration) is None: return await old_handle(self, request, *args, **kwargs) weak_request = weakref.ref(request) with Hub(Hub.current) as hub: with hub.configure_scope() as scope: scope.clear_breadcrumbs() scope.add_event_processor( _make_request_processor(weak_request)) span = Span.continue_from_headers(request.headers) span.op = "http.server" # If this transaction name makes it to the UI, AIOHTTP's # URL resolver did not find a route or died trying. span.transaction = "generic AIOHTTP request" with hub.start_span(span): try: response = await old_handle(self, request) except HTTPException: raise except Exception: reraise(*_capture_exception(hub)) return response
def test_continue_from_headers(sentry_init, capture_events, sampled): sentry_init(traces_sample_rate=1.0, traceparent_v2=True) events = capture_events() with Hub.current.start_span(transaction="hi"): with Hub.current.start_span() as old_span: old_span.sampled = sampled headers = dict(Hub.current.iter_trace_propagation_headers()) header = headers["sentry-trace"] if sampled is True: assert header.endswith("-1") if sampled is False: assert header.endswith("-0") if sampled is None: assert header.endswith("-") span = Span.continue_from_headers(headers) span.transaction = "WRONG" assert span is not None assert span.sampled == sampled assert span.trace_id == old_span.trace_id assert span.same_process_as_parent is False assert span.parent_span_id == old_span.span_id assert span.span_id != old_span.span_id with Hub.current.start_span(span): with Hub.current.configure_scope() as scope: scope.transaction = "ho" capture_message("hello") if sampled is False: trace1, message = events assert trace1["transaction"] == "hi" else: trace1, message, trace2 = events assert trace1["transaction"] == "hi" assert trace2["transaction"] == "ho" assert ( trace1["contexts"]["trace"]["trace_id"] == trace2["contexts"]["trace"]["trace_id"] == span.trace_id == message["contexts"]["trace"]["trace_id"] ) assert message["message"] == "hello"
def _inner(*args, **kwargs): hub = Hub.current if hub.get_integration(CeleryIntegration) is None: return f(*args, **kwargs) with hub.push_scope() as scope: scope._name = "celery" scope.clear_breadcrumbs() scope.add_event_processor(_make_event_processor(task, *args, **kwargs)) span = Span.continue_from_headers(args[3].get("headers") or {}) span.transaction = "unknown celery task" with capture_internal_exceptions(): # Celery task objects are not a thing to be trusted. Even # something such as attribute access can fail. span.transaction = task.name with hub.span(span): return f(*args, **kwargs)
def _run_app(self, scope, callback): # type: (Any, Any) -> Any if _asgi_middleware_applied.get(False): response = yield from callback() return response _asgi_middleware_applied.set(True) try: hub = Hub(Hub.current) with hub: with hub.configure_scope() as sentry_scope: sentry_scope.clear_breadcrumbs() sentry_scope._name = "asgi" processor = functools.partial(self.event_processor, asgi_scope=scope) sentry_scope.add_event_processor(processor) if scope["type"] in ("http", "websocket"): span = Span.continue_from_headers(dict(scope["headers"])) span.op = "{}.server".format(scope["type"]) else: span = Span() span.op = "asgi.server" span.set_tag("asgi.type", scope["type"]) span.transaction = "generic ASGI request" with hub.start_span(span) as span: # XXX: Would be cool to have correct span status, but we # would have to wrap send(). That is a bit hard to do with # the current abstraction over ASGI 2/3. try: response = yield from callback() return response except Exception as exc: _capture_exception(hub, exc) raise exc from None finally: _asgi_middleware_applied.set(False)
def inner(): # type: () -> Any hub = Hub.current if hub.get_integration(AioHttpIntegration) is None: old_handle_response = yield from old_handle( self, request, *args, **kwargs) return old_handle_response weak_request = weakref.ref(request) with Hub(Hub.current) as hub: with hub.configure_scope() as scope: scope.clear_breadcrumbs() scope.add_event_processor( _make_request_processor(weak_request)) span = Span.continue_from_headers(request.headers) span.op = "http.server" # If this transaction name makes it to the UI, AIOHTTP's # URL resolver did not find a route or died trying. span.transaction = "generic AIOHTTP request" with hub.start_span(span): try: response = yield from old_handle(self, request) except HTTPException as e: span.set_http_status(e.status_code) raise except asyncio.CancelledError: span.set_status("cancelled") raise except Exception: # This will probably map to a 500 but seems like we # have no way to tell. Do not set span status. reraise(*_capture_exception(hub)) span.set_http_status(response.status) return response