Ejemplo n.º 1
0
 async def test_buggy_listener(self, loop, console, results):
     listener = MyBuggyListener()
     eventer = EventSender(console)
     eventer.add_listener(listener)
     await eventer.send_event("my_event")
     resp = await serialize(console)
     self.assertTrue("Bam" in resp)
Ejemplo n.º 2
0
    async def test_buggy_listener(self, loop, console, results):
        class MyListener(BaseListener):
            def on_my_event(self, **options):
                raise Exception("Bam")

        listener = MyListener()
        eventer = EventSender(console)
        eventer.add_listener(listener)
        await eventer.send_event("my_event")
        resp = await serialize(console)
        self.assertTrue("Bam" in resp)
Ejemplo n.º 3
0
    async def test_add_listener(self, loop, console, results):
        class MyListener(BaseListener):
            def __init__(self):
                self.fired = False
                self.value = None

            def on_my_event(self, **options):
                self.fired = True
                self.value = options["value"]

        listener = MyListener()
        eventer = EventSender(console)
        eventer.add_listener(listener)
        await eventer.send_event("my_event", value=42)
        await serialize(console)

        self.assertTrue(listener.fired)
        self.assertEqual(listener.value, 42)
Ejemplo n.º 4
0
class SessionTracer(TraceConfig):
    def __init__(self, loop, console, verbose, statsd):
        super().__init__(
            trace_config_ctx_factory=self._trace_config_ctx_factory)
        self.loop = loop
        self.console = console
        self.verbose = verbose
        self.eventer = EventSender(
            console,
            [
                StdoutListener(
                    verbose=self.verbose, console=self.console, loop=self.loop)
            ],
        )
        self.on_request_start.append(self._request_start)
        self.on_request_end.append(self._request_end)
        self.context = namedtuple("context", ["statsd"])
        self.context.statsd = statsd

    def _trace_config_ctx_factory(self, trace_request_ctx):
        return SimpleNamespace(trace_request_ctx=trace_request_ctx,
                               context=self.context)

    def add_listener(self, listener):
        return self.eventer.add_listener(listener)

    async def send_event(self, event, **options):
        await self.eventer.send_event(event, session=self, **options)

    async def _request_start(self, session, trace_config_ctx, params):
        if self.context.statsd:
            prefix = "molotov.%(hostname)s.%(method)s.%(host)s.%(path)s"
            data = {
                "method": params.method,
                "hostname": _HOST,
                "host": params.url.host,
                "path": params.url.path,
            }
            label = prefix % data
            trace_config_ctx.start = perf_counter()
            trace_config_ctx.label = label
            trace_config_ctx.data = data

    async def _request_end(self, session, trace_config_ctx, params):
        if self.context.statsd:
            duration = int((perf_counter() - trace_config_ctx.start) * 1000)
            self.context.statsd.timing(trace_config_ctx.label, value=duration)
            self.context.statsd.increment(trace_config_ctx.label + "." +
                                          str(params.response.status))
        await self.send_event(
            "response_received",
            response=params.response,
            request=params.response.request,
        )