【问题标题】:AWS XRay: Unable to write to /tmp/.aws-xray/initialized. Failed to signal SDK initializationAWS XRay:无法写入 /tmp/.aws-xray/initialized。未能发出 SDK 初始化信号
【发布时间】:2019-05-03 08:16:58
【问题描述】:

我正在尝试编写一个 Lambda 函数来将数据存储在 dyanamodb 中并尝试与 AWS Xray 集成。下面是 Lambda 函数的代码。我收到了错误

无法写入 /tmp/.aws-xray/initialized。未能向 SDK 发出信号 初始化。由于 Lambda 工作程序而丢弃了子段 put_item 仍在初始化

我安装了 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

【问题讨论】:

  • hii joey 欢迎来到 stackoverflow,您可以在这里发布完整代码
  • 请检查我更新了
  • cloudwatch 是否说明该错误发生在哪一行?
  • 现在我在本地运行,这个错误来了。
  • 当我刚刚打补丁时,无法写入 /tmp/.aws-xray/initialized。未能发出 SDK 初始化信号 显示此错误,

标签: python amazon-web-services aws-lambda aws-xray


【解决方案1】:

您不能在 Lambda 函数中创建分段。 Lambda 将在外部包装器中创建您无法在函数内访问的段。能不能把xray_recorder.begin_segment('get_item')改成xray_recorder.begin_subsegment('get_item')

此外,在 lambda 处理程序之外生成的跟踪数据也不会被捕获,因为在此期间 lambda 函数仍在初始化并且没有可用的跟踪上下文。

【讨论】:

  • 它有效.. 对于 xray_recorder.begin_subsegment('put_item') 的第一个代码,我也需要更改吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多