Skip to content

Refactoring TraceHeader Calls#361

Merged
atshaw43 merged 20 commits intoaws:masterfrom
atshaw43:master
Nov 7, 2022
Merged

Refactoring TraceHeader Calls#361
atshaw43 merged 20 commits intoaws:masterfrom
atshaw43:master

Conversation

@atshaw43
Copy link
Copy Markdown
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
Copy Markdown

codecov-commenter commented Nov 3, 2022

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.41%. Comparing base (fd93c8a) to head (0c6be25).
⚠️ Report is 61 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #361      +/-   ##
============================================
+ Coverage     58.35%   58.41%   +0.05%     
- Complexity     1226     1229       +3     
============================================
  Files           133      133              
  Lines          5016     5011       -5     
  Branches        595      589       -6     
============================================
  Hits           2927     2927              
+ Misses         1815     1814       -1     
+ Partials        274      270       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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
Copy Markdown
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
Copy Markdown
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.isRecording = false;
this.samplingStrategyOverride = samplingStrategyOverride;

id = creator.getIdGenerator().newEntityId();
Copy link
Copy Markdown
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
Copy Markdown
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.

* @return the sampled
*/
@JsonIgnore
@Override
Copy link
Copy Markdown
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
Copy Markdown
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.

3 participants