Reputation: 119
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)
Upvotes: 0
Views: 11355
Reputation: 913
Not sure but I have similar problem and just add this line of code
xray_recorder.begin_segment("Test Segment")
Upvotes: 0
Reputation: 341
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
.
Upvotes: 2
Reputation: 679
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
?
Upvotes: 4