0

I have created an AWS Lambda function and used Boto3 to get the traces.

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')

I am getting the error of the following.

'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

I am getting UnprocessedTraceSegments error. I am using amazon_header = os.getenv('_X_AMZN_TRACE_ID','') to get the segment id, Yet it is throwing the error of Invalid segment and invalid id for the error code Response metadata.

How can I fix this ?

After changing the id, the following is the log info

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  
Kathiravan Natarajan
  • 3,158
  • 6
  • 22
  • 45

1 Answers1

1

The problem is your id value. According to the AWS docs:

id – A 64-bit identifier for the segment, unique among segments in the same trace, in 16 hexadecimal digits.

What you're doing is:

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

However if you look at the doc samples:

"id"         : "53995c3f42cd8ad8",

This is what your call is going to give you:

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

Instead try this:

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

Note you'll need the random module imported

Chris White
  • 1,409
  • 8
  • 10