Exemple #1
0
class Sentry(object):
    """Sentry wrapper class that allows this app to work without a sentry token.

    If no token is specified in the config, the messages used for logging are simply not called.
    """

    initialized = False

    def __init__(self, config):
        """Construct new sentry wrapper."""
        if config['develop']['sentry_token'] is not None \
                and config['develop'].getboolean('sentry_enabled'):
            self.initialized = True
            self.sentry = Client(
                config['develop']['sentry_token'],
                ignore_exceptions=[
                    KeyboardInterrupt,
                    GitError,
                ],
            )

    def captureMessage(self, *args, **kwargs):
        """Capture message with sentry."""
        if self.initialized:
            self.sentry.captureMessage(*args, **kwargs)

    def captureException(self, *args, **kwargs):
        """Capture exception with sentry."""
        if self.initialized:
            self.sentry.captureException(*args, **kwargs)
Exemple #2
0
class SentryNotifier(object):  # pylint: disable=unused-variable
    """Send messages to a Sentry server."""
    command = "sentry"

    def __init__(self, args, extra):
        """Initialize Sentry notifier with client"""
        self.args = args
        self.extra = extra
        self.client = Client(self.args.sentry_dsn)

    @classmethod
    def parse_args(cls, parser):
        """Add command line argument parsing rules for Sentry."""
        parser.add_argument(
            "--sentry-dsn",
            required=False,
            help="""Sentry DSN to be used for notifications. It can also be set
                            with the evironment variable $SENTRY_DSN.""")

    def notify(self, msg=""):
        """Send a message to Sentry server.

        The message can be an exception or a simple string. In the first case,
        handle it with `captureException` method to add more context to the
        error.
        """
        if issubclass(msg.__class__, Exception):
            self.client.captureException()
        else:
            self.client.captureMessage(msg)
    def process_jobs(self, beanstalk):
        while True:
            logger.debug("Beanstalk connection established, waiting for jobs")
            job = beanstalk.reserve()
            job_name = job.stats()["tube"]
            if job_name in self.jobs:
                logger.debug("Calling %s with arg: %s" % (job_name, job.body))
                try:
                    connection = db.connections["default"]
                    if connection.connection:
                        try:
                            connection.connection.ping()
                        except OperationalError as e:
                            connection.close()

                    flush_transaction()
                    self.jobs[job_name](job.body)
                except Exception, e:
                    tp, value, tb = sys.exc_info()
                    logger.error('Error while calling "%s" with arg "%s": ' "%s" % (job_name, job.body, e))
                    logger.debug("%s:%s" % (tp.__name__, value))
                    logger.debug("\n".join(traceback.format_tb(tb)))

                    client = Client(dsn=settings.RAVEN_CONFIG["dsn"])
                    client.captureMessage(str(e), stack=True, level=logging.ERROR)

                    job.bury()
                else:
                    job.delete()
            else:
                job.release()
Exemple #4
0
    async def report(self, success: bool, job: 'RunningJob',
                     config: Dict[str, Any]) -> None:
        config = config['sentry']
        if config['dsn']['value']:
            dsn = config['dsn']['value']
        elif config['dsn']['fromFile']:
            with open(config['dsn']['fromFile'], "rt") as dsn_file:
                dsn = dsn_file.read().strip()
        elif config['dsn']['fromEnvVar']:
            dsn = os.environ[config['dsn']['fromEnvVar']]
        else:
            return  # sentry disabled: early return

        template = jinja2.Template(config['body'])
        body = template.render(job.template_vars)
        client = Client(transport=AioHttpTransport,
                        dsn=dsn,
                        string_max_length=4096)
        extra = {
            'job': job.config.name,
            'exit_code': job.retcode,
            'command': job.config.command,
            'shell': job.config.shell,
            'success': success,
        }
        logger.debug("sentry body: %r", body)
        client.captureMessage(
            body,
            extra=extra,
        )
Exemple #5
0
    def report_fail(self, exit_status, last_lines_stdout, last_lines_stderr,
                    elapsed):
        if self.dsn == "":
            return

        message = "Command \"%s\" failed" % (self.command, )

        client = Client(transport=HTTPTransport,
                        dsn=self.dsn,
                        string_max_length=self.string_max_length)

        # if this fails, it will log to stderr (including the original command that failed)
        client.captureMessage(message,
                              data={
                                  'logger': 'cron',
                              },
                              extra={
                                  'command': self.command,
                                  'exit_status': exit_status,
                                  'last_lines_stdout': last_lines_stdout,
                                  'last_lines_stderr': last_lines_stderr,
                              },
                              time_spent=elapsed)

        return client.state.did_fail()
Exemple #6
0
    def report(self, exit_status, last_lines_stdout, last_lines_stderr,
               elapsed):
        if self.dsn is None:
            return

        if exit_status == 0:
            message = "Command \"%s\" has succeeded" % (self.command, )
            log_level = logging.INFO
        else:
            message = "Command \"%s\" has failed" % (self.command, )
            log_level = logging.ERROR

        client = Client(transport=HTTPTransport,
                        dsn=self.dsn,
                        string_max_length=self.string_max_length)
        extra = self.extra.copy()
        extra.update({
            'command': self.command,
            'exit_status': exit_status,
            'last_lines_stdout': last_lines_stdout,
            'last_lines_stderr': last_lines_stderr,
        })

        client.captureMessage(message,
                              level=log_level,
                              data={
                                  'logger': 'cron',
                              },
                              extra=extra,
                              time_spent=elapsed)
Exemple #7
0
class Sentry(object):
    """Sentry wrapper class that allows this app to work without a sentry token.

    If no token is specified in the config, the messages used for logging are simply not called.
    """

    initialized = False

    def __init__(self):
        """Construct new sentry wrapper."""
        if config["logging"]["sentry_enabled"]:
            self.initialized = True
            self.sentry = Client(config["logging"]["sentry_token"])

    def captureMessage(self, *args, **kwargs):
        """Capture message with sentry."""
        if self.initialized:
            if "tags" not in kwargs:
                kwargs["tags"] = {}

            # Tag it as stickerfinder
            kwargs["tags"]["bot"] = "stickerfinder"
            self.sentry.captureMessage(*args, **kwargs)

    def captureException(self, *args, **kwargs):
        """Capture exception with sentry."""
        if self.initialized:
            if "tags" not in kwargs:
                kwargs["tags"] = {}

            # Tag it as stickerfinder
            kwargs["tags"]["bot"] = "stickerfinder"
            self.sentry.captureException(*args, **kwargs)
Exemple #8
0
            def load_beanstalk_data(instance, beanstalk_data_str):
                try:
                    beanstalk_data = json.loads(beanstalk_data_str)

                    instance.beanstalk_data = beanstalk_data
                    instance.jobdata_pk = beanstalk_data["jobdata_pk"]
                    instance.attempt = beanstalk_data.get("attempt", 1)
                    return True
                except (TypeError, ValueError):
                    error_msg = "Unable to load json data."
                    culprit = instance.get_sentry_culprit("load_beanstalk_data")
                    error_data = {
                        "culprit": culprit,
                        "extra": {
                            "data string": beanstalk_data_str,
                        },
                    }
                    raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn'])
                    raven_client.captureMessage(error_msg, data=error_data, stack=True)
                except KeyError as e:
                    error_msg = "Missing required parameters for retry data beanstalk job."
                    culprit = instance.get_sentry_culprit("load_beanstalk_data")
                    error_data = {
                        "culprit": culprit,
                        "extra": {
                            "error": str(e),
                        }
                    }
                    raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn'])
                    raven_client.captureMessage(error_msg, data=error_data, stack=True)

                return False
