aws / aws-xray-sdk-node

The official AWS X-Ray SDK for Node.js.
Apache License 2.0
271 stars 155 forks source link

Can we assign an id for the AWS X-ray segment, which going to be autogenerated by AWS for a subsegment? #488

Closed YathuNK closed 1 year ago

YathuNK commented 2 years ago

I send a segment with a subsegment to x-ray API. Subsegment is an DynamoDB putItem request. When X-ray receives this request, It automatically creates a segment for the DynamoDB subsegment and assigns a random id. Is this possible to assign an id (or get to know the id) before sending the request to x-ray API?

This is the request I sent to x-ray API aws xray put-trace-segments --trace-segment-documents "{\"trace_id\":\"1-620b79b7-ddce7ad676d9eb605e937952\",\"id\":\"e8760610e869f263\",\"start_time\":1644919222.813,\"name\":\"raw-xray-dev\",\"service\":{\"runtime\":\"node\",\"runtime_version\":\"v16.13.2\",\"version\":\"unknown\",\"name\":\"unknown\"},\"aws\":{\"ecs\":{\"container\":\"SL-YKalanantharasan\"},\"xray\":{\"sdk\":\"X-Ray for Node.js\",\"sdk_version\":\"3.3.4\",\"package\":\"aws-xray-sdk\"}},\"origin\":\"AWS::ECS::Container\",\"subsegments\":[{\"id\":\"2763c71e6e6227d2\",\"name\":\"dynamodb\",\"start_time\":1644919354.899,\"namespace\":\"aws\",\"aws\":{\"operation\":\"PutItem\",\"region\":\"ap-southeast-2\",\"request_id\":\"NELH0NTFAVHLQFAEFDJHSU9DJVV4KQNSO5AEMVJF66Q9ASUAAJG\",\"retries\":0,\"table_name\":\"raw-staging-dev\"},\"http\":{\"response\":{\"status\":200,\"content_length\":\"2\"}},\"end_time\":1644919355.785}],\"end_time\":1644919357.087}"

Here is the raw data of the segment.

{
    "Id": "1-620b79b7-ddce7ad676d9eb605e937952",
    "Duration": 134.274,
    "LimitExceeded": false,
    "Segments": [
        {
            "Id": "e8760610e869f263",
            "Document": {
                "id": "e8760610e869f263",
                "name": "raw-xray-dev",
                "start_time": 1644919222.813,
                "trace_id": "1-620b79b7-ddce7ad676d9eb605e937952",
                "end_time": 1644919357.087,
                "aws": {
                    "ecs": {
                        "container": "SL-YKalanantharasan"
                    },
                    "xray": {
                        "package": "aws-xray-sdk",
                        "sdk_version": "3.3.4",
                        "sdk": "X-Ray for Node.js"
                    }
                },
                "service": {
                    "name": "unknown",
                    "version": "unknown",
                    "runtime": "node",
                    "runtime_version": "v16.13.2"
                },
                "origin": "AWS::ECS::Container",
                "subsegments": [
                    {
                        "id": "2763c71e6e6227d2",
                        "name": "DynamoDB",
                        "start_time": 1644919354.899,
                        "end_time": 1644919355.785,
                        "http": {
                            "response": {
                                "status": 200
                            }
                        },
                        "aws": {
                            "retries": 0,
                            "region": "ap-southeast-2",
                            "operation": "PutItem",
                            "request_id": "NELH0NTFAVHLQFAEFDJHSU9DJVV4KQNSO5AEMVJF66Q9ASUAAJG",
                            "table_name": "raw-staging-dev",
                            "resource_names": [
                                "raw-staging-dev"
                            ]
                        },
                        "namespace": "aws"
                    }
                ]
            }
        },
        {
            "Id": "1537a9f61a574777",
            "Document": {
                "id": "1537a9f61a574777",
                "name": "DynamoDB",
                "start_time": 1644919354.899,
                "trace_id": "1-620b79b7-ddce7ad676d9eb605e937952",
                "end_time": 1644919355.785,
                "parent_id": "2763c71e6e6227d2",
                "inferred": true,
                "http": {
                    "response": {
                        "status": 200
                    }
                },
                "aws": {
                    "retries": 0,
                    "region": "ap-southeast-2",
                    "operation": "PutItem",
                    "request_id": "NELH0NTFAVHLQFAEFDJHSU9DJVV4KQNSO5AEMVJF66Q9ASUAAJG",
                    "table_name": "raw-staging-dev",
                    "resource_names": [
                        "raw-staging-dev"
                    ]
                },
                "origin": "AWS::DynamoDB::Table"
            }
        }
    ]
}

The second segment is autogenerated by AWS X-ray. subsegment.id gives the value of "2763c71e6e6227d2", Which is not I am looking for. I need to know the segment Id ("1537a9f61a574777") before sending the request to API. So that I can add this id to DaynamoDB streams(lambda functions) as parent_id.

1) I can't use subsegment.addNewSubsegment() because DynamoDB streams are separate lambda functions. 2) I tried to use XRay.batchGetTraces() and loop through the results to find the id, but the DynamoDB trigger called before X-ray API process and save the segment trace.

Can you help me if you have any possible solutions to this problem?

bhautikpip commented 2 years ago

I think segment ID generation is done by the SDK. I don't think we expose any API to modify the segment ID of the internal segment created by instrumentation. I think your best bet would be to probably add some delay in (dynamodb trigger) approach 2 that you mentioned so that you have parsed through the response you received from batch get traces API.

YathuNK commented 2 years ago

Thanks @bhautikpip