Boto3 client.get_log_events() returns an empty list, but a list of log events is viewable in the AWS console

0

We have a server that runs this piece of code:

        region = settings.AWS_REGION
        client = boto3.client('logs', region_name=region)
        
        job_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
        response = client.get_log_events(
            logGroupName=f'/proprietary/log/group/name',
            logStreamName=job_id,
            startTime=0,
            startFromHead=False,  # the latest log events are returned first.
        )

The response is sometimes, but not always, empty. An example of an empty response is:

{'events': [], 'nextForwardToken': 'f/xxxxxxxx/s', 'nextBackwardToken': 'b/xxxxxxxx/s', 'ResponseMetadata': {'RequestId': 'xxxxxxxx', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'xxxxxxxx, 'content-type': 'application/x-amz-json-1.1', 'content-length': 'xxxxxxxx', 'date': 'Fri, 13 Oct 2023 14:55:41 GMT'}, 'RetryAttempts': 0}}

However, if I go to the log group and stream name associated with /proprietary/log/group/name and job_id, I do see a stream of results.

Other posts suggested running this code

list_resp = client.list_metrics()

but it's not available to run with the client

Another post suggested It's a permissions issue. But I'm using the same permissions to view the log stream in AWS console as I'm using to run this code.

Is the code wrong or is this a bug?

IsaacH
asked 7 months ago700 views
1 Answer
0

Hi,

if the nextForwardToken is not null, maybe you should try calling the get_log_events API again and pass it the token? According to the API doc https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs/client/get_log_events.html, "This operation can return empty results while there are more log events available through the token."

profile pictureAWS
Jsc
answered 7 months ago
profile picture
EXPERT
reviewed 7 months ago
  • Interesting! This is very helpful, thanks! I'm trying it and will accept if it works

  • So I've passing the nextForwardToken into the boto3 call several times in a row, more than 5 times, and still getting empty responses every time. It's unclear how many times I would have to loop through the while loop to get something. Furthermore, the while loop would just spin forever if there actually were no events.

  • I'm having a similar issue, where some of my logs streams can be returned just fine, but others, which have the same number of log entries, just return an empty array, [].

    EDIT1: Came back this morning after the weekend and now everything works. (?????) EDIT2: A solution that seems to work is adding 'startTime' and 'endTime'. Also I have no issues if I use 'startFromHead=True'. It seems to be a bug with 'startFromHead=False', see https://github.com/boto/boto3/issues/3718

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions