Skip to content

@TRACER.capture_method causes botocore response body of objects retrieved from S3 to be pre-exhausted #238

Closed
@paulalex

Description

@paulalex

Hi,

I am really loving powertools, its been really useful recently for me, although I have encountered an issue since starting to use it in a new serverless application, I am currently using it in a few others and it is working o.k there and I had not noticed this issue.

This may well be a 'User Error' and what I am experiencing may be desired or necessary behaviour and I can change the way my code functions so I can still decorate this method but I think it is worth raising this.

I have provided as much information as I can below.

When a method is decorated with @TRACER.capture_method and that method retrieves a file from S3 using boto3 and returns the S3 object\dictionary then the botocore.response.StreamingBody object is already read meaning there is no data to be read anymore.

To add tracing to a method that calls S3 to retrieve a CSV file using boto3, has affected me in that since adding powertools tracing retrieved CSV files had no data when converted to data frame yet the response body was populated and files retrieved from S3.

Expected Behavior

Method is decorated and objects stream data is not read.

Current Behavior

See the following code snippet which works if the decorator is removed:

@TRACER.capture_method
def load_file_from_s3(bucket_name, key):
    try:
        obj = s3_client.get_object(Bucket=bucket_name, Key=key)        
    except ClientError as exc:
        if exc.response["Error"]["Code"] != "404":
            raise exc
    
    return obj

Environment

  • Powertools version used:
    Latest

How to enable debug mode**

{
    "level": "ERROR",
    "location": "generate_single_shape:186",
    "message": "No columns to parse from file",
    "timestamp": "2020-12-09 18:41:03,474",
    "service": "",
    "sampling_rate": 0,
    "cold_start": true,
    "function_name": "",
    "function_memory_size": "128",
    "function_arn": "arn:aws:lambda:eu-west-1:",
    "function_request_id": "2b74000d-10a6-48a9-8858-822f1f1f01e5",
    "exception": "Traceback (most recent call last):\n  File \"/var/task/src/x/controller.py\", line 166, in generate_single_shape\n    df_shape = get_shape_dataframe(int(shape_number), \"shape\")\n  File \"/var/task/src/x/controller.py\", line 68, in get_shape_dataframe\n    df = service.s3_shape_obj_to_pandas(file_obj, columns)\n  File \"/var/task/src/forecasting/shape_tool_service.py\", line 85, in s3_shape_obj_to_pandas\n    df = pd.read_csv(csv_file, usecols=columns, index_col=0, skiprows=2)\n  File \"/opt/python/pandas/io/parsers.py\", line 688, in read_csv\n    return _read(filepath_or_buffer, kwds)\n  File \"/opt/python/pandas/io/parsers.py\", line 454, in _read\n    parser = TextFileReader(fp_or_buf, **kwds)\n  File \"/opt/python/pandas/io/parsers.py\", line 948, in __init__\n    self._make_engine(self.engine)\n  File \"/opt/python/pandas/io/parsers.py\", line 1180, in _make_engine\n    self._engine = CParserWrapper(self.f, **self.options)\n  File \"/opt/python/pandas/io/parsers.py\", line 2010, in __init__\n    self._reader = parsers.TextReader(src, **kwds)\n  File \"pandas/_libs/parsers.pyx\", line 540, in pandas._libs.parsers.TextReader.__cinit__\npandas.errors.EmptyDataError: No columns to parse from file",
    "xray_trace_id": "1-5fd11a3a-75ee6c8d49267424492ea927"
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions