【问题标题】:AWS X-Ray Python SDK get_service_graphAWS X-Ray Python 开发工具包 get_service_graph
【发布时间】:2017-10-20 19:54:12
【问题描述】:

我正在尝试使用 AWS X-Ray Python SDK 在 AWS Lambda 函数中提供的 get_service_graph() 获取 JSON。 reference link

import boto3
from datetime import datetime

def lambda_handler(event, context):
    client = boto3.client('xray')

    response1 = client.get_service_graph(
        StartTime=datetime(2017, 5, 20, 12, 0),
        EndTime=datetime(2017, 5, 20, 18, 0)
    )

    return response1

但是,当我传递 StartTime 和 EndTime 参数时,堆栈跟踪报告 datetime 类型不是 JSON 可序列化的。我什至尝试了以下方法。

response1 = client.get_service_graph(
        StartTime="2017-05-20 00:00:00",
        EndTime="2017-05-20 02:00:00"
)

奇怪的是,如果 EndTime 设置为“2017-05-20 01:00:00”,则不会产生错误。除此之外,发生了同样的错误。

    {
      "stackTrace": [
        [
          "/usr/lib64/python2.7/json/__init__.py",
          251,
          "dumps",
          "sort_keys=sort_keys, **kw).encode(obj)"
        ],
        [
          "/usr/lib64/python2.7/json/encoder.py",
          207,
          "encode",
          "chunks = self.iterencode(o, _one_shot=True)"
        ],
        [
          "/usr/lib64/python2.7/json/encoder.py",
          270,
          "iterencode",
          "return _iterencode(o, 0)"
        ],
        [
          "/var/runtime/awslambda/bootstrap.py",
          104,
          "decimal_serializer",
          "raise TypeError(repr(o) + \" is not JSON serializable\")"
        ]
      ],
      "errorType": "TypeError",
      "errorMessage": "datetime.datetime(2017, 5, 20, 1, 53, 13, tzinfo=tzlocal()) is not JSON serializable"
    }

我确实尝试过只使用日期,例如 datetime(2017, 5, 20)。但是,如果我使用连续两天作为 StartTime 和 EndTime,运行时会抱怨间隔不能超过 6 小时。如果我使用相同的日期,它只返回空 JSON。我不知道如何获取 get_service_graph() 的粒度。

我认为适用于 AWS X-Ray 的 Python 开发工具包可能为时过早,但我仍然想向有相同经验的人寻求帮助。谢谢!

【问题讨论】:

    标签: json datetime aws-lambda boto3 aws-xray


    【解决方案1】:

    正确的方法是使用datetime(2017, 5, 20) 而不是字符串...但是您可以尝试仅使用日期...没有时间吗?至少 AWS 文档显示了一个与您的完全一样的示例,但只有 yyyy-mm-dd 没有时间

    【讨论】:

    • 我确实尝试过只使用日期。如果我连续两天使用 StartTime 和 EndTime,运行时会抱怨间隔不能超过 6 小时。如果我使用相同的日期,它只会返回空 JSON。我不知道如何获取 get_service_graph() 的粒度。
    • 尝试 int(datetime.datetime(2017,5,20,1,0,0).strftime("%s")) * 1000 或在 JS 中不使用 * 1000 它接受纪元日期时间,也可能在 python 上
    • JS中另一种可以使用的格式是Wed Dec 31 1969 16:00:00 GMT-0800 (PST)
    • 您应该能够以秒分辨率指定日期和时间。
    • 我尝试了您的解决方案,但似乎 StartTime 无法采用 int。 { "errorType": "ParamValidationError", "errorMessage": "参数验证失败:\n 参数 StartTime 的类型无效,值:1495242000000,类型:,有效类型: , 时间戳字符串" }
    猜你喜欢
    • 1970-01-01
    • 2018-10-29
    • 2011-11-10
    • 2012-05-24
    • 2015-04-07
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多