Exemple #9
0
 def warn(self, msg):
     try:
         sentry_client = Client(sentry_dns)
         sentry_client.captureMessage(msg)
     except Exception as exp:
         logger = logging.getLogger('exception')
         logger.exception(exp)
Exemple #10
0
class SentryClient(object):
    client = None

    def __init__(self, ignore_exceptions=None):
        self.client = Client(dsn=settings.SENTRY_DSN,
                             ignore_exceptions=ignore_exceptions)

    def error(self, message):
        return self.client.captureMessage(message, level='error')

    def warning(self, message):
        return self.client.captureMessage(message, level='warning')

    def info(self, message):
        return self.client.captureMessage(message, level='info')

    def debug(self, message):
        return self.client.captureMessage(message, level='debug')

    def fatal(self, message):
        return self.client.captureMessage(message, level='fatal')

    def capture_exception(self):
        try:
            self.client.captureException()
        except:
            pass
Exemple #11
0
class Sentry(object):
    """Sentry wrapper class that allows this app to work without a sentry token.

    If no token is specified in the config, the methods used for logging are simply not called.
    """

    initialized = False

    def __init__(self):
        """Construct new sentry wrapper."""
        if config['logging']['sentry_enabled']:
            self.initialized = True
            self.sentry = Client(config['logging']['sentry_token'])

    def captureMessage(self, *args, **kwargs):
        """Capture message with sentry."""
        if self.initialized:
            if 'tags' not in kwargs:
                kwargs['tags'] = {}

            # Tag it as hetznerbot
            kwargs['tags']['bot'] = 'archivebot'
            self.sentry.captureMessage(*args, **kwargs)

    def captureException(self, *args, **kwargs):
        """Capture exception with sentry."""
        if self.initialized:
            if 'tags' not in kwargs:
                kwargs['tags'] = {}

            # Tag it as hetznerbot
            kwargs['tags']['bot'] = 'archivebot'
            self.sentry.captureException(*args, **kwargs)
Exemple #12
0
 def __call__(instance, pk_str):
     try:
         flush_transaction()
         pk = int(pk_str)
         data = JobData.objects.get(pk=pk)
     except (TypeError, ValueError) as e:
         error_msg = "Invalid value for pk"
         error_extra = {
             "pk tried": pk_str
         }
         raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn'])
         raven_client.captureMessage(error_msg, extra=error_extra, stack=True)
     except JobData.DoesNotExist:
         error_msg = "Unable to find beanstalk job data."
         error_extra = {
             "pk tried": pk
         }
         raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn'])
         raven_client.captureMessage(error_msg, extra=error_extra, stack=True)
     else:
         try:
             val = instance.f(data)
             if self.cleanup:
                 data.delete()
             return val
         except Exception as e:
             raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn'])
             raven_client.captureException()
Exemple #13
0
 def connect_sentry(result):
     pillar_data = __salt__['pillar.raw']()
     sentry_data = {
         'result': result,
         'returned': ret,
         'pillar': pillar_data,
         'grains': __salt__['grains.items']()
     }
     servers = []
     try:
         for server in pillar_data['raven']['servers']:
             servers.append(server + '/api/store/')
         client = Client(
             servers=servers,
             public_key=pillar_data['raven']['public_key'],
             secret_key=pillar_data['raven']['secret_key'],
             project=pillar_data['raven']['project'],
         )
     except KeyError as missing_key:
         logger.error("Sentry returner need config '%s' in pillar",
                      missing_key)
     else:
         try:
             client.captureMessage(ret['comment'], extra=sentry_data)
         except Exception as err:
             logger.error("Can't send message to sentry: %s", err,
                          exc_info=True)
Exemple #14
0
    def main(self):
        # consume standard input early
        lines = []
        p = re.compile("Subject: Cron <[^@]+@[^ ]+> (.*)")
        mail_subject = 'This mail has no subject.'
        for line in sys.stdin:
            line = line.rstrip()
            lines.append(line)
            if line.startswith('Subject:'):
                mail_subject = line
                # Removes hostname from cron subject to aggregate sentry events
                if p.search(line):
                    cron_subject = p.search(line).group(1)
                    mail_subject = "Subject: Cron {0}".format(cron_subject)

        body = os.linesep.join(lines)
        if not len(body):
            sys.stderr.write("Empty stdin, nothing to report")
            sys.stderr.write(os.linesep)
            sys.exit(1)

        # init raven quickly, so if something is wrong it get logged early
        from raven import Client
        dsn = self.config['sentry_dsn']
        if not dsn.startswith("requests+"):
            dsn = "requests+" + dsn
        client = Client(dsn=dsn)

        if self.config['subject']:
            subject = self.config['subject']
        else:
            subject = mail_subject
        msg = os.linesep.join((subject, body))
        client.captureMessage(msg, extra=os.environ)
Exemple #15
0
    def main(self):
        # consume standard input early
        lines = []
        p = re.compile("Subject: Cron <[^@]+@[^ ]+> (.*)")
        mail_subject = 'This mail has no subject.'
        for line in sys.stdin:
            line = line.rstrip()
            lines.append(line)
            if line.startswith('Subject:'):
                mail_subject = line
                # Removes hostname from cron subject to aggregate sentry events
                if p.search(line):
                    cron_subject = p.search(line).group(1)
                    mail_subject = "Subject: Cron {0}".format(cron_subject)

        body = os.linesep.join(lines)
        if not len(body):
            sys.stderr.write("Empty stdin, nothing to report")
            sys.stderr.write(os.linesep)
            sys.exit(1)

        # init raven quickly, so if something is wrong it get logged early
        from raven import Client
        dsn = self.config['sentry_dsn']
        if not dsn.startswith("requests+"):
            dsn = "requests+" + dsn
        client = Client(dsn=dsn)

        if self.config['subject']:
            subject = self.config['subject']
        else:
            subject = mail_subject
        msg = os.linesep.join((subject, body))
        client.captureMessage(msg, extra=os.environ)
