aws / aws-xray-sdk-java

The official AWS X-Ray Recorder SDK for Java.
Apache License 2.0
94 stars 98 forks source link

TracingInterceptor should take effect only one time #399

Closed wangzlei closed 6 months ago

wangzlei commented 6 months ago

Issue #, if available: It is possible for a customer to inadvertently introduce more than one X-Ray interceptor into the AWS SDK v2 client, resulting in:

  1. Generating subsegments with duplicate data, such as retry and exception. Below is the example even there is no retry, but subsegment contains retries : 1
    "subsegments": [
                            {
                                "id": "529b401c23f45c01",
                                "name": "SQS",
                                "start_time": 1709320615.891,
                                "end_time": 1709320616.678,
                                "http": {
                                    "response": {
                                        "status": 200,
                                        "content_length": 106
                                    }
                                },
                                "aws": {
                                    "retries": 1,
                                    "queue_url": "https://sqs.us-east-1.amazonaws.com/133818585968/sam-dev-zgaouett-FlexMonitoringSe-ItemReactionStateChangeHandlerQue-ASj2Usx3cSJZ",
                                    "message_id": "024c4942-9515-44bf-b6c1-a08ead6f342d",
                                    "region": "us-east-1",
                                    "operation": "SendMessage",
                                    "request_id": "7d9b6cb5-fc44-50dd-be7b-32e9d7ac7162",
                                    "resource_names": [
                                        "https://sqs.us-east-1.amazonaws.com/133818585968/sam-dev-zgaouett-FlexMonitoringSe-ItemReactionStateChangeHandlerQue-ASj2Usx3cSJZ"
                                    ]
                                },
                                "namespace": "aws"
                            }
                        ]
  2. Injecting duplicate trace headers to downstream. Screenshot 2024-03-02 at 12 27 26 PM

Description of changes: The PR includes 2 changes:

  1. Storing the address of the first XRay Interceptor in AWS SDK context, to make sure only one XRay Interceptor takes effect
  2. Using putHeader() instead of appendHeader() in trace header injection, this ensures the request contains only one xray trace header value.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

codecov-commenter commented 6 months ago

Codecov Report

Attention: Patch coverage is 89.47368% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 28.60%. Comparing base (0cc3e14) to head (90a4734).

Files Patch % Lines
...mazonaws/xray/interceptors/TracingInterceptor.java 89.47% 1 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #399 +/- ## ============================================ + Coverage 24.59% 28.60% +4.00% - Complexity 505 588 +83 ============================================ Files 138 138 Lines 5265 5283 +18 Branches 635 637 +2 ============================================ + Hits 1295 1511 +216 + Misses 3786 3568 -218 - Partials 184 204 +20 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.