Ejemplo n.º 1
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)
Ejemplo n.º 2
0
 def _span_ann_format4zipkin(
     self, span: Span, kind: str, content: str, ts: float
 ) -> None:
     span.annotate4adapter(
         self.app.logger.ADAPTER_ZIPKIN,
         kind,
         default_json_encode({kind: content}),
         ts=ts,
     )
Ejemplo n.º 3
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: PgSpan = self._db.app.logger.span_new(  # type: ignore
                PgSpan.NAME_ACQUIRE,
                cls=PgSpan,
            )
        else:
            span = pspan.new_child(  # type: ignore
                PgSpan.NAME_ACQUIRE,
                cls=PgSpan,
            )
        self._span = span
        span.set_name4adapter(self._db.app.logger.ADAPTER_PROMETHEUS,
                              PgSpan.P8S_NAME_ACQUIRE)
        self._ctx_token = ctx_span_set(span)
        span.start()
        try:
            self._conn = await self._db._pool.acquire(  # noqa
                timeout=self._acquire_timeout)
            span.annotate(PgSpan.ANN_ACQUIRE, '')
        except BaseException as err:
            span.finish(exception=err)
            ctx_span_reset(self._ctx_token)
            raise
        else:
            pool_queue = self._db._pool._queue  # noqa
            pg_conn = self._conn._con  # noqa
            pid = pg_conn.get_server_pid()
            span.tag(PgSpan.TAG_POOL_MAX_SIZE, pool_queue.maxsize)
            span.tag(PgSpan.TAG_POOL_FREE_COUNT, pool_queue.qsize())
            span.annotate(PgSpan.ANN_PID, pid)
            span.annotate4adapter(
                self._db.app.logger.ADAPTER_ZIPKIN,
                PgSpan.ANN_PID,
                default_json_encode({'pid': str(pid)}),
            )

            self._pg_conn = self._db._connection_factory(self._db, self._conn)
            self._db._connections.append(self._pg_conn)  # noqa
            return self._pg_conn
Ejemplo n.º 4
0
def props2ann(properties: pika.spec.BasicProperties) -> str:
    anns: List[str] = []
    for prop in (
            'content_type',
            'content_encoding',
            'headers',
            'delivery_mode',
            'priority',
            'correlation_id',
            'reply_to',
            'expiration',
            'message_id',
            'timestamp',
            'type',
            'user_id',
            'app_id',
            'cluster_id',
    ):
        attr: Any = getattr(properties, prop)
        if attr is not None:
            anns.append('%s: %s' % (prop, default_json_encode(attr)))

    return '\n'.join(anns)
Ejemplo n.º 5
0
    async def __aenter__(self) -> 'asyncpg.transaction.Transaction':
        if self._conn.in_transaction:
            raise UserWarning('Transaction already started')

        pspan = ctx_span_get()
        if pspan is None:  # pragma: no cover
            raise UserWarning
        span: PgSpan = pspan.new_child(cls=PgSpan)  # type: ignore
        self._span = span
        self._ctx_token = ctx_span_set(span)
        span.start()

        try:
            span.annotate(PgSpan.ANN_PID, self._conn.pid)
            span.annotate4adapter(
                self._conn._db.app.logger.ADAPTER_ZIPKIN,
                PgSpan.ANN_PID,
                default_json_encode({'pid': str(self._conn.pid)}),
            )

            if self._xact_lock is not None:
                await self._xact_lock.acquire()

            async with self._conn._lock:
                self._tr = self._conn._conn.transaction(
                    isolation=self._isolation_level,
                    readonly=self._readonly,
                    deferrable=self._deferrable,
                )
                await self._tr.__aenter__()
                span.annotate(PgSpan.ANN_XACT_BEGIN, 'BEGIN')
        except BaseException as err:
            span.finish(exception=err)
            ctx_span_reset(self._ctx_token)
            raise
        return self._tr
Ejemplo n.º 6
0
 def _jsonb_encoder(value: JsonType) -> bytes:
     return b'\x01' + default_json_encode(value).encode('utf-8')
Ejemplo n.º 7
0
 def _json_encoder(value: JsonType) -> str:
     return default_json_encode(value)