python - AWS XRay: Unable to write to /tmp/.aws-xray/initialized. Failed to signal SDK initialization
问题描述
I am trying to write a Lambda function to store data in dyanamodb and try to integrate with AWS Xray. Below is the code for Lambda function. I am getting the error
Unable to write to /tmp/.aws-xray/initialized. Failed to signal SDK initialization. Subsegment put_item discarded due to Lambda worker still initializing
I install Aws xray SDK package. Also, begin segment and end segment are included in the code. and also set an environment variable of LAMBDA_TASK_ROOT. Please give a solution to this error.
import json
import os
import configparser
import boto3
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch
patch(['boto3'])
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['dynamodb_table'])
def lambda_handler(event, context):
for i in event:
item = {
'key': i['key'],
'search': i['search'],
}
put_item_into_dynamodb(item)
response = {
"statusCode": 200,
"body": json.dumps(item)
}
return response
def put_item_into_dynamodb(item):
try:
xray_recorder.begin_subsegment('put_item')
response = table.put_item(Item=item)
status_code = response['ResponseMetadata']['HTTPStatusCode']
xray_recorder.current_subsegment().put_annotation('put_response', status_code)
finally:
xray_recorder.end_subsegment()
Update-2 (second problem) : in this code AttributeError: 'NoneType' object has no attribute 'put_annotation' this error is coming .. i dont have any idea why this is coming..
def lambda_handler(event, context):
val = (str(event['userId']),str(event['teamId']), str(event['searchScope']))
key_table = "|".join(val)
key = {
'key': key_table
}
response = get_item_into_dynamodb(key)
try:
data = response['Item']
for i in data['search']:
keyword_list.append(i['searchText'])
dict_of_keyword[i['searchText']] = i['dateTime']
recent_sort = sorted(dict_of_keyword.items(), key=lambda x: x[1], reverse=True)
def get_item_into_dynamodb(key):
try:
xray_recorder.begin_segment('get_item')
response = table.get_item(Key = key)
status_code = response['ResponseMetadata']['HTTPStatusCode']
xray_recorder.current_subsegment().put_annotation('get_response', status_code) #error is on this line
finally:
xray_recorder.end_subsegment()
return response
解决方案
You cannot create segments within Lambda function. Lambda will create the segments in the outside wrapper which you cannot access within the function. Could you try to change from xray_recorder.begin_segment('get_item')
to xray_recorder.begin_subsegment('get_item')
?
Also the trace data generated outside of lambda handler will not be captured because during that time the lambda function is still initializing and no tracing context is available.
推荐阅读
- python - 如何在opencv python中将透明图像堆叠在另一个图像上
- javascript - 如何通过索引 node.js 组织对象数组
- elasticsearch - 查找 elasticsearch 服务端点
- javascript - 如何下载加密的base64?然后解密
- html - 提交(按钮)选项值(来自下拉列表)后如何显示警报/文本框?
- c# - 列表
> 引用类型不会更改为新的字典值 - pandas - 转换为 Pandas DataFrame 时在 Ibis 中指定 datetime64 分辨率
- sql-server - Azure SQL Server 广告管理员创建 ValidationError
- java - 在自定义 Spring Cloud Gateway 过滤器中获取 Principal 的名称
- javascript - 如何删除数组中的重复数组?