Exemple #16
0
class Sentry(object):
    """Sentry wrapper class that allows this app to work without a sentry token.

    If no token is specified in the config, the messages used for logging are simply not called.
    """

    initialized = False

    def __init__(self):
        """Construct new sentry wrapper."""
        if config.SENTRY_TOKEN is not None:
            self.initialized = True
            self.sentry = Client(config.SENTRY_TOKEN)

    def captureMessage(self, *args, **kwargs):
        """Capture message with sentry."""
        if self.initialized:
            if 'tags' not in kwargs:
                kwargs['tags'] = {}

            # Tag it as stickerfinder
            kwargs['tags']['bot'] = 'stickerfinder'
            self.sentry.captureMessage(*args, **kwargs)

    def captureException(self, *args, **kwargs):
        """Capture exception with sentry."""
        if self.initialized:
            if 'tags' not in kwargs:
                kwargs['tags'] = {}

            # Tag it as stickerfinder
            kwargs['tags']['bot'] = 'stickerfinder'
            self.sentry.captureException(*args, **kwargs)
    def _write_message_with_apnsclient(self, message, devices):

        # start with all devices in "complete" list. remove as necessary.
        # convert to list: attempting to avoid deadlock in "set_devices_last_notified_at"
        complete_devices = list(devices[:])
        fail_devices = []
        retry_devices = []

        con = self.session.get_connection(address=(self.hostname, 2195), cert_string=self.certificate, key_string=self.private_key)

        srv = APNs(con)
        res = srv.send(message)

        # Check failures. Check codes in APNs reference docs.
        for token, reason in res.failed.items():
            code, errmsg = reason

            # Log with sentry
            client = Client(dsn=settings.RAVEN_CONFIG['dsn'])
            client.captureMessage("APNs Failure - Reason:%s - Device:%s" % (errmsg, token))

            # Disable device
            for device in devices:
                if device.token == token:
                    complete_devices.remove(device)

                    device.is_active = False
                    device.save()

            print "Device faled: {0}, reason: {1}".format(token, errmsg)

        # Check failures not related to devices.
        for code, errmsg in res.errors:

            # Log with sentry
            client = Client(dsn=settings.RAVEN_CONFIG['dsn'])
            client.captureMessage("APNs Failure - Error:%s" % errmsg)

            print "Error: ", errmsg

        # Check if there are tokens that can be retried
        if res.needs_retry():
            # repeat with retry_message
            retry_message = res.retry()

            # add retried devices to "retry_devices"
            for token in retry_message.tokens:
                for device in complete_devices:
                    if device.token == token:
                        retry_devices.append(device)
            # remove retried devices from "complete_devices"
            for device in retry_devices:
                complete_devices.remove(device)

            # retry message
            self._write_message_with_apnsclient(retry_message, retry_devices)

        # set date of last message for "complete_devices"
        self.set_devices_last_notified_at(complete_devices)
Exemple #18
0
def common_except_info(info, from_function):
    """
    输出特定消息到sentry中
    by: 马志  at: 2015-11-27

    """
    client = Client(settings.SENTRY_CLIENT_KEY)
    client.captureMessage("info:%s,from:%s" % (info, from_function))
Exemple #19
0
def handle_error(message, logger, status_code=500):
    env = os.getenv("ENV", "test")
    if env is not "test":
        client = Client(RAVEN_CLIENT)
        client.captureMessage("%s in authorizer %s: %s" %
                              (status_code, os.getenv("ENV", "test"), message))
    logger.error(message)
    return make_response(message, status_code)
Exemple #20
0
class CommandReporter(object):
    def __init__(self, cmd, dsn):
        self.dsn = dsn
        self.command = cmd
        self.client = None

    def run(self):
        start = time()

        with TemporaryFile() as stdout:
            with TemporaryFile() as stderr:
                exit_status = call(self.command, stdout=stdout, stderr=stderr)

                last_lines_stdout = self._get_last_lines(stdout)
                last_lines_stderr = self._get_last_lines(stderr)

                if exit_status > 0:
                    elapsed = int((time() - start) * 1000)
                    self.report_fail(exit_status, last_lines_stdout, last_lines_stderr, elapsed)

                sys.stdout.write(last_lines_stdout)
                sys.stderr.write(last_lines_stderr)


    def report_fail(self, exit_status, last_lines_stdout, last_lines_stderr, elapsed):
        if self.dsn is None:
            return

        message = "Command \"%s\" failed" % (self.command,)

        if self.client is None:
            self.client = Client(dsn=self.dsn)

        self.client.captureMessage(
            message,
            data={
                'logger': 'cron',
            },
            extra={
                'command': self.command,
                'exit_status': exit_status,
                'last_lines_stdout': last_lines_stdout,
                'last_lines_stderr': last_lines_stderr,
            },
            time_spent=elapsed
        )

    def _get_last_lines(self, buf):
        buf.seek(0, SEEK_END)
        file_size = buf.tell()
        if file_size < MAX_MESSAGE_SIZE:
            buf.seek(0)
            last_lines = buf.read().decode('utf-8')
        else:
            buf.seek(-(MAX_MESSAGE_SIZE-3), SEEK_END)
            last_lines = u'...' + buf.read().decode('utf-8')
        return last_lines
Exemple #21
0
class CommandReporter(object):
    def __init__(self, cmd, dsn, always, logger, description):

        self.dsn = dsn
        self.command = " ".join(cmd)
        self.always = always
        self.client = None
        self.logger = logger
        self.description = description

    def run(self):
        buf = TemporaryFile()
        start = time()

        exit_status = call(self.command, stdout=buf, stderr=buf, shell=True)
        
        if exit_status > 0 or self.always == True:
            elapsed = int((time() - start) * 1000)
            self.report_fail(exit_status, buf, elapsed)

        buf.close()
        
    def report_fail(self, exit_status, buf, elapsed):
        if self.dsn is None:
            return

        # Hack to get the file size since the tempfile doesn't exist anymore
        buf.seek(0, SEEK_END)
        file_size = buf.tell()
        if file_size < MAX_MESSAGE_SIZE:
            buf.seek(0)
            last_lines = buf.read()
        else:
            buf.seek(-(MAX_MESSAGE_SIZE-3), SEEK_END)
            last_lines = '...' + buf.read()

        if self.description:
            message=self.description
        else:
            message="Command \"%s\" failed" % (self.command,)

        if self.client is None:
            self.client = Client(dsn=self.dsn)

        self.client.captureMessage(
            message,
            data={
                'logger': self.logger,
            },
            extra={
                'command': self.command,
                'exit_status': exit_status,
                'last_lines': last_lines,
            },
            time_spent=elapsed
        )
Exemple #22
0
 def activate(self):
     """Activates the user and logs him in."""
     user = get_user_model().objects.get(pk=self.activation_key)
     user.is_active = True
     user.save(update_fields=['is_active'])
     client = Client()
     client.captureMessage("New user activated",
                           extra={'email': user.email})
     user.backend = 'ratelimitbackend.backends.RateLimitModelBackend'
     auth_login(self.request, user)
Exemple #23
0
 def activate(self):
     """Activates the user and logs him in."""
     user = get_user_model().objects.get(pk=self.activation_key)
     user.is_active = True
     user.save(update_fields=['is_active'])
     client = Client()
     client.captureMessage("New user activated", extra={
         'email': user.email
     })
     user.backend = 'ratelimitbackend.backends.RateLimitModelBackend'
     auth_login(self.request, user)
Exemple #24
0
class CommandReporter(object):
    def __init__(self, cmd, dsn, always, logger, description):

        self.dsn = dsn
        self.command = " ".join(cmd)
        self.always = always
        self.client = None
        self.logger = logger
        self.description = description

    def run(self):
        buf = TemporaryFile()
        start = time()

        exit_status = call(self.command, stdout=buf, stderr=buf, shell=True)

        if exit_status > 0 or self.always == True:
            elapsed = int((time() - start) * 1000)
            self.report_fail(exit_status, buf, elapsed)

        buf.close()

    def report_fail(self, exit_status, buf, elapsed):
        if self.dsn is None:
            return

        # Hack to get the file size since the tempfile doesn't exist anymore
        buf.seek(0, SEEK_END)
        file_size = buf.tell()
        if file_size < MAX_MESSAGE_SIZE:
            buf.seek(0)
            last_lines = buf.read()
        else:
            buf.seek(-(MAX_MESSAGE_SIZE - 3), SEEK_END)
            last_lines = '...' + buf.read()

        if self.description:
            message = self.description
        else:
            message = "Command \"%s\" failed" % (self.command, )

        if self.client is None:
            self.client = Client(dsn=self.dsn)

        self.client.captureMessage(message,
                                   data={
                                       'logger': self.logger,
                                   },
                                   extra={
                                       'command': self.command,
                                       'exit_status': exit_status,
                                       'last_lines': last_lines,
                                   },
                                   time_spent=elapsed)
