Пример #1
0
    def test_data_collector(self):
        scriptname = "foo"
        hostname = "bar"
        schema = "http"
        tags = {"baz": "qux"}
        collector = DataCollector(scriptname, hostname, schema, tags)
        assert collector.enabled is True
        assert collector.timers == set()
        assert collector.scriptname == scriptname
        assert collector.hostname == hostname
        assert collector.schema == schema
        assert collector.tags == tags
        assert collector.dt_start is None
        assert collector.started is False
        assert collector.elapsed is None
        with self.assertRaises(RuntimeError):
            collector.stop()

        collector.start()
        assert collector.started is True
        assert collector.elapsed is None
        with self.assertRaises(RuntimeError):
            collector.start()
        collector.stop()
        assert collector.elapsed is not None
Пример #2
0
    def push(self):
        """Pushes current context into local stack.
        """

        self.pynba = DataCollector(self._scriptname,
                                   self._hostname,
                                   self._schema,
                                   self.config.get('tags', {}))
        self.pynba.start()
        LOCAL_STACK.pynba = self.pynba
        self.resources = resource.getrusage(resource.RUSAGE_SELF)
Пример #3
0
    def __init__(self,
                 address,
                 hostname=None,
                 scriptname=None,
                 servername=None,
                 reporter=None,
                 tags=None,
                 autostart=True):
        self.reporter = reporter or self.default_reporter(address)
        self.collector = DataCollector(tags=tags or {})
        self.hostname = hostname or socket.gethostname()
        self.scriptname = scriptname or " ".join(sys.argv)
        self.servername = servername or socket.gethostname()
        self.resources = None
        self.ru_utime = None
        self.ru_stime = None

        if autostart:
            self.start()
Пример #4
0
    def __init__(self, address, hostname=None, scriptname=None,
                 servername=None, reporter=None, tags=None, autostart=True):
        self.reporter = reporter or self.default_reporter(address)
        self.collector = DataCollector(tags=tags or {})
        self.hostname = hostname or socket.gethostname()
        self.scriptname = scriptname or " ".join(sys.argv)
        self.servername = servername or socket.gethostname()
        self.resources = None
        self.ru_utime = None
        self.ru_stime = None

        if autostart:
            self.start()
Пример #5
0
    def test_linked_timer(self):
        collector = DataCollector()
        timer = collector.timer(foo='bar')

        assert timer in collector.timers
        cloned = timer.clone()
        assert cloned in collector.timers

        timer.delete()
        assert timer not in collector.timers
        assert cloned in collector.timers

        cloned.delete()
        assert cloned not in collector.timers

        timer2 = collector.timer(foo='bar')
        assert timer2 in collector.timers
        collector.flush()
        assert timer2 not in collector.timers
Пример #6
0
class ScriptMonitor(object):
    """Helper for monitoring standalone scripts.

    >>> monitor = ScriptMonitor(('127.0.0.1', 30002))
    >>> timer = monitor.timer(tag1='bar')
    >>> timer.start()
    >>> timer.stop()
    >>>
    >>> @monitor.timer(tag1='bar')
    >>> def baz():
    >>>     pass
    >>> baz()
    >>>
    >>> with monitor.timer(tag1='baz'):
    >>>     print "hello"
    >>>
    >>> monitor.send()
    """

    default_reporter = Reporter

    def __init__(self, address, hostname=None, scriptname=None,
                 servername=None, reporter=None, tags=None, autostart=True):
        self.reporter = reporter or self.default_reporter(address)
        self.collector = DataCollector(tags=tags or {})
        self.hostname = hostname or socket.gethostname()
        self.scriptname = scriptname or " ".join(sys.argv)
        self.servername = servername or socket.gethostname()
        self.resources = None
        self.ru_utime = None
        self.ru_stime = None

        if autostart:
            self.start()

    @property
    def started(self):
        return self.collector.started

    @property
    def elapsed(self):
        return self.collector.elapsed

    def start(self):
        """Starts monitoring.
        """
        self.collector.start()
        self.resources = resource.getrusage(resource.RUSAGE_SELF)
        return self

    def stop(self):
        """Stops current elapsed time and every attached timers.
        """
        self.collector.stop()
        usage = resource.getrusage(resource.RUSAGE_SELF)
        self.ru_utime = usage.ru_utime - self.resources.ru_utime
        self.ru_stime = usage.ru_stime - self.resources.ru_stime
        return self

    @property
    def tags(self):
        """Returns collector tags.
        """
        return self.collector.tags

    def timer(self, **kwargs):
        """Factory new timer.
        """
        return self.collector.timer(**kwargs)

    def flush(self):
        """Flushes timers.
        """
        self.collector.flush()
        return self

    def send(self):
        """Sends timers to server.
        """
        self.stop()
        timers = [timer for timer in self.collector.timers if timer.elapsed]
        document_size = self.collector.document_size
        memory_peak = self.collector.memory_peak
        ru_utime = self.ru_utime
        ru_stime = self.ru_stime

        self.reporter(
            self.servername,
            self.hostname,
            self.scriptname,
            self.collector.elapsed,
            timers,
            ru_utime=ru_utime,
            ru_stime=ru_stime,
            document_size=document_size,
            memory_peak=memory_peak,
            status=None,
            memory_footprint=None,
            schema=None,
            tags=self.collector.tags
        )

        self.flush()
