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

Keep track of emitted in emitter. #279

Merged
merged 2 commits into from
Apr 23, 2021

Conversation

anuraaga
Copy link
Contributor

@anuraaga anuraaga commented Apr 21, 2021

Issue #, if available:

Description of changes:
In #278, we try to avoid having a long-standing lock around all emissions in a subtree. It causes a race where subsegments may be emitted multiple times. This is because the current emitted isn't being kept track of by the emitter actually, it's set by the caller. This PR splits into emitted, which is intended to prevent double-emission, and ended, which is intended to prevent mutation of an ended entity in most cases.

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

@codecov-commenter
Copy link

Codecov Report

Merging #279 (7c9b078) into master (57c11db) will decrease coverage by 0.30%.
The diff coverage is 35.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #279      +/-   ##
============================================
- Coverage     59.57%   59.26%   -0.31%     
+ Complexity     1210     1207       -3     
============================================
  Files           131      131              
  Lines          5046     5060      +14     
  Branches        586      589       +3     
============================================
- Hits           3006     2999       -7     
- Misses         1767     1786      +19     
- Partials        273      275       +2     
Impacted Files Coverage Δ Complexity Δ
...a/com/amazonaws/xray/entities/DummySubsegment.java 0.00% <0.00%> (ø) 0.00 <0.00> (ø)
.../main/java/com/amazonaws/xray/entities/Entity.java 100.00% <ø> (ø) 3.00 <0.00> (ø)
...zonaws/xray/strategy/DefaultStreamingStrategy.java 87.09% <ø> (-0.41%) 14.00 <0.00> (ø)
...n/java/com/amazonaws/xray/emitters/UDPEmitter.java 31.42% <14.28%> (-21.91%) 5.00 <0.00> (-2.00)
...n/java/com/amazonaws/xray/entities/EntityImpl.java 88.01% <14.28%> (-1.70%) 68.00 <0.00> (ø)
...java/com/amazonaws/xray/entities/DummySegment.java 15.09% <100.00%> (-3.01%) 3.00 <1.00> (-3.00)
.../java/com/amazonaws/xray/entities/NoOpSegment.java 98.79% <100.00%> (+0.01%) 79.00 <1.00> (+1.00)
...va/com/amazonaws/xray/entities/NoOpSubSegment.java 94.80% <100.00%> (+0.06%) 68.00 <1.00> (+1.00)
.../java/com/amazonaws/xray/entities/SegmentImpl.java 87.50% <100.00%> (ø) 22.00 <0.00> (ø)
...va/com/amazonaws/xray/entities/SubsegmentImpl.java 75.00% <100.00%> (ø) 15.00 <0.00> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 57c11db...7c9b078. Read the comment docs.

Copy link
Contributor

@willarmiros willarmiros left a comment

Choose a reason for hiding this comment

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

Overall I'm comfortable with setting emitted in the emitter instead, but I think introducing the new ended variable is redundant - it either is the same as the old emitted variable, or it's the same as the inProgress field.

@@ -216,7 +220,7 @@ protected EntityImpl(AWSXRayRecorder creator, String name) {
*/
protected void checkAlreadyEmitted() {
synchronized (lock) {
if (emitted) {
if (ended || emitted) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For what it's worth, this does not actually make entities immutable after they're ended because ended is set in the same conditions as emitted before it. However I don't think that we should "fix" this and make them immutable, as it would break several customer setups similar to this one:

AWSXRay.beginSegment('service');

// Make an instrumented AWS SDK request that begins & ends a subsegment
DynamoClient.listTables();

// Get the Dynamo subsegment
Subsegment sub = AWSXRay.getSubsegments().get(0);  

// Add some additional metadata they want to see on the Dynamo subsegment.
// Because the subsegment is created created and ended by our instrumentation logic, they can only
// access it after it's been ended
sub.putAnnotation("key", "value");  

I tested locally and this type of logic was still possible even after your change (because ended is the same as emitted before).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup just intending to preserve existing behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok just making sure, thought you were intending to change that behavior based on the PR description.

Copy link
Contributor Author

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

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

it either is the same as the old emitted variable, or it's the same as the inProgress field.

Thanks for the suggestion, It seemed nice, but at first glance while this seems to be true, unfortunately we expose a setInProgress method :/ So technically, it's possible for a user to call setInProgress (we don't really expect it but it's possible) and mutations were still allowed while if I used inProgress in the mutation check instead of a new variable, they would stop being allowed. So I'm thinking of leaving this new field.

@@ -216,7 +220,7 @@ protected EntityImpl(AWSXRayRecorder creator, String name) {
*/
protected void checkAlreadyEmitted() {
synchronized (lock) {
if (emitted) {
if (ended || emitted) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup just intending to preserve existing behavior.

Copy link
Contributor

@willarmiros willarmiros left a comment

Choose a reason for hiding this comment

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

Shoot yeah didn't think about setInProgress. This makes sense then, thanks for the change!

@anuraaga anuraaga merged commit d66d69a into aws:master Apr 23, 2021
willarmiros pushed a commit to willarmiros/aws-xray-sdk-java that referenced this pull request Aug 13, 2022
wangzlei pushed a commit that referenced this pull request Aug 22, 2022
* Revert "Don't lock segment from subsegment (#306)"

This reverts commit d5ccae2.

* Revert "Keep track of emitted in emitter. (#279)"

This reverts commit d66d69a.

* Revert "Remove subsegments lock (#278)"

This reverts commit adfb395.

* Revert "Lock all accesses to entities. (#250)"

This reverts commit f0a3b3a.

* add back errorprone annotations

* fixed shouldPropagate

* added back getSubsegmentsCopy

* added back compareAndSetEmitted

* whitespace fix
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