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

Oversampling mitigation #354

Merged
merged 15 commits into from
Sep 30, 2022
Merged

Conversation

atshaw43
Copy link
Contributor

Issue #, if available:

Description of changes:
Oversampling Mitigation and SQS Helper

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 September 22, 2022 01:16
@codecov-commenter
Copy link

codecov-commenter commented Sep 22, 2022

Codecov Report

Merging #354 (fcba4a0) into master (9487f19) will increase coverage by 0.23%.
The diff coverage is 66.07%.

@@             Coverage Diff              @@
##             master     #354      +/-   ##
============================================
+ Coverage     58.30%   58.54%   +0.23%     
- Complexity     1209     1234      +25     
============================================
  Files           131      133       +2     
  Lines          4948     4986      +38     
  Branches        590      595       +5     
============================================
+ Hits           2885     2919      +34     
- Misses         1788     1793       +5     
+ Partials        275      274       -1     
Impacted Files Coverage Δ
...core/src/main/java/com/amazonaws/xray/AWSXRay.java 59.18% <0.00%> (-1.24%) ⬇️
...va/com/amazonaws/xray/contexts/SegmentContext.java 88.23% <ø> (ø)
...a/com/amazonaws/xray/entities/DummySubsegment.java 0.00% <0.00%> (ø)
...va/com/amazonaws/xray/lambda/SQSMessageHelper.java 33.33% <33.33%> (ø)
.../main/java/com/amazonaws/xray/AWSXRayRecorder.java 63.58% <50.00%> (-0.17%) ⬇️
...onaws/xray/contexts/ThreadLocalSegmentContext.java 74.46% <57.14%> (-3.31%) ⬇️
.../amazonaws/xray/contexts/LambdaSegmentContext.java 76.71% <63.63%> (-1.86%) ⬇️
...n/java/com/amazonaws/xray/entities/Subsegment.java 83.33% <75.00%> (-16.67%) ⬇️
...va/com/amazonaws/xray/entities/SubsegmentImpl.java 71.92% <80.00%> (+2.11%) ⬆️
...va/com/amazonaws/xray/handlers/TracingHandler.java 42.91% <100.00%> (+4.77%) ⬆️
... and 4 more

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

@atshaw43
Copy link
Contributor Author

atshaw43 commented Sep 22, 2022

Sample code in Lambda

package example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage;

import com.amazonaws.xray.AWSXRayRecorder;
import com.amazonaws.xray.sqs.SQSMessageHelper;
import com.amazonaws.xray.AWSXRay;

import java.lang.StringBuilder;
import java.util.Map;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import io.opentelemetry.api.trace.Span;

public class Handler implements RequestHandler<SQSEvent, String> {
  public Handler(){
  }

  @Override
  public String handleRequest(SQSEvent event, Context context) {

    // This variable will override the SamplingStrategy to whatever we need.
    boolean toSample = false;

    for (SQSMessage message: event.getRecords()) {
      // This helper function makes it easier to tell if a SQS message is sampled.
      // There are some magic strings involved that we are hiding from the customer.
      if (SQSMessageHelper.isSampled(message)) {
        toSample = true;
      }
    }
    
    // This is the new function that allows the override of the SamplingStrategy.
    AWSXRay.getGlobalRecorder().beginSubsegmentWithSamplingOverride("Batch Trace 1", toSample);
    AWSXRay.getGlobalRecorder().endSubsegment();

    // Testing making two in a single event for good measure.
    AWSXRay.getGlobalRecorder().beginSubsegmentWithSamplingOverride("Batch Trace 2", toSample);
    AWSXRay.getGlobalRecorder().endSubsegment();
  }
}

Test 1 - This is the JSON sent to lambda

{
  "Records": [
    {
      "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
      "receiptHandle": "MessageReceiptHandle",
      "body": "Hello from SQS!",
      "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "1523232000000",
        "SenderId": "123456789012",
        "ApproximateFirstReceiveTimestamp": "1523232000001",
        "AWSTraceHeader": "Root=1-632BB806-bd862e3fe1be46a994272793;Sampled=1"
      },
      "messageAttributes": {},
      "md5OfBody": "{{{md5_of_body}}}",
      "eventSource": "aws:sqs",
      "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
      "awsRegion": "us-east-1"
    }
  ]
}

Sampling

Test 2 - Note the different Sampled value

{
  "Records": [
    {
      "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
      "receiptHandle": "MessageReceiptHandle",
      "body": "Hello from SQS!",
      "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "1523232000000",
        "SenderId": "123456789012",
        "ApproximateFirstReceiveTimestamp": "1523232000001",
        "AWSTraceHeader": "Root=1-632BB806-bd862e3fe1be46a994272793;Sampled=0"
      },
      "messageAttributes": {},
      "md5OfBody": "{{{md5_of_body}}}",
      "eventSource": "aws:sqs",
      "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
      "awsRegion": "us-east-1"
    }
  ]
}

NoSampling


import com.amazonaws.services.lambda.runtime.events.SQSEvent;

public final class SQSMessageHelper {
Copy link
Contributor

@wangzlei wangzlei Sep 23, 2022

Choose a reason for hiding this comment

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

XRayLambdaSQSMessageHelper? it might be a horrible name.

Copy link
Contributor Author

@atshaw43 atshaw43 Sep 27, 2022

Choose a reason for hiding this comment

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

I think it makes sense to change the package/module name from SQS to lambda. But let's keep the class name concise. This is what they did in the Lambda library where SQSMessage lives.


tasks.jar {
manifest {
attributes("Automatic-Module-Name" to "com.amazonaws.xray_sqs")
Copy link
Contributor

@wangzlei wangzlei Sep 23, 2022

Choose a reason for hiding this comment

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

Module name could be lambda helper since it is help Lambda user, not for sqs instrumentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

package com.amazonaws.xray.internal;

public enum SamplingStrategyOverride {
DISABLED, // Does not override the SamplingStrategy.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not quite get why have to use DISABLED.
Can we use existing API, then update the sampled flag in SegmentImpl?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we are changing the parent's flag (the Lambda Frontend Segment), then will that not cause an issue in multithreading?


boolean isRecording = (parentSegment.isRecording() &&
samplingStrategyOverride == SamplingStrategyOverride.DISABLED) ||
samplingStrategyOverride == SamplingStrategyOverride.TRUE;
Copy link
Contributor

Choose a reason for hiding this comment

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

When parentSegment.isRecording() == false and samplingStrategyOverride == SamplingStrategyOverride.TRUE, should we create a new subsegment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. The override to true will ignore any other indicators.
I guess this creates a problem of an orphaned subsegment?

@atshaw43 atshaw43 merged commit 11c498b into aws:master Sep 30, 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