我创建了一个 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