0

错误:

    send: b'{"specversion": "1.0", "logEntryBatches": [{"entries": [{"data": "{\\"hello\\": \\"oracle\\", \\"as\\": \\"aaa\\"}", "id": "ocid1.test.oc1..jkhjkhh23423fd", "time": "2021-04-01T12:19:28.416000Z"}], "source": "EXAMPLE-source-Value", "type": "remediationLogs", "defaultlogentrytime": "2021-04-01T12:19:28.416000Z"}]}'
reply: 'HTTP/1.1 400 Bad Request\r\n'
header: Date: Fri, 02 Apr 2021 07:39:16 GMT
header: opc-request-id: ER6S6HDVTNWUOKCJ7XXZ/OpcRequestIdExample/770899C2C7CA6ABA11D996CC57E8EE8F
header: Content-Type: application/json
header: Connection: close
header: Content-Length: 79
Traceback (most recent call last):
  File "tool.py", line 45, in <module>
    put_logs_response = loggingingestion_client.put_logs(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/oci/loggingingestion/logging_client.py", line 172, in put_logs
    return self.base_client.call_api(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/oci/base_client.py", line 276, in call_api
    response = self.request(request)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/oci/base_client.py", line 388, in request
    self.raise_service_error(request, response)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/oci/base_client.py", line 553, in raise_service_error
    raise exceptions.ServiceError(
oci.exceptions.ServiceError: {'opc-request-id': 'ER6S6HDVTNWUOKCJ7XXZ/OpcRequestIdExample/770899C2C7CA6ABA11D996CC57E8EE8F', 'code': 'InvalidParameter', 'message': 'Unable to process JSON input', 'status': 400}

我正在尝试将 json 数据发送到 Oracle 日志,但出现上述错误。我正在使用 json.dumps(data) 将字典转换为字符串。请让我知道是否有任何可用的解决方法。

代码:

data = {'hello':'oracle', "as":"aaa"}
put_logs_response = loggingingestion_client.put_logs(
        log_id="ocid1.log.oc1.iad.<<Log OCID>>",
        put_logs_details=oci.loggingingestion.models.PutLogsDetails(
            specversion="1.0",
            log_entry_batches=[
                oci.loggingingestion.models.LogEntryBatch(
                    entries=[
                        oci.loggingingestion.models.LogEntry(
                            data= json.dumps(data),
                            id="ocid1.test.oc1..jkhjkhh23423fd",
                            time=datetime.strptime(
                                "2021-04-01T12:19:28.416Z",
                                "%Y-%m-%dT%H:%M:%S.%fZ"))],
                    source="EXAMPLE-source-Value",
                    type="Logs",
                    defaultlogentrytime=datetime.strptime(
                        "2021-04-01T12:19:28.416Z",
                        "%Y-%m-%dT%H:%M:%S.%fZ"))]),
        timestamp_opc_agent_processing=datetime.strptime(
            "2021-04-01T12:19:28.416Z",
            "%Y-%m-%dT%H:%M:%S.%fZ"),
        opc_agent_version="EXAMPLE-opcAgentVersion-Value",
        opc_request_id="ER6S6HDVTNWUOKCJ7XXZ/OpcRequestIdExample/")
4

3 回答 3

3

您的数据看起来不错,我认为问题在于时间精度超过毫秒。如果您及时松开尾随零,它应该可以正常工作。
将日期时间格式化为以毫秒为单位的字符串

https://docs.oracle.com/en-us/iaas/api/#/en/logging-dataplane/20200831/LogEntry/

于 2021-04-05T18:36:05.870 回答
3

此异常表明您的 JSON 输入中有InvalidParameter

oci.exceptions.ServiceError: {'opc-request-id': 'ER6S6HDVTNWUOKCJ7XXZ/OpcRequestIdExample/770899C2C7CA6ABA11D996CC57E8EE8F', 'code': 'InvalidParameter', 'message': 'Unable to process JSON input', 'status': 400}

InvalidParameter 是您的时间戳,即 date - 2021-04-01T12:19:28.416Z

根据Oracle 的文档,您需要在创建 LogEntry 时使用具有毫秒精度的 RFC3339 格式的日期时间字符串。

此代码片段来自oci-python-sdk - log_entry.py,但它没有像 Oracle 的文档那样提及毫秒精度。

@time.setter
    def time(self, time):
        """
        Sets the time of this LogEntry.
        Optional. The timestamp associated with the log entry. An RFC3339-formatted date-time string.
        If unspecified, defaults to PutLogsDetails.defaultlogentrytime.
        :param time: The time of this LogEntry.
        :type: datetime
        """
        self._time = time

此代码创建一个毫秒精度的 UTC RFC3339 投诉时间戳

from datetime import datetime
from datetime import timezone

current_utc_time_with_offset = datetime.now(timezone.utc).isoformat()
print(current_utc_time_with_offset)
#output 
2021-04-06T13:00:52.706040+00:00

current_utc_time_with_timezone = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
print(current_utc_time_with_timezone)
#output 
2021-04-06T13:09:10.053432Z

这个 Stack Overflow 问题值得一读:

ISO 8601 和 RFC 3339 日期格式有什么区别?

这篇文章也很有用:

了解关于软件工程中日期时间和时区格式的 RFC 3339

于 2021-04-06T01:45:25.607 回答
1

您的时间是 RFC3339,但精度超过毫秒

'{"specversion": "1.0", "logEntryBatches": [{"entries": [{"data": "{\"hello\": \"oracle\", \"as\": \"aaa\ “}”,“id”:“ocid1.test.oc1..jkhjkhh23423fd”,“时间”:“ 2021-04-01T12:19:28.416000Z ”}],“源”:“示例源值”, “类型”:“补救日志”,“默认日志入口时间”:“ 2021-04-01T12:19:28.416000Z ”}]}'

请参阅https://docs.oracle.com/en-us/iaas/api/#/en/logging-dataplane/20200831/LogEntry/

The timestamp associated with the log entry. An RFC3339-formatted date-time string with milliseconds precision. If unspecified, defaults to PutLogsDetails.defaultlogentrytime.
于 2021-04-06T17:45:54.500 回答