Exemple #25
0
class Sentry:
    """A simple cog for bug reports."""
    def __init__(self, liara):
        self.liara = liara
        self.settings = dataIO.load_json('sentry')
        if 'dsn' not in self.settings:
            self.settings['dsn'] = None
        self.client = SentryClient(site=liara.user.id)

    async def on_command_error(self, exception, context):
        if isinstance(exception, commands_errors.MissingRequiredArgument):
            return
        if isinstance(exception, commands_errors.CommandNotFound):
            return
        if self.settings['dsn'] is None:
            return
        try:
            self.client.set_dsn(self.settings['dsn'])
        except InvalidDsn:
            self.settings['dsn'] = None
            self.client.set_dsn(None)

        _exception = exception.original
        message = context.message
        self.client.user_context({'id': message.author.id})
        # noinspection PyBroadException
        try:
            raise _exception
        except:
            self.client.captureException(data={'message': message.content},
                                         extra={
                                             'server_id': message.server.id,
                                             'channel_id': message.channel.id,
                                             'message_id': message.id
                                         })

    @commands.command()
    @checks.is_owner()
    async def set_sentry(self, dsn=None):
        """Sets the DSN for Sentry."""
        try:
            self.client.set_dsn(dsn)
        except InvalidDsn:
            await self.liara.say('That DSN is invalid.')
            return
        self.settings['dsn'] = dsn
        if dsn is None:
            await self.liara.say('DSN cleared.')
        else:
            await self.liara.say(
                'DSN successfully set! All your exceptions will now be logged to Sentry.'
            )
            self.client.captureMessage('Hello, world!')
Exemple #26
0
    def connect_sentry(result):
        pillar_data = __salt__['pillar.raw']()
        grains = __salt__['grains.items']()
        tags = {}
        if 'tags' in pillar_data['raven']:
            for tag in pillar_data['raven']['tags']:
                tags[tag] = grains[tag]
        global_extra_data = {'pillar': pillar_data, 'grains': grains}
        global_data = {'platform': 'python', 'level': 'error'}

        servers = []
        try:
            for server in pillar_data['raven']['servers']:
                servers.append(server + '/api/store/')
            client = Client(
                servers=servers,
                public_key=pillar_data['raven']['public_key'],
                secret_key=pillar_data['raven']['secret_key'],
                project=pillar_data['raven']['project'],
            )
        except KeyError as missing_key:
            logger.error("Sentry returner need config '%s' in pillar",
                         missing_key)
        else:
            try:
                if isinstance(result['return'], collections.Mapping):
                    for state, changes in result.get('return', {}).iteritems():
                        if changes.get('result', True):
                            continue
                        data = global_data
                        data['culprit'] = state.replace('_|-', ' ')
                        extra_data = global_extra_data
                        extra_data['result'] = changes
                        client.captureMessage(message=changes.get(
                            'comment', 'No comment supplied'),
                                              data=data,
                                              extra=extra_data,
                                              tags=tags)
                else:
                    data = global_data
                    data['culprit'] = result['fun']
                    extra_data = global_extra_data
                    extra_data['result'] = result
                    message = "\n".join(result['return'])
                    client.captureMessage(message=message,
                                          data=data,
                                          extra=extra_data,
                                          tags=tags)
            except Exception as err:
                logger.error("Can't send message to sentry: %s",
                             err,
                             exc_info=True)
    def connect_sentry(result):
        pillar_data = __salt__['pillar.raw']()
        grains = __salt__['grains.items']()
        tags = {}
        if 'tags' in pillar_data['raven']:
            for tag in pillar_data['raven']['tags']:
                tags[tag] = grains[tag]
        global_extra_data = {
            'pillar': pillar_data,
            'grains': grains
        }
        global_data = {
            'platform': 'python',
            'level': 'error'
        }


        servers = []
        try:
            for server in pillar_data['raven']['servers']:
                servers.append(server + '/api/store/')
            client = Client(
                servers=servers,
                public_key=pillar_data['raven']['public_key'],
                secret_key=pillar_data['raven']['secret_key'],
                project=pillar_data['raven']['project'],
            )
        except KeyError as missing_key:
            logger.error("Sentry returner need config '%s' in pillar",
                         missing_key)
        else:
            try:
                if isinstance(result['return'], collections.Mapping):
                    for state, changes in result.get('return', {}).iteritems():
                        if changes.get('result', True):
                            continue
                        data = global_data
                        data['culprit'] = state.replace('_|-',' ')
                        extra_data = global_extra_data
                        extra_data['result'] = changes
                        client.captureMessage(message=changes.get('comment', 'No comment supplied'), data=data, extra=extra_data, tags=tags)
                else:
                    data = global_data
                    data['culprit'] = result['fun']
                    extra_data = global_extra_data
                    extra_data['result'] = result
                    message = "\n".join(result['return'])
                    client.captureMessage(message=message, data=data, extra=extra_data, tags=tags)
            except Exception as err:
                logger.error("Can't send message to sentry: %s", err,
                             exc_info=True)
Exemple #28
0
class SailerLogger:
    def __init__(self) -> None:
        self.client = Client(
            'https://*****:*****@sentry.io/244780')

    def info(self, message):
        self.client.captureMessage(message, level='info')

    def error(self, message):
        self.client.captureMessage(message, level='error')

    @property
    def sentry(self):
        return self.client
Exemple #29
0
    def test_basic(self, send_remote):
        send_remote.side_effect = self.sendRemote
        client = Client(
            dsn='http://%s:%s@localhost:8000/%s' %
            (self.pk.public_key, self.pk.secret_key, self.pk.project_id))

        with self.tasks():
            client.captureMessage(message='foo')

        assert send_remote.call_count is 1
        assert Group.objects.count() == 1
        group = Group.objects.get()
        assert group.event_set.count() == 1
        instance = group.event_set.get()
        assert instance.data['sentry.interfaces.Message']['message'] == 'foo'
 def send_sentry(message, result=None):
     logger.trace("Sending some data to Sentry")
     pillar_data = __salt__['pillar.data']()
     sentry_data = {
         'result': result,
         'returned': ret,
         'pillar': pillar_data,
         'grains': __salt__['grains.items']()
     }
     try:
         client = Client(pillar_data['sentry_dsn'])
         client.captureMessage(message, extra=sentry_data)
     except Exception, err:
         logger.error("Can't send message '%s' extra '%s' to sentry: %s",
                      message, sentry_data, err)
