0

I am trying to get JSON using get_service_graph() provided by AWS X-Ray Python SDK in AWS Lambda function. 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

However, when I passed StartTime and EndTime parameters, stack trace reports datetime type is not JSON serializable. I even tried the following way.

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

What's weird is, if EndTime is set as "2017-05-20 01:00:00", there is no error generated. Other than that, the same error occurred.

    {
      "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"
    }

I did try only use date, like datetime(2017, 5, 20). However, if I use two consecutive days as StartTime and EndTime, the runtime complains the interval can't be more than 6 hours. If I use same date, it only returns empty JSON. I don't know how to get granularity of get_service_graph().

I think Python SDK for AWS X-Ray might be premature, but I'd still like to seek help from someone who had the same experience. Thanks!

Michael_Zhang
  • 333
  • 2
  • 7

1 Answers1

0

the right way is using datetime(2017, 5, 20) not a string... but can you try using only date... without time? at least the AWS docs shows an example exactly like yours but only yyyy-mm-dd without time

UXDart
  • 2,500
  • 14
  • 12
  • I did try only use date. If I use two consecutive days as StartTime and EndTime, the runtime complains the interval can't be more than 6 hours. If I use same date, it only returns empty JSON. I don't know how to get granularity of get_service_graph(). – Michael_Zhang May 21 '17 at 00:15
  • try `int(datetime.datetime(2017,5,20,1,0,0).strftime("%s")) * 1000` or without the `* 1000` in JS it accepts epoch number datetime, maybe on python too – UXDart May 21 '17 at 00:23
  • the other format in JS that can be used is `Wed Dec 31 1969 16:00:00 GMT-0800 (PST)` – UXDart May 21 '17 at 00:24
  • You should be able to specify both date and time with second resolution. – Rohit Banga May 21 '17 at 10:16
  • I tried your solution, but seems like StartTime can't take int. { "errorType": "ParamValidationError", "errorMessage": "Parameter validation failed:\n Invalid type for parameter StartTime, value: 1495242000000, type: , valid types: , timestamp-string" } – Michael_Zhang May 21 '17 at 23:11
  • seems like a bug in python sdk, you are using the last version of boto3 right? – UXDart May 21 '17 at 23:20