Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring TraceHeader Calls #361

Merged
merged 20 commits into from
Nov 7, 2022
Merged

Refactoring TraceHeader Calls #361

merged 20 commits into from
Nov 7, 2022

Conversation

atshaw43
Copy link
Contributor

@atshaw43 atshaw43 commented Nov 3, 2022

Issue #, if available:

Description of changes:
Problem 1
We have redundant code for creating a trace header.

Solution
Create a centralized function for creating trace headers from entities. This required NoOpSubsegment to have its traceId.

Problem 2
Why does NoOpSubsegment need a namespace and name? We have a bug where we are not correctly ending subsegments if they are NoOp. The subsegment gets created, but never ended by afterResponse because it is seen as a duplicate because its namespace and name is always empty.

Solution
Pass down the name and namespace in NoOpSubsegments.

https://github.com/aws/aws-xray-sdk-java/blob/master/aws-xray-recorder-sdk-aws-sdk/src/main/java/com/amazonaws/xray/handlers/TracingHandler.java

@Override
    public void afterResponse(Request<?> request, Response<?> response) {
        if (isSubsegmentDuplicate(recorder.getCurrentSubsegmentOptional(), request)) {
            Optional<Subsegment> currentSubsegmentOptional = recorder.getCurrentSubsegmentOptional();
            if (!currentSubsegmentOptional.isPresent()) {
                return;
            }
            Subsegment currentSubsegment = currentSubsegmentOptional.get();
            populateAndEndSubsegment(currentSubsegment, request, response, null);
        }
    }

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

@atshaw43 atshaw43 requested a review from a team as a code owner November 3, 2022 00:01
@codecov-commenter
Copy link

codecov-commenter commented Nov 3, 2022

Codecov Report

Merging #361 (ce630b3) into master (fd93c8a) will increase coverage by 0.03%.
The diff coverage is 92.30%.

@@             Coverage Diff              @@
##             master     #361      +/-   ##
============================================
+ Coverage     58.35%   58.38%   +0.03%     
- Complexity     1226     1228       +2     
============================================
  Files           133      133              
  Lines          5016     5015       -1     
  Branches        595      590       -5     
============================================
+ Hits           2927     2928       +1     
- Misses         1815     1816       +1     
+ Partials        274      271       -3     
Impacted Files Coverage Δ
.../main/java/com/amazonaws/xray/entities/Entity.java 100.00% <ø> (ø)
...main/java/com/amazonaws/xray/entities/Segment.java 100.00% <ø> (ø)
...va/com/amazonaws/xray/entities/NoOpSubSegment.java 85.43% <84.61%> (-0.14%) ⬇️
...aws/xray/proxies/apache/http/TracedHttpClient.java 40.47% <100.00%> (-2.06%) ⬇️
...mazonaws/xray/interceptors/TracingInterceptor.java 60.18% <100.00%> (+0.18%) ⬆️
...va/com/amazonaws/xray/handlers/TracingHandler.java 41.56% <100.00%> (-1.35%) ⬇️
.../amazonaws/xray/contexts/LambdaSegmentContext.java 79.16% <100.00%> (ø)
...n/java/com/amazonaws/xray/entities/Subsegment.java 50.00% <100.00%> (+10.00%) ⬆️
...va/com/amazonaws/xray/entities/SubsegmentImpl.java 73.52% <100.00%> (+1.88%) ⬆️
.../java/com/amazonaws/xray/entities/TraceHeader.java 90.58% <100.00%> (+0.46%) ⬆️
... and 2 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

isSampled ? currentSubsegment.getId() : null,
isSampled ? SampleDecision.SAMPLED : SampleDecision.NOT_SAMPLED);
request.addHeader(TraceHeader.HEADER_KEY, header.toString());
request.addHeader(TraceHeader.HEADER_KEY, TraceHeader.fromEntity(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code doesn't look equivalent to the one it's replacing.
In the left side code, the parent id as the TraceHeader argument is either currentSubsegment's id or null. Only the isSampled is fetched from either previousSubsegment or the recorder.getCurrentSegment().
In the right side code, the whole TraceHeader will be created either from previousSubsegment or currentSubsegment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, good catch! This was a bug.
I looked at the original code and I don't think we even need to use the previous subsegment here.

this.samplingStrategyOverride = samplingStrategyOverride;

id = creator.getIdGenerator().newEntityId();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this NoOpSubsegment is never emitted and never propagated, can we set a static id here? We should avoid unnecessary computation if we can.

Copy link
Contributor Author

@atshaw43 atshaw43 Nov 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.
I added it when I noticed parentId was missing in NoOpSubsegments in trace headers. Then I saw we don't actually add it to the header when it is not sampled. Forgot to remove it after that.

@@ -43,6 +43,8 @@ static Segment noOp(TraceID traceId, AWSXRayRecorder recorder) {
/**
* @return the sampled
*/
@JsonIgnore
@Override
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to necessarily override the isSampled() method from Entity?
Is it possible for Entity types and subtypes to directly use this method from Entity without an override?

Copy link
Contributor Author

@atshaw43 atshaw43 Nov 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, removed.

@atshaw43 atshaw43 merged commit 93a5877 into aws:master Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants