AWS Lambda函数与Boto 3:响应元数据问题

时间:2017-06-28 22:47:36

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

我创建了一个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,但是它会抛出无效段的错误和错误代码的无效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  

1 个答案:

答案 0 :(得分:0)

问题是你的id值。根据{{​​3}}:

  

id - 段的64位标识符,在同一跟踪中的段之间唯一,以16个十六进制数字表示。

您正在做的是:

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

但是,如果你看一下doc样本:

"id"         : "53995c3f42cd8ad8",

这是您打电话给您的内容:

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

而是试试这个:

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

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