Exemple #31
0
            def __call__(instance, arg):
                try:
                    data = json.loads(arg)
                    attempt = int(data.pop(u'__attempt', 0))
                except (TypeError, ValueError) as e:
                    return instance.f(arg)

                try:
                    return instance.f(data)
                except BeanstalkRetryError as e:
                    try:
                        job = settings.BEANSTALK_JOB_NAME % {
                            u'app': instance.app,
                            u'job': instance.__name__,
                        }
                    except AttributeError:
                        job = u"{}.{}".format(instance.app, instance.__name__)

                    if attempt < self.max_retries:
                        if self.warn_after is not None and attempt == (self.warn_after - 1):
                            msg = u"Approaching max retry attempts for {}.".format(job)
                            warn_data = {
                                'extra': {
                                    'Job': job,
                                    'Attempt number': attempt,
                                    'Warn after': self.warn_after,
                                    'Max retries': self.max_retries,
                                    'Job data': data,
                                }
                            }
                            raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn'])
                            raven_client.captureMessage(msg, data=warn_data, stack=True, level=logging.WARN)

                        data[u'__attempt'] = attempt + 1

                        beanstalk_client = BeanstalkClient()
                        beanstalk_client.call(job, json.dumps(data), delay=(2 ** attempt), priority=self.priority, ttr=self.ttr)
                    else:
                        msg = u"Exceeded max retry attempts for {}.".format(job)
                        error_data = e.data if e.data is not None else {}
                        raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn'])
                        raven_client.captureMessage(msg, data=error_data, stack=True)

                        if e.should_email:
                            send_mail(e.email_subject, e.email_body, settings.DEFAULT_FROM_EMAIL, [e.email_address], fail_silently=False)
                except Exception as e:
                    raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn'])
                    raven_client.captureException()
Exemple #32
0
def process_varaamo_libraries():
    """
    Find varaamo libraries' Units from the db,
    ask their data from kirjastot.fi and
    process resulting opening hours if found
    into their Unit object

    Asks the span of opening hours from get_time_range

    TODO: Libraries in Helmet system with resources need more reliable identifier

    :return: None
    """
    in_namespaces = Q(identifiers__namespace="helmet") | Q(
        identifiers__namespace="kirjastot.fi")
    varaamo_units = Unit.objects.filter(in_namespaces).exclude(
        resources__isnull=True)

    start, end = get_time_range()
    problems = []
    for varaamo_unit in varaamo_units:
        data = timetable_fetcher(varaamo_unit, start, end)
        if data:
            try:
                with transaction.atomic():
                    varaamo_unit.periods.all().delete()
                    process_periods(data, varaamo_unit)
            except Exception as e:
                print("Problem in processing data of library ", varaamo_unit,
                      e)
                problems.append(" ".join([
                    "Problem in processing data of library ",
                    str(varaamo_unit),
                    str(e)
                ]))
        else:
            print("Failed data fetch on library: ", varaamo_unit)
            problems.append(" ".join(
                ["Failed data fetch on library: ",
                 str(varaamo_unit)]))

    try:
        if problems and settings.RAVEN_DSN:
            # Report problems to Raven/Sentry
            client = Client(settings.RAVEN_DSN)
            client.captureMessage("\n".join(problems))
    except AttributeError:
        pass
Exemple #33
0
def main():
    root = logging.getLogger('sentry.errors')
    root.setLevel(logging.DEBUG)
    root.addHandler(logging.StreamHandler())

    dsn = ' '.join(sys.argv[2:])
    if not (dsn or os.environ.get('SENTRY_DSN')):
        print "Error: No configuration detected!"
        print "You must either pass a DSN to the command, or set the SENTRY_DSN environment variable."
        sys.exit(1)

    print "Using DSN configuration:"
    print " ", dsn
    print

    client = Client(dsn)

    print "Client configuration:"
    for k in ('servers', 'project', 'public_key', 'secret_key'):
        print '  %-15s: %s' % (k, getattr(client, k))
    print

    if not all([client.servers, client.project, client.public_key, client.secret_key]):
        print "Error: All values must be set!"
        sys.exit(1)

    print 'Sending a test message...',
    ident = client.get_ident(client.captureMessage('This is a test message generated using ``raven test``'))
    print 'success!'
    print
    print 'The test message can be viewed at the following URL:'
    url = client.servers[0].split('/api/store/', 1)[0]
    print '  %s/%s/search/?q=%s' % (url, client.project, ident)
Exemple #34
0
    def __call__(self, request, *args, **kwargs):
        client = Client(settings.RAVEN_CONFIG['dsn'])
        traceback.print_exc()

        if 'exception' in kwargs:
            client.captureException(kwargs['exception'])
        else:
            client.captureMessage(self.message, level='error')

        return http.HttpResponse(status=self.status,
                                 content=render_to_string(
                                     context=dict(
                                         status=self.status,
                                         message=self.message,
                                     ),
                                     template_name=f'error.html'))
Exemple #35
0
    def test_basic(self, send_remote):
        send_remote.side_effect = self.sendRemote
        client = Client(
            dsn='http://%s:%s@localhost:8000/%s' % (
                self.pk.public_key, self.pk.secret_key, self.pk.project_id)
        )

        with self.tasks():
            client.captureMessage(message='foo')

        assert send_remote.call_count is 1
        assert Group.objects.count() == 1
        group = Group.objects.get()
        assert group.event_set.count() == 1
        instance = group.event_set.get()
        assert instance.data['sentry.interfaces.Message']['message'] == 'foo'
Exemple #36
0
def main():
    root = logging.getLogger('sentry.errors')
    root.setLevel(logging.DEBUG)
    root.addHandler(logging.StreamHandler())

    parser = OptionParser()
    parser.add_option("--data", action="callback", callback=store_json,
                      type="string", nargs=1, dest="data")
    (opts, args) = parser.parse_args()

    dsn = ' '.join(args[1:]) or os.environ.get('SENTRY_DSN')
    if not dsn:
        print "Error: No configuration detected!"
        print "You must either pass a DSN to the command, or set the SENTRY_DSN environment variable."
        sys.exit(1)

    print "Using DSN configuration:"
    print " ", dsn
    print

    client = Client(dsn, include_paths=['raven'])

    print "Client configuration:"
    for k in ('servers', 'project', 'public_key', 'secret_key'):
        print '  %-15s: %s' % (k, getattr(client, k))
    print

    if not all([client.servers, client.project, client.public_key, client.secret_key]):
        print "Error: All values must be set!"
        sys.exit(1)

    print 'Sending a test message...',
    ident = client.get_ident(client.captureMessage(
        message='This is a test message generated using ``raven test``',
        data=opts.data or {
            'culprit': 'raven.scripts.runner',
            'logger': 'raven.test',
            'sentry.interfaces.Http': {
                'method': 'GET',
                'url': 'http://example.com',
            }
        },
        level=logging.INFO,
        stack=True,
        extra={
            'user': pwd.getpwuid(os.geteuid())[0],
            'loadavg': os.getloadavg(),
        }
    ))

    if client.state.did_fail():
        print 'error!'
        return False

    print 'success!'
    print
    print 'The test message can be viewed at the following URL:'
    url = client.servers[0].split('/api/store/', 1)[0]
    print '  %s/%s/search/?q=%s' % (url, client.project, ident)
