Example #1
0
async def test_span_ctx():
    app = BaseApplication(BaseConfig())
    lgr = app.logger

    assert ctx_span_get() is None
    with lgr.span_new() as span1:
        assert ctx_span_get() is span1
        with span1.new_child() as span2:
            assert ctx_span_get() is span2
        assert ctx_span_get() is span1
    assert ctx_span_get() is None
Example #2
0
    async def __aenter__(self) -> 'Connection':
        if self._db is None or self._db._pool is None:  # noqa
            raise UserWarning
        pspan = ctx_span_get()

        if pspan is None:
            span: OraSpan = self._db.app.logger.span_new(  # type: ignore
                OraSpan.NAME_ACQUIRE,
                cls=OraSpan,
            )
        else:
            span = pspan.new_child(  # type: ignore
                OraSpan.NAME_ACQUIRE,
                cls=OraSpan,
            )
        self._span = span
        span.set_name4adapter(self._db.app.logger.ADAPTER_PROMETHEUS,
                              OraSpan.P8S_NAME_ACQUIRE)
        self._ctx_token = ctx_span_set(span)
        span.start()
        try:
            # todo acquire_timeout
            self._conn = await self._db.loop.run_in_executor(
                None, self._db._pool.acquire)
            span.annotate(OraSpan.ANN_ACQUIRE, '')
        except BaseException as err:
            span.finish(exception=err)
            ctx_span_reset(self._ctx_token)
            raise
        else:
            return self._db._connection_factory(self._db, self._conn)
Example #3
0
    async def _health_handler_head(self, request: web.Request) -> web.Response:
        result = await self._healthcheck()

        if result["is_sick"]:
            raise web.HTTPInternalServerError()
        else:
            span = ctx_span_get()
            if span:
                span.skip()
        return web.Response(text='')
Example #4
0
    async def _health_handler_get(self, request: web.Request) -> web.Response:
        result = await self._healthcheck()
        headers = {"Content-Type": "application/json;charset=utf-8"}

        if result["is_sick"]:
            raise web.HTTPInternalServerError(text=default_json_encode(
                result, indent=4),
                                              headers=headers)
        else:
            span = ctx_span_get()
            if span:
                span.skip()

        return web.Response(text=default_json_encode(result, indent=4),
                            headers=headers)
Example #5
0
async def test_trap():
    app = BaseApplication(BaseConfig())
    lgr = app.logger

    class ExSpan(Span):
        pass

    assert ctx_span_get() is None

    with app.logger.capture_span(ExSpan) as trap:
        with pytest.raises(UserWarning):
            assert trap.span is None
        with lgr.span_new(name='t1') as span1:
            with span1.new_child(name='t2', cls=ExSpan):
                pass
    assert trap.span is not None
    assert trap.span.name == 't2'