简体   繁体   中英

SegmentNotFoundException in AWS Xray with Lambda

I am trying to write a Lambda function to copy files from one s3 bucket to another integrated with AWS Xray. Below is the code for Lambda function. I am getting the error

aws_xray_sdk.core.exceptions.exceptions.SegmentNotFoundException: cannot find the current segment/subsegment, please make sure you have a segment open

I have included the Aws xray SDK in my deployment package. Also, begin segment and end segment are included in the code. Please give a solution to this error.

import boto3
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch
patch(['boto3'])

client = boto3.client('s3')
s3 = boto3.resource('s3')
SourceBucket = 'bucket1'
DestBucket = 'bucket2'
list1=[];

def lambda_handler(event, context):
    response = client.list_objects(Bucket=SourceBucket)
    if 'Contents' in response:
        for item in response['Contents']:
            list1.append(item['Key']);
        put_object_into_s3()
        for name in list1:
            copy_source = {
                'Bucket': SourceBucket,
                'Key': name
            }
            response = s3.meta.client.copy(copy_source, DestBucket, name)

the context management for a Lambda environment would never throw a SegmentNotFoundException . If there is no active segment/subsegment in thread local storage, it constructs a segment based on environment variables set in Lambda container. See https://github.com/aws/aws-xray-sdk-python/blob/master/aws_xray_sdk/core/lambda_launcher.py#L79

The lambda context management will be used when an environment variable LAMBDA_TASK_ROOT is set. Are you using some tool to run your Lambda function locally or have you enabled your Lambda function's active tracing ?

If you have any top-level code (almost always a bad idea) that calls xray_recorder.configure then it will clear out the segment that Lambda creates, but not create a new & valid segment. If you also have environment variable AWS_XRAY_CONTEXT_MISSING set to RUNTIME_ERROR - which it often is by default - then you will get these exceptions.

Our logs were littered with ERROR log messages (with AWS_XRAY_CONTEXT_MISSING=LOG_ERROR ) until we found and removed the un-necessary calls to xray_recorder.configure .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM