def capture_event(event): if event.get("type") == "transaction" and options.get( "transaction-events.force-disable-internal-project"): return # Upstream should get the event first because it is most isolated from # the this sentry installation. if upstream_transport: metrics.incr("internal.captured.events.upstream") # TODO(mattrobenolt): Bring this back safely. # from sentry import options # install_id = options.get('sentry:install-id') # if install_id: # event.setdefault('tags', {})['install-id'] = install_id upstream_transport.capture_event(event) if relay_transport and options.get( "store.use-relay-dsn-sample-rate") == 1: if is_current_event_safe(): metrics.incr("internal.captured.events.relay") relay_transport.capture_event(event) else: metrics.incr("internal.uncaptured.events.relay", skip_internal=False) if event.get("type") != "transaction": sdk_logger.warn("internal-error.unsafe-stacktrace.relay")
def get_project_key(): from sentry.models import ProjectKey if not settings.SENTRY_PROJECT: return None key = None try: if settings.SENTRY_PROJECT_KEY is not None: key = ProjectKey.objects.get(id=settings.SENTRY_PROJECT_KEY, project=settings.SENTRY_PROJECT) else: key = ProjectKey.get_default(settings.SENTRY_PROJECT) except Exception as exc: # if the relation fails to query or is missing completely, lets handle # it gracefully sdk_logger.warn( "internal-error.unable-to-fetch-project", extra={ "project_id": settings.SENTRY_PROJECT, "project_key": settings.SENTRY_PROJECT_KEY, "error_message": six.text_type(exc), }, ) if key is None: sdk_logger.warn( "internal-error.no-project-available", extra={ "project_id": settings.SENTRY_PROJECT, "project_key": settings.SENTRY_PROJECT_KEY, }, ) return key
def setup_integrations(integrations, with_defaults=True): """Given a list of integration instances this installs them all. When `with_defaults` is set to `True` then all default integrations are added unless they were already provided before. """ integrations = dict((integration.identifier, integration) for integration in integrations or ()) if with_defaults: for integration_cls in iter_default_integrations(): if integration_cls.identifier not in integrations: instance = integration_cls() integrations[instance.identifier] = instance for identifier, integration in iteritems(integrations): with _installer_lock: if identifier not in _installed_integrations: try: type(integration).setup_once() except NotImplementedError: if getattr(integration, "install", None) is not None: logger.warn( "Integration %s: The install method is " "deprecated. Use `setup_once`.", identifier, ) integration.install() else: raise _installed_integrations.add(identifier) return integrations
def get_project_key(): from sentry.models import ProjectKey if not settings.SENTRY_PROJECT: return None key = None try: if settings.SENTRY_PROJECT_KEY is not None: key = ProjectKey.objects.get( id=settings.SENTRY_PROJECT_KEY, project=settings.SENTRY_PROJECT, ) else: key = ProjectKey.get_default(settings.SENTRY_PROJECT) except Exception as exc: # if the relation fails to query or is missing completely, lets handle # it gracefully sdk_logger.warn('internal-error.unable-to-fetch-project', extra={ 'project_id': settings.SENTRY_PROJECT, 'project_key': settings.SENTRY_PROJECT_KEY, 'error_message': six.text_type(exc), }) if key is None: sdk_logger.warn('internal-error.no-project-available', extra={ 'project_id': settings.SENTRY_PROJECT, 'project_key': settings.SENTRY_PROJECT_KEY, }) return key
def _capture_event(self, event): with capture_internal_exceptions(): key = self.project_key if key is None: return if not is_current_event_safe(): metrics.incr("internal.uncaptured.events", skip_internal=False) sdk_logger.warn("internal-error.unsafe-stacktrace") return auth = Auth( scheme="https", host="localhost", project_id=key.project_id, public_key=key.public_key, secret_key=key.secret_key, client="sentry-python/%s" % SDK_VERSION, ) headers = { "HTTP_X_SENTRY_AUTH": auth.to_header(), "HTTP_CONTENT_ENCODING": "deflate" } request = self.request_factory.post( "/api/{}/store/".format(key.project_id), data=zlib.compress(json.dumps(event).encode("utf8")), content_type="application/octet-stream", **headers) from sentry.web.api import StoreView resp = StoreView.as_view()(request, project_id=six.text_type( key.project_id)) if resp.status_code != 200: sdk_logger.warn( "internal-error.invalid-response", extra={ "project_id": settings.SENTRY_PROJECT, "project_key": settings.SENTRY_PROJECT_KEY, "status_code": resp.status_code, }, )
def _capture_event(self, event): with capture_internal_exceptions(): key = self.project_key if key is None: return if not is_current_event_safe(): metrics.incr('internal.uncaptured.events', skip_internal=False) sdk_logger.warn('internal-error.unsafe-stacktrace') return auth = Auth( scheme="https", host="localhost", project_id=key.project_id, public_key=key.public_key, secret_key=key.secret_key, client="sentry-python/%s" % SDK_VERSION ) headers = { 'HTTP_X_SENTRY_AUTH': auth.to_header(), 'HTTP_CONTENT_ENCODING': 'deflate' } request = self.request_factory.post( '/api/{}/store/'.format(key.project_id), data=zlib.compress(json.dumps(event).encode('utf8')), content_type='application/octet-stream', **headers ) from sentry.web.api import StoreView resp = StoreView.as_view()( request, project_id=six.text_type(key.project_id), ) if resp.status_code != 200: sdk_logger.warn('internal-error.invalid-response', extra={ 'project_id': settings.SENTRY_PROJECT, 'project_key': settings.SENTRY_PROJECT_KEY, 'status_code': resp.status_code, })
def capture_event(self, event): with capture_internal_exceptions(): key = self.project_key if key is None: return if not is_current_event_safe(): metrics.incr('internal.uncaptured.events') sdk_logger.warn('internal-error.unsafe-stacktrace') return auth = Auth( scheme="https", host="localhost", project_id=key.project_id, public_key=key.public_key, secret_key=key.secret_key, client="sentry-python/%s" % SDK_VERSION ) headers = { 'HTTP_X_SENTRY_AUTH': auth.to_header(), 'HTTP_CONTENT_ENCODING': 'deflate' } request = self.request_factory.post( '/api/{}/store/'.format(key.project_id), data=zlib.compress(json.dumps(event).encode('utf8')), content_type='application/octet-stream', **headers ) from sentry.web.api import StoreView resp = StoreView.as_view()( request, project_id=six.text_type(key.project_id), ) if resp.status_code != 200: sdk_logger.warn('internal-error.invalid-response', extra={ 'project_id': settings.SENTRY_PROJECT, 'project_key': settings.SENTRY_PROJECT_KEY, 'status_code': resp.status_code, })
def setup_integrations(integrations, with_defaults=True): # type: (List[Integration], bool) -> Dict[str, Integration] """Given a list of integration instances this installs them all. When `with_defaults` is set to `True` then all default integrations are added unless they were already provided before. """ integrations = dict( (integration.identifier, integration) for integration in integrations or () ) logger.debug("Setting up integrations (with default = %s)", with_defaults) if with_defaults: for integration_cls in iter_default_integrations(): if integration_cls.identifier not in integrations: instance = integration_cls() integrations[instance.identifier] = instance for identifier, integration in iteritems(integrations): # type: ignore with _installer_lock: if identifier not in _installed_integrations: logger.debug( "Setting up previously not enabled integration %s", identifier ) try: type(integration).setup_once() except NotImplementedError: if getattr(integration, "install", None) is not None: logger.warn( "Integration %s: The install method is " "deprecated. Use `setup_once`.", identifier, ) integration.install() else: raise _installed_integrations.add(identifier) for identifier in integrations: logger.debug("Enabling integration %s", identifier) return integrations
def transport(event): with capture_internal_exceptions(): metrics.incr('internal.uncaptured.events', skip_internal=False) sdk_logger.warn('internal-error.noop-hub')
def transport(event): with capture_internal_exceptions(): metrics.incr("internal.uncaptured.events.noop-hub", skip_internal=False) sdk_logger.warn("internal-error.noop-hub")
def setup_once(): import __main__ as lambda_bootstrap if not hasattr(lambda_bootstrap, "make_final_handler"): logger.warn("Not running in AWS Lambda environment, " "AwsLambdaIntegration disabled") return import runtime as lambda_runtime old_make_final_handler = lambda_bootstrap.make_final_handler def sentry_make_final_handler(*args, **kwargs): handler = old_make_final_handler(*args, **kwargs) def sentry_handler(event, context, *args, **kwargs): hub = Hub.current integration = hub.get_integration(AwsLambdaIntegration) if integration is None: return handler(event, context, *args, **kwargs) with hub.push_scope() as scope: with capture_internal_exceptions(): scope.transaction = context.function_name scope.add_event_processor( _make_request_event_processor(event, context)) try: return handler(event, context, *args, **kwargs) except Exception: exc_info = sys.exc_info() event, hint = event_from_exception( exc_info, client_options=hub.client.options, mechanism={ "type": "aws_lambda", "handled": False }, ) hub.capture_event(event, hint=hint) reraise(*exc_info) return sentry_handler lambda_bootstrap.make_final_handler = sentry_make_final_handler old_report_done = lambda_runtime.report_done def sentry_report_done(*args, **kwargs): with capture_internal_exceptions(): hub = Hub.current integration = hub.get_integration(AwsLambdaIntegration) if integration is not None: # Flush out the event queue before AWS kills the # process. This is not threadsafe. # make new transport with empty queue new_transport = hub.client.transport.copy() hub.client.close() hub.client.transport = new_transport return old_report_done(*args, **kwargs) lambda_runtime.report_done = sentry_report_done