Exemple #37
0
    async def senderror(self, ctx, *, errormessage):
        """Send's the error!"""

        client = Client('')
        channel = ctx.message.channel
        sender = ctx.message.author.display_name
        server = ctx.message.server.name
        strchannel = str(channel)
        strsender = str(sender)
        strserver = str(server)

        await self.bot.say("Processing Report")
        client.captureMessage("User " + strsender +
                              " Has sent an error from the server " +
                              strserver + ", in the channel " + strchannel +
                              ". Their message: " + errormessage)
        await self.bot.say("Message Sent!")
def send_android_error_report(user_id, error_report):
    # Encountered a corrupted (write error) error report upload on Apr 30 2017, adding error sentry
    # so that we get *some* report of the error occuring but also delete that file from the device.
    with ErrorSentry(SENTRY_DSN, sentry_client_kwargs={'transport': HTTPTransport}):
        #get all non-empty lines in the error report
        contents = [line for line in error_report.splitlines() if line.strip() ]
        
        # the first line contains a unix millisecond timestamp, construct a datetime
        # The printed value in the crash report is in UTC
        try: #Beiwe version greater than 4
            timestamp = datetime.fromtimestamp(float(contents[0]) / 1000)
            contents.pop(0) #remove timestamp from message text
        except ValueError: #Beiwe version 4
            timestamp = datetime.fromtimestamp(float(request.values['file_name'].split("_")[1]) / 1000)
    
        device_identifiers = contents[0].split(',')
        contents.pop(0)  # remove device identifiers from message text
    
        # Insert the actual error message as the first line
        report_title = contents[0].split(":", 1)[1].strip()
        if "}" in report_title:  #cut title at end of file name
            report_title = report_title.split("}", 1)[0] + "}"
        contents.insert(0, "Android Error: %s" % report_title)
        
        # the second line contains all the identifiers. Clean it up and parse into a dictionary.
        device_identifiers = {ID.strip().split(":",1)[0] : ID.strip().split(":",1)[1]
                                              for ID in device_identifiers}
    
        #get a useful timestamp...
        eastern_time = timestamp.replace(tzinfo=tz.gettz('UTC')).astimezone(tz.gettz('America/New_York'))
        
        #construct some useful tags for this error report, add all identifiers as tags.
        tags = {"Android_Error": "android error",
                "user_id":user_id,
                "date": str(timestamp.date()),
                "time": str(timestamp).split(" ")[1].split(".")[0],
                "eastern_date": str(eastern_time.date()),
                "eastern_time": str(eastern_time).split(" ")[1].split(".")[0]
                }
        tags.update(device_identifiers)
        
        sentry_client = SentryClient(dsn=SENTRY_DSN,
                                     tags=tags,
                                     transport=HTTPTransport )
    
        sentry_client.captureMessage("\n".join(contents))
def main():
    root = logging.getLogger('sentry.errors')
    root.setLevel(logging.DEBUG)
    root.addHandler(logging.StreamHandler())

    parser = OptionParser(version=get_version())
    parser.add_option("--tags",
                      type="string",
                      nargs=1,
                      dest="tags",
                      default={},
                      action="callback",
                      callback=store_json)
    parser.add_option("--dsn")
    opts, args = parser.parse_args()

    dsn = opts.dsn or os.environ.get('SENTRY_DSN')
    if not dsn:
        logging.error(
            "Error: No configuration detected!\n"
            "You must either pass a DSN to the command, or set the SENTRY_DSN environment variable."
        )
        return 1

    client = Client(dsn,
                    include_paths=['raven'],
                    install_sys_hook=False,
                    install_logging_hook=False,
                    string_max_length=100000,
                    transport=HTTPTransport)

    input = sys.stdin.readline().strip()
    try:
        info = parse_harakiri_line(input)
        info['env'].update(os.environ)

        client.captureMessage(message='uwsgi harakiri on worker',
                              data=dict(request=info,
                                        logger='uwsgi.log-alarm.harakiri'),
                              level=logging.FATAL,
                              stack=False,
                              tags=opts.tags)

    except ValueError:
        logging.exception('Cannot parse STDIN')
        return 1
Exemple #40
0
def log_sentry_error(msg):
    try:
        client = Client(settings.RAVEN_CONFIG['dsn'])
    except (AttributeError, KeyError):
        # If RAVEN_CONFIG isn't set, we can't log the error.
        return

    return client.captureMessage(msg)
Exemple #41
0
def log_sentry_error(msg):
    try:
        client = Client(settings.RAVEN_CONFIG['dsn'])
    except (AttributeError, KeyError):
        # If RAVEN_CONFIG isn't set, we can't log the error.
        return

    return client.captureMessage(msg)
Exemple #42
0
 def sentry(msg):
 
     t_client = Client(
         # This is the secret key
         dsn=keyring[5],
         
         # This will appear as the host name in alerts
         name='Twitter',
         
         ignore_exceptions = [
                 'Http404',
                 'django.exceptions.*',
                 TypeError,
                 ValueError,
                 ]
         )
     t_client.captureMessage(msg)
Exemple #43
0
    def report_fail(self, exit_status, last_lines_stdout, last_lines_stderr, elapsed):
        if self.dsn is None:
            return

        message = 'Command "%s" failed' % (self.command,)

        client = Client(dsn=self.dsn, string_max_length=-1)

        client.captureMessage(
            message,
            data={"logger": "cron"},
            extra={
                "command": self.command,
                "exit_status": exit_status,
                "last_lines_stdout": last_lines_stdout,
                "last_lines_stderr": last_lines_stderr,
            },
            time_spent=elapsed,
        )
Exemple #44
0
class SentryAlerter(AbstractAlerterPlugin):
    def __init__(self, config):
        self.client = Client(
            config['dsn'],
            auto_log_stacks=False,
            enable_breadcrumbs=False,
        )
        self.config = config

    def handle_alert(self, node, match):
        name = match.result['name']

        if name.startswith('pack'):
            try:
                _, pack, query = name.split(
                    current_app.config['DOORMAN_PACK_DELIMITER'])
            except ValueError:
                pack, query = None, name
        else:
            pack, query = None, name

        message = match.rule.template.safe_substitute(match.result['columns'],
                                                      **node)

        self.client.captureMessage(
            message=message.rstrip(),
            data={'logger': current_app.name},
            extra={
                'action':
                match.result['action'],
                'columns':
                match.result['columns'],
                'timestamp':
                match.result['timestamp'].strftime('%Y-%m-%d %H:%M:%S'),
                'node':
                node,
            },
            tags={
                'host_identifier': node.get('host_identifier'),
                'pack': pack,
                'query': query,
            },
        )
