Пример #1
0
 def __init__(
     self,
     request: BxJsonRpcRequest,
     node: "AbstractGatewayNode",
 ):
     if request.params is None:
         request.params = {
             rpc_constants.DETAILS_LEVEL_PARAMS_KEY: GatewayStatusDetailsLevel.SUMMARY.name
         }
     self._details_level = None
     super().__init__(request, node)
     self._json_encoder = EnhancedJSONEncoder()
     params = request.params
     assert isinstance(params, dict)
     self.params = params
Пример #2
0
class CustomFormatter(AbstractFormatter):
    encoder = EnhancedJSONEncoder()

    def format(self, record) -> str:
        log_record = {
            k: v
            for k, v in record.__dict__.items() if k not in BUILT_IN_ATTRS
            and k not in EXCLUDE_FROM_PLAIN_FORMATTING
        }
        if record.args and not hasattr(record.msg, "__dict__"):
            record.msg = self._handle_args(record)
            record.args = ()

        record.msg = "{}{}".format(
            self.encoder.encode(record.msg), ",".join({
                " {}={}".format(k, self.encoder.encode(v))
                for (k, v) in log_record.items() if k != "msg"
            }))
        if self.instance != self.NO_INSTANCE:
            record.instance = self.instance
        return super(CustomFormatter, self).format(record)

    def formatTime(self, record, datefmt=None) -> str:
        current_time = datetime.fromtimestamp(record.created)
        if datefmt:
            s = current_time.astimezone().strftime(datefmt)
        else:
            s = current_time.isoformat()
        return s
Пример #3
0
def _get_handler_fluentd(fluentd_host: Optional[str],
                         fluentd_tag_suffix: Optional[str],
                         max_queue_size: int,
                         loop: Optional[AbstractEventLoop] = None):
    assert fluentd_host is not None, "fluentd host name is missing"
    assert FluentHandler is not None, "fluentd handler is not installed"

    if ":" in fluentd_host:
        fluentd_host, fluentd_port = fluentd_host.split(":")
    else:
        fluentd_port = constants.FLUENTD_PORT
    if fluentd_tag_suffix:
        fluentd_tag = constants.FLUENTD_DEFAULT_TAG + "." + fluentd_tag_suffix
    else:
        fluentd_tag = constants.FLUENTD_DEFAULT_TAG
    return FluentHandler(
        fluentd_tag,
        host=fluentd_host,
        port=int(fluentd_port),
        buffer_overflow_handler=overflow_handler,
        nanosecond_precision=True,
        max_queue_size=max_queue_size,
        log_unhandled_exceptions=True,
        loop=loop,
        packer_kwargs={"default": EnhancedJSONEncoder().default}
    )
Пример #4
0
class CustomFormatter(AbstractFormatter):
    encoder = EnhancedJSONEncoder()

    def format(self, record) -> str:
        log_record = {
            k: v
            for k, v in record.__dict__.items() if k not in BUILT_IN_ATTRS
        }
        if record.args and not hasattr(record.msg, "__dict__"):
            log_record["msg"] = self._formatter(record.msg, record.args)
        else:
            log_record["msg"] = record.msg

        record.msg = "{}{}".format(
            self.encoder.encode(record.msg), ",".join({
                " {}={}".format(k, self.encoder.encode(v))
                for (k, v) in log_record.items() if k != "msg"
            }))
        record.instance = self.instance
        return super(CustomFormatter, self).format(record)

    def formatTime(self, record, datefmt=None) -> str:
        ct = datetime.fromtimestamp(record.created)
        if datefmt:
            s = ct.strftime(datefmt)
        else:
            s = ct.isoformat()
        return s
Пример #5
0
class GatewayStatusRpcRequest(AbstractRpcRequest["AbstractGatewayNode"]):
    help = {
        "params": f"Optional - {rpc_constants.DETAILS_LEVEL_PARAMS_KEY}: "
                  f"{list(GatewayStatusDetailsLevel.__members__.keys())}.",
        "description": "return status of the bloXroute Gateway"
    }

    def __init__(
        self,
        request: BxJsonRpcRequest,
        node: "AbstractGatewayNode",
    ):
        if request.params is None:
            request.params = {
                rpc_constants.DETAILS_LEVEL_PARAMS_KEY: GatewayStatusDetailsLevel.SUMMARY.name
            }
        self._details_level = None
        super().__init__(request, node)
        self._json_encoder = EnhancedJSONEncoder()
        params = request.params
        assert isinstance(params, dict)
        self.params = params

    def validate_params(self) -> None:
        super().validate_params()
        params = self.params
        assert isinstance(params, dict)
        if rpc_constants.DETAILS_LEVEL_PARAMS_KEY not in params:
            params[rpc_constants.DETAILS_LEVEL_PARAMS_KEY] = GatewayStatusDetailsLevel.SUMMARY.name

        self._details_level = params[rpc_constants.DETAILS_LEVEL_PARAMS_KEY].lower()

    async def process_request(self) -> JsonRpcResponse:
        loop = asyncio.get_event_loop()
        opts = self.node.opts
        diagnostics = await loop.run_in_executor(
            self.node.requester.thread_pool,
            status_log.get_diagnostics,
            opts.use_extensions,
            opts.source_version,
            opts.external_ip,
            opts.continent,
            opts.country,
            opts.should_update_source_version,
            self.node.account_id,
            self.node.quota_level
        )
        if self._details_level == GatewayStatusDetailsLevel.SUMMARY.name.lower():
            data = diagnostics.summary
        else:
            data = diagnostics
        result = self._json_encoder.as_dict(data)
        return self.ok(result)
Пример #6
0
 def format(self, record):
     return EnhancedJSONEncoder().as_dict(self._format_json(record))