Пример #7
0
class RequestContext(object):
    """
    A new instance will be created every new request.

    :param reporter: a :class:`Reporter` instance
    :param environ: the current WSGI environ mapping
    :param config: may have these keys:
                   ``prefix`` will prepend scriptname
    """

    def __init__(self, reporter, environ, **config):
        self.reporter = reporter

        #: config['prefix'] prepends the sent scriptname to pinba.
        #: config['tags'] prepends tags to pinba.
        self.config = config

        #: futur :class:`DataCollector`
        self.pynba = None
        #: will keep a snap of :func:`resource.getrusage`
        self.resources = None

        self._hostname = environ.get('SERVER_NAME', None)
        self._schema = environ.get('wsgi.url_scheme', None)
        self._scriptname = environ.get('PATH_INFO', '')
        self._servername = ''

    @property
    def scriptname(self):
        out = self.config.get('prefix', '')

        if self.pynba:
            pynba = self.pynba
            if pynba.scriptname:
                return out + self.pynba.scriptname
        return out + self._scriptname

    @property
    def hostname(self):
        if self.pynba:
            pynba = self.pynba
            if pynba.hostname:
                return pynba.hostname
        if self._hostname:
            return self._hostname

        return None

    @property
    def servername(self):
        return self._servername

    @property
    def tags(self):
        if self.pynba:
            pynba = self.pynba
            response = pynba.tags or {}
        else:
            response = self.config.get('tags', {})
        return response

    def push(self):
        """Pushes current context into local stack.
        """

        self.pynba = DataCollector(self._scriptname,
                                   self._hostname,
                                   self._schema,
                                   self.config.get('tags', {}))
        self.pynba.start()
        LOCAL_STACK.pynba = self.pynba
        self.resources = resource.getrusage(resource.RUSAGE_SELF)

    def pop(self):
        """Pops current context from local stack.
        """

        del LOCAL_STACK.pynba
        self.pynba = None
        self.resources = None

    def __enter__(self):
        """Opens current scope.
        """

        self.push()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        """Closes current scope.
        """

        self.flush()
        self.pop()

    def flush(self):
        """Flushes timers.

        Similar to the PHP ``pinba_flush()`` function.
        scriptname sent to pinba will be prepend by config['prefix']
        """

        if not self.pynba or not self.pynba.enabled:
            return

        self.pynba.stop()
        timers = [timer for timer in self.pynba.timers if timer.elapsed]
        document_size = self.pynba.document_size
        memory_peak = self.pynba.memory_peak
        usage = resource.getrusage(resource.RUSAGE_SELF)
        ru_utime = usage.ru_utime - self.resources.ru_utime
        ru_stime = usage.ru_stime - self.resources.ru_stime
        memory_footprint = self.pynba.memory_peak
        schema = self.pynba.schema

        self.reporter(
            self.servername,
            self.hostname,
            self.scriptname,
            self.pynba.elapsed,
            timers,
            ru_utime=ru_utime,
            ru_stime=ru_stime,
            document_size=document_size,
            memory_peak=memory_peak,
            memory_footprint=memory_footprint,
            schema=schema,
            tags=self.tags
        )

        self.pynba.flush()
Пример #8
0
 def test_collector(self):
     DataCollector().flush()
     assert self.handler.messages == {'debug': ['flush']}
Пример #9
0
class ScriptMonitor(object):
    """Helper for monitoring standalone scripts.

    >>> monitor = ScriptMonitor(('127.0.0.1', 30002))
    >>> timer = monitor.timer(tag1='bar')
    >>> timer.start()
    >>> timer.stop()
    >>>
    >>> @monitor.timer(tag1='bar')
    >>> def baz():
    >>>     pass
    >>> baz()
    >>>
    >>> with monitor.timer(tag1='baz'):
    >>>     print "hello"
    >>>
    >>> monitor.send()
    """

    default_reporter = Reporter

    def __init__(self,
                 address,
                 hostname=None,
                 scriptname=None,
                 servername=None,
                 reporter=None,
                 tags=None,
                 autostart=True):
        self.reporter = reporter or self.default_reporter(address)
        self.collector = DataCollector(tags=tags or {})
        self.hostname = hostname or socket.gethostname()
        self.scriptname = scriptname or " ".join(sys.argv)
        self.servername = servername or socket.gethostname()
        self.resources = None
        self.ru_utime = None
        self.ru_stime = None

        if autostart:
            self.start()

    @property
    def started(self):
        return self.collector.started

    @property
    def elapsed(self):
        return self.collector.elapsed

    def start(self):
        """Starts monitoring.
        """
        self.collector.start()
        self.resources = resource.getrusage(resource.RUSAGE_SELF)
        return self

    def stop(self):
        """Stops current elapsed time and every attached timers.
        """
        self.collector.stop()
        usage = resource.getrusage(resource.RUSAGE_SELF)
        self.ru_utime = usage.ru_utime - self.resources.ru_utime
        self.ru_stime = usage.ru_stime - self.resources.ru_stime
        return self

    @property
    def tags(self):
        """Returns collector tags.
        """
        return self.collector.tags

    def timer(self, **kwargs):
        """Factory new timer.
        """
        return self.collector.timer(**kwargs)

    def flush(self):
        """Flushes timers.
        """
        self.collector.flush()
        return self

    def send(self):
        """Sends timers to server.
        """
        self.stop()
        timers = [timer for timer in self.collector.timers if timer.elapsed]
        document_size = self.collector.document_size
        memory_peak = self.collector.memory_peak
        ru_utime = self.ru_utime
        ru_stime = self.ru_stime

        self.reporter(self.servername,
                      self.hostname,
                      self.scriptname,
                      self.collector.elapsed,
                      timers,
                      ru_utime=ru_utime,
                      ru_stime=ru_stime,
                      document_size=document_size,
                      memory_peak=memory_peak,
                      status=None,
                      memory_footprint=None,
                      schema=None,
                      tags=self.collector.tags)

        self.flush()