AWS XRay:无法写入 /tmp/。aws-xray/initialized。未能发出 SDK 初始化信号
AWS XRay: Unable to write to /tmp/.aws-xray/initialized. Failed to signal SDK initialization
我正在尝试编写一个 Lambda 函数来将数据存储在 dyanamodb 中并尝试与 AWS Xray 集成。下面是 Lambda 函数的代码。我收到错误
Unable to write to /tmp/.aws-xray/initialized. Failed to signal SDK
initialization. Subsegment put_item discarded due to Lambda worker
still initializing
我安装了 Aws xray SDK 包。此外,开始段和结束段也包含在代码中。并设置环境变量LAMBDA_TASK_ROOT。
请给出这个错误的解决方案。
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(第二个问题):在这段代码中 AttributeError: 'NoneType' object has no attribute 'put_annotation' 这个错误来了..我没有知道为什么会这样..
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
您不能在 Lambda 函数内创建分段。 Lambda 将在外部包装器中创建您无法在函数内访问的段。您可以尝试将 xray_recorder.begin_segment('get_item')
更改为 xray_recorder.begin_subsegment('get_item')
吗?
此外,不会捕获在 lambda 处理程序外部生成的跟踪数据,因为在此期间 lambda 函数仍在初始化并且没有可用的跟踪上下文。
我正在尝试编写一个 Lambda 函数来将数据存储在 dyanamodb 中并尝试与 AWS Xray 集成。下面是 Lambda 函数的代码。我收到错误
Unable to write to /tmp/.aws-xray/initialized. Failed to signal SDK initialization. Subsegment put_item discarded due to Lambda worker still initializing
我安装了 Aws xray SDK 包。此外,开始段和结束段也包含在代码中。并设置环境变量LAMBDA_TASK_ROOT。 请给出这个错误的解决方案。
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(第二个问题):在这段代码中 AttributeError: 'NoneType' object has no attribute 'put_annotation' 这个错误来了..我没有知道为什么会这样..
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
您不能在 Lambda 函数内创建分段。 Lambda 将在外部包装器中创建您无法在函数内访问的段。您可以尝试将 xray_recorder.begin_segment('get_item')
更改为 xray_recorder.begin_subsegment('get_item')
吗?
此外,不会捕获在 lambda 处理程序外部生成的跟踪数据,因为在此期间 lambda 函数仍在初始化并且没有可用的跟踪上下文。