【问题标题】:AWS Lambda function with Boto 3: Issue with response metadata带有 Boto 3 的 AWS Lambda 函数:响应元数据问题
【发布时间】:2017-06-28 22:47:36
【问题描述】:

我创建了一个 AWS Lambda 函数并使用 Boto3 来获取跟踪。

import json
import urllib.request
import boto3
import time
import uuid
import os

print('Loading function')

def lambda_handler(event, context):
    """
    id: uuid4
    trace_id: passed in
    start_time: seconds since epoch
    end_time: seconds since epoch


    response = client.put_trace_segments(
    TraceSegmentDocuments=[
        'json',
    ]
    )

    Root=1-59530dab-b846082c09374e4d0ed6e890;Parent=49fc192e247e8f1c;Sampled=1
    """
    print(os.getenv('_X_AMZN_TRACE_ID',''))
    amazon_header = os.getenv('_X_AMZN_TRACE_ID','')
    parts = amazon_header.split(';')
    segment_id = parts[1]
    parts = segment_id.split('Parent=')
    segment_id = parts[1]
    print(segment_id)
    print("Received request id: " + str(context.aws_request_id))
    print("value1 = " + event.get('key1'))
    print("value2 = " + event.get('key2'))
    print("value3 = " + event.get('key3'))

    start_time = time.time()
    with urllib.request.urlopen('http://www.python.org/') as f:
        print(f.read(300))
        end_time = time.time()

    client = boto3.client('xray')
    response = client.put_trace_segments(TraceSegmentDocuments=[json.dumps(
        {'name': 'HTTP Call',
        'id': str(uuid.uuid4()), 
        'trace_id': context.aws_request_id, 
        'start_time': start_time,
        'end_time': end_time,
        'parent_id': segment_id
        })])
    print(response)

    return event.get('key1')  # Echo back the first key value
    #raise Exception('Something went wrong')

我收到以下错误。

'UnprocessedTraceSegments': [{'Id': '06fd3976-7034-4718-b898-ff1e85cd04e4', 'ErrorCode': 'InvalidId', 'Message': 'Invalid segment. ErrorCode: InvalidId'}]}
END RequestId: 1ac976e2-5c4c-11e7-a2f1-939effdc550a
REPORT RequestId: 1ac976e2-5c4c-11e7-a2f1-939effdc550a

我收到 UnprocessedTraceSegments 错误。我正在使用 amazon_header = os.getenv('_X_AMZN_TRACE_ID','') 来获取段 id,但是它为错误代码响应元数据抛出了 Invalid segment 和 invalid id 的错误。

我该如何解决这个问题?

更改id后,以下是日志信息

START RequestId: b8c6024e-5d0a-11e7-b5c7-f5003e1056aa Version: $LATEST
Root=1-595564f4-84fb09d58c9def1b0a6c682a;Parent=428680bc112c1621;Sampled=1
428680bc112c1621
Received request id: b8c6024e-5d0a-11e7-b5c7-f5003e1056aa
value1 = value1
value2 = value2
value3 = value3
b'<!doctype html>
<!--[if lt IE 7]>   <html class="no-js ie6 lt-ie7 lt-ie8 lt-ie9">   <![endif]-->
<!--[if IE 7]>      <html class="no-js ie7 lt-ie8 lt-ie9">          <![endif]-->
<!--[if IE 8]>      <html class="no-js ie8 lt-ie9">                 <![endif]-->
<!--[if gt IE 8]><!--><html class="no-js"'
{'ResponseMetadata': {'RequestId': 'b95b3df7-5d0a-11e7-9176-d3e9a71b5edd', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Thu, 29 Jun 2017 20:37:09 GMT', 'content-type': 'application/json', 'content-length': '140', 'connection': 'keep-alive', 'x-amzn-requestid': 'b95b3df7-5d0a-11e7-9176-d3e9a71b5edd'}, 'RetryAttempts': 0}, 'UnprocessedTraceSegments': [{'Id': '231a6ad44626fc5d', 'ErrorCode': 'InvalidTraceId', 'Message': 'Invalid segment. ErrorCode: InvalidTraceId'}]}
END RequestId: b8c6024e-5d0a-11e7-b5c7-f5003e1056aa
REPORT RequestId: b8c6024e-5d0a-11e7-b5c7-f5003e1056aa  Duration: 648.67 ms Billed Duration: 700 ms     Memory Size: 128 MB Max Memory Used: 28 MB  

【问题讨论】:

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


    【解决方案1】:

    问题在于您的 id 值。根据AWS docs:

    id – 段的 64 位标识符,在同一跟踪中的段之间是唯一的,采用 16 位十六进制数字。

    你正在做的是:

    'id': str(uuid.uuid4())
    

    但是,如果您查看文档示例:

    "id"         : "53995c3f42cd8ad8",
    

    这就是你的电话将给你的:

    str(uuid.uuid4())
    'f88a7dac-fb53-48e9-82ce-b9ed269d3c57'
    

    试试这个:

    'id': '%016x' % random.randrange(16**16)
    

    请注意,您需要导入随机模块

    【讨论】:

    • 是 更改 uuid 后,id 正在更改。仍然收到 UnprocessedTraceSegments 错误。
    • @Kathir 您需要更新您的帖子,然后使用其他错误信息。如果没有更新的错误,我不知道可能出了什么问题。
    • @Kathir 如果您更新了 ID 生成部分,则错误不会相同,因为 ID 的格式已更改。我需要查看更新的整条消息,看看还有什么可能不正确。
    • 嗨。我现在已经更新了这个问题。请检查一下。
    猜你喜欢
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2015-03-17
    • 2020-07-04
    • 1970-01-01
    • 2016-02-29
    相关资源
    最近更新 更多