Exemple #45
0
def process_varaamo_libraries():
    """
    Find varaamo libraries' Units from the db,
    ask their data from kirjastot.fi and
    process resulting opening hours if found
    into their Unit object

    Asks the span of opening hours from get_time_range

    TODO: Libraries in Helmet system with resources need more reliable identifier

    :return: None
    """
    varaamo_units = Unit.objects.filter(identifiers__namespace=KIRKANTA_NAMESPACE)

    start, end = get_time_range()
    problems = []
    for varaamo_unit in varaamo_units:
        data = timetable_fetcher(varaamo_unit, start, end)
        if data:
            try:
                with transaction.atomic():
                    varaamo_unit.periods.all().delete()
                    process_periods(data, varaamo_unit)
                    varaamo_unit.update_opening_hours()
            except Exception as e:
                import traceback
                print("Problem in processing data of library ", varaamo_unit, traceback.format_exc())
                problems.append(" ".join(["Problem in processing data of library ", str(varaamo_unit), str(e)]))
            if varaamo_unit.data_source_hours != IMPORTER_NAME:
                varaamo_unit.data_source_hours = IMPORTER_NAME
                varaamo_unit.save()
        else:
            print("Failed data fetch on library: ", varaamo_unit)
            problems.append(" ".join(["Failed data fetch on library: ", str(varaamo_unit)]))

    try:
        if problems and settings.RAVEN_DSN:
            # Report problems to Raven/Sentry
            client = Client(settings.RAVEN_DSN)
            client.captureMessage("\n".join(problems))
    except AttributeError:
        pass
Exemple #46
0
def main():
    root = logging.getLogger("sentry.errors")
    root.setLevel(logging.DEBUG)
    root.addHandler(logging.StreamHandler())

    parser = OptionParser()
    parser.add_option("--data", action="callback", callback=store_json, type="string", nargs=1, dest="data")
    (opts, args) = parser.parse_args()

    dsn = " ".join(args[1:]) or os.environ.get("SENTRY_DSN")
    if not dsn:
        print("Error: No configuration detected!")
        print("You must either pass a DSN to the command, or set the SENTRY_DSN environment variable.")
        sys.exit(1)

    print("Using DSN configuration:")
    print(" ", dsn)
    print()

    client = Client(dsn, include_paths=["raven"])

    print("Client configuration:")
    for k in ("servers", "project", "public_key", "secret_key"):
        print("  %-15s: %s" % (k, getattr(client, k)))
    print()

    if not all([client.servers, client.project, client.public_key, client.secret_key]):
        print("Error: All values must be set!")
        sys.exit(1)

    print("Sending a test message...", end=" ")
    ident = client.get_ident(
        client.captureMessage(
            message="This is a test message generated using ``raven test``",
            data=opts.data
            or {
                "culprit": "raven.scripts.runner",
                "logger": "raven.test",
                "sentry.interfaces.Http": {"method": "GET", "url": "http://example.com"},
            },
            level=logging.INFO,
            stack=True,
            extra={"user": pwd.getpwuid(os.geteuid())[0], "loadavg": os.getloadavg()},
        )
    )

    if client.state.did_fail():
        print("error!")
        return False

    print("success!")
    print()
    print("The test message can be viewed at the following URL:")
    url = client.servers[0].split("/api/store/", 1)[0]
    print("  %s/%s/search/?q=%s" % (url, client.project, ident))
Exemple #47
0
class SentryAlerter(AbstractAlerterPlugin):
    def __init__(self, config):
        self.client = Client(
            config['dsn'],
            auto_log_stacks=False,
            enable_breadcrumbs=False,
        )
        self.config = config

    def handle_alert(self, node, match):
        name = match.result['name']

        if name.startswith('pack'):
            try:
                _, pack, query = name.split(current_app.config['DOORMAN_PACK_DELIMITER'])
            except ValueError:
                pack, query = None, name
        else:
            pack, query = None, name

        message = match.rule.template.safe_substitute(
            match.result['columns'],
            **node
        )

        self.client.captureMessage(
            message=message.rstrip(),
            data={
                'logger': current_app.name
            },
            extra={
                'action': match.result['action'],
                'columns': match.result['columns'],
                'timestamp': match.result['timestamp'].strftime('%Y-%m-%d %H:%M:%S'),
                'node': node,
            },
            tags={
                'host_identifier': node.get('host_identifier'),
                'pack': pack,
                'query': query,
            },
        )
Exemple #48
0
def start():
    """start"""
    print "You are in a dark room."
    print "There is a door to your right and left."
    print "Which one do you take?"

    # set variables from config file
    SENTRY_TOKEN = read_config_file("config.ini")
    print "SENTRY_TOKEN is " + SENTRY_TOKEN
    client = Client(SENTRY_TOKEN)
    client.captureMessage("At the start of ex35")

    next = raw_input("> ")

    if next == "left":
        bear_room()
    elif next == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")
Exemple #49
0
    def report_fail(self, exit_status, last_lines_stdout, last_lines_stderr, elapsed):
        if self.dsn is None:
            return

        message = "Command \"%s\" failed" % (self.command,)

        client = Client(dsn=self.dsn, string_max_length=self.string_max_length)

        client.captureMessage(
            message,
            data={
                'logger': 'cron',
            },
            extra={
                'command': self.command,
                'exit_status': exit_status,
                'last_lines_stdout': last_lines_stdout,
                'last_lines_stderr': last_lines_stderr,
            },
            time_spent=elapsed
        )
Exemple #50
0
            def handle_missing_data(instance):
                job = instance.get_job_name()
                if instance.attempt < self.max_retries:
                    instance.beanstalk_data['attempt'] = instance.attempt + 1

                    backoff = 2 ** instance.attempt
                    beanstalk_client = BeanstalkClient()
                    beanstalk_client.call(job, json.dumps(instance.beanstalk_data), delay=backoff, ttr=self.ttr)
                else:
                    msg = u"Exceeded max retry attempts for {}.".format(job)
                    culprit = instance.get_sentry_culprit("handle_missing_data")
                    error_data = {
                        "culprit": culprit,
                        "extra": {
                            "Job name": job,
                            "Attempt number": instance.attempt,
                            "Beanstalk Data": instance.beanstalk_data,
                        }
                    }
                    raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn'])
                    raven_client.captureMessage(msg, data=error_data, stack=True)
    def update_device_settings(self, new_settings):
        for slug, value in new_settings.iteritems():
            setting = self.settings_dict.get(slug)

            if not setting:
                try:
                    app_setting = AppSetting.objects.get(service=self.service, slug=slug)

                    setting = DeviceSetting()
                    setting.app_setting = app_setting
                except AppSetting.DoesNotExist:
                    client = Client(dsn=settings.RAVEN_CONFIG["dsn"])
                    client.captureMessage(
                        "Invalid setting.  Does not exist for this app.",
                        extra={"token": self.token, "invalid_setting": {slug: value}},
                    )
                    continue

            setting.device = self
            setting.value = value
            setting.save()
Exemple #52
0
 def connect_sentry(message, result):
     pillar_data = __salt__['pillar.data']()
     sentry_data = {
         'result': result,
         'returned': ret,
         'pillar': pillar_data,
         'grains': __salt__['grains.items']()
     }
     servers = []
     for server in pillar_data['raven']['servers']:
         servers.append(server + '/api/store/')
     try:
         client = Client(
             servers=servers,
             public_key=pillar_data['raven']['public_key'],
             secret_key=pillar_data['raven']['secret_key'],
             project=pillar_data['raven']['project'],
         )
         client.captureMessage(ret['comment'], extra=sentry_data)
     except Exception, err:
         logger.error("Can't send message to sentry: %s", err, exc_info=True)
Exemple #53
0
    def report_resurrection(self, comeback: bool, ex: bool, printer: 'Printer', transition_history: List[Tuple[int, Dict]]) -> None:
        data: Dict[str, Any] = {
            'connected_at': self.connected_at,
            'comeback': comeback,
            'ex': ex,
        }

        seen = set()
        for (i, status) in transition_history:
            data[f'status{str(i).zfill(4)}'] = status
            ext_id = status.get('current_print_ts', None)
            if ext_id not in seen:
                print = Print.all_objects.filter(
                    printer=printer,
                    ext_id=ext_id
                ).first()
                if print:
                    data[f'print.{ext_id}'] = model_to_dict(print)
                    data[f'print.{ext_id}']['deleted'] = print.deleted
                seen.add(ext_id)

        data['printer.ext_id'] = printer.ext_id
        data['print.current'] = model_to_dict(
            printer.current_print) if printer.current_print else None

        from raven import Client
        c = Client(
            install_sys_hook=False,
            install_logging_hook=False,
            enable_breadcrumbs=False,
            tags={
                'printer_id': printer.id,
                'ext_id': ext_id,
                'connected_at': self.connected_at,
                'comeback': comeback,
                'ex': ex
            }
        )
        c.extra_context(data=data)
        c.captureMessage('Resurrected print')
Exemple #54
0
def process_varaamo_libraries():
    """
    Find varaamo libraries' Units from the db,
    ask their data from kirjastot.fi and
    process resulting opening hours if found
    into their Unit object

    Asks the span of opening hours from get_time_range

    TODO: Libraries in Helmet system with resources need more reliable identifier

    :return: None
    """
    varaamo_units = Unit.objects.filter(identifiers__namespace="kirjastot.fi").exclude(resources__isnull=True)

    start, end = get_time_range()
    problems = []
    for varaamo_unit in varaamo_units:
        data = timetable_fetcher(varaamo_unit, start, end)
        if data:
            try:
                with transaction.atomic():
                    varaamo_unit.periods.all().delete()
                    process_periods(data, varaamo_unit)
                    varaamo_unit.update_opening_hours()
            except Exception as e:
                print("Problem in processing data of library ", varaamo_unit, e)
                problems.append(" ".join(["Problem in processing data of library ", str(varaamo_unit), str(e)]))
        else:
            print("Failed data fetch on library: ", varaamo_unit)
            problems.append(" ".join(["Failed data fetch on library: ", str(varaamo_unit)]))

    try:
        if problems and settings.RAVEN_DSN:
            # Report problems to Raven/Sentry
            client = Client(settings.RAVEN_DSN)
            client.captureMessage("\n".join(problems))
    except AttributeError:
        pass
    def work(self):
        """children only: watch tubes for all jobs, start working"""
        try:

            while True:
                try:
                    # Reattempt Beanstalk connection if connection attempt fails or is dropped
                    beanstalk = connect_beanstalkd(server=self.beanstalk_server, port=self.beanstalk_port)
                    for job in self.jobs.keys():
                        beanstalk.watch(job)
                    beanstalk.ignore("default")

                    # Connected to Beanstalk queue, continually process jobs until an error occurs
                    # Each worker will have their own connection
                    db.connections["default"].close()
                    self.process_jobs(beanstalk)

                except (BeanstalkError, SocketError) as e:
                    msg = "Beanstalk connection error: " + str(e)
                    logger.info(msg)
                    client = Client(dsn=settings.RAVEN_CONFIG["dsn"])
                    client.captureMessage(msg, stack=True, level=logging.ERROR)

                    time.sleep(2.0)
                    logger.info("retrying Beanstalk connection...")
                except KeyboardInterrupt:
                    raise
                except Exception as e:
                    msg = "Beanstalk error: " + str(e)
                    client = Client(dsn=settings.RAVEN_CONFIG["dsn"])
                    client.captureMessage(msg, stack=True, level=logging.ERROR)

                    logger.info(msg)
                    time.sleep(2.0)
                    logger.info("retrying Beanstalk connection...")

        except KeyboardInterrupt:
            sys.exit(0)
Exemple #56
0
def main():
    root = logging.getLogger('sentry.errors')
    root.setLevel(logging.DEBUG)
    root.addHandler(logging.StreamHandler())

    dsn = ' '.join(sys.argv[2:])
    if not (dsn or os.environ.get('SENTRY_DSN')):
        print "Error: No configuration detected!"
        print "You must either pass a DSN to the command, or set the SENTRY_DSN environment variable."
        sys.exit(1)

    print "Using DSN configuration:"
    print " ", dsn
    print

    client = Client(dsn, include_paths=['raven'])

    print "Client configuration:"
    for k in ('servers', 'project', 'public_key', 'secret_key'):
        print '  %-15s: %s' % (k, getattr(client, k))
    print

    if not all([client.servers, client.project, client.public_key, client.secret_key]):
        print "Error: All values must be set!"
        sys.exit(1)

    print 'Sending a test message...',
    ident = client.get_ident(client.captureMessage(
        message='This is a test message generated using ``raven test``',
        data={
            'culprit': 'raven.scripts.runner',
            'logger': 'raven.test',
            'sentry.interfaces.Http': {
                'method': 'GET',
                'url': 'http://example.com',
            }
        },
        stack=True,
        extra={
            'user': os.getlogin(),
            'loadavg': os.getloadavg(),
        }
    ))
    print 'success!'
    print
    print 'The test message can be viewed at the following URL:'
    url = client.servers[0].split('/api/store/', 1)[0]
    print '  %s/%s/search/?q=%s' % (url, client.project, ident)
Exemple #57
0
def main():
    root = logging.getLogger('sentry.errors')
    root.setLevel(logging.DEBUG)
    root.addHandler(logging.StreamHandler())

    parser = OptionParser()
    parser.add_option("--dsn", action="store", default = os.environ.get('SENTRY_DSN'))
    (opts, args) = parser.parse_args()

    if not opts.dsn:
        print "Error: No configuration detected!"
        print "You must either pass a DSN with --dsn or set the SENTRY_DSN environment variable."
        sys.exit(1)

    if not args:
        print "Error: no files specified!"
        print "You must pass at least one filename on the command line."
        sys.exit(1)

    client = Client(opts.dsn)
    client.string_max_length = None

    data = {
        'culprit': 'sentry_uploads.scripts.runner',
        'logger': 'sentry_uploads.test',
    }

    data.update({
        'sentry_uploads.interfaces.Uploads': {
            'files': [{
                'filename': os.path.basename(path),
                'data': read_encode(path),
            } for path in args],
        },
    })

    ident = client.get_ident(client.captureMessage(
        message = 'Upload of %s via sentry-upload script' % str(args),
        data = data,
        level = logging.INFO,
    ))

    if client.state.did_fail():
        print 'error!'
        return False

    print 'success!'
Exemple #58
0
class MongoopTrigger(BaseTrigger):

    def __init__(self, *args, **kwargs):
        super(MongoopTrigger, self).__init__(*args, **kwargs)

        self.client = Client(self.params['dns'])

    def op_nok(self, operations):
        """
        This is the most low-level method available to send a message to sentry.
        """
        try:
            for operation in self.operations:
                result = self.client.captureMessage(
                    message=self.params['message'].format(**operation),
                    data={'level': self.params.get('level', 'info')},
                    extra={'operation': operation}
                )
                logging.info('run :: {} :: {}'.format(self.name, result))
        except Exception as e:
            logging.error('unable to run :: {} :: {}'.format(self.name, e))
            return False
        else:
            return True