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

Fix xray.BeginSegment to honor sampling rule #244

Merged
merged 4 commits into from
Aug 3, 2020
Merged

Conversation

kevioke
Copy link
Contributor

@kevioke kevioke commented Jul 24, 2020

Fixes #242

Description of changes:
Requests that have empty fields should not apply to sampling rules that require specific patterns to match. This change removes the OR condition on having empty fields short circuit any sampling rule pattern match.

In addition, this change also passes the ServiceName to the ShouldTrace in the BeginSegmentWithSampling function so that sampling rules can be applied more accurately with traces that only specify ServiceNames.

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

@srprash srprash requested a review from bhautikpip July 28, 2020 00:18

testCases := []testCase{
{
testName: "if the request is empty and the rule has a specific ServiceName, AppliesTo should return false",
Copy link
Contributor

@bhautikpip bhautikpip Jul 29, 2020

Choose a reason for hiding this comment

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

Can we add one test case here, where all the values of rule are populated like URLPath, ServiceName, ServiceType, Host and httpMethod. Now, we pass an empty request, AppliesTo should return false ? Same for TestLocalizedRuleAppliesTo too.

@@ -83,8 +83,8 @@ func BeginSegmentWithSampling(ctx context.Context, name string, r *http.Request,
}

if r == nil || traceHeader == nil {
// Sampling strategy fallbacks to default sampling in the case of directly using BeginSegment API without any instrumentation
sd := seg.ParentSegment.GetConfiguration().SamplingStrategy.ShouldTrace(&sampling.Request{})
// No header or request information provided so we can only evaluate sampling based on the serviceName
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, can we include this as a Note in README file so that sampling behavior is very clear when customer use BeginSegment API directly. (since we are evaluating only based on service name here)

(method == "" || pattern.WildcardMatchCaseInsensitive(p.HTTPMethod, method))
return pattern.WildcardMatchCaseInsensitive(p.Host, host) &&
pattern.WildcardMatchCaseInsensitive(p.URLPath, path) &&
pattern.WildcardMatchCaseInsensitive(p.HTTPMethod, method)
}

// AppliesTo returns true if the sampling rule matches against given sampling request. False Otherwise.
// Assumes lock is already held, if required.
func (r *CentralizedRule) AppliesTo(request *Request) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Changing this to exclude absent request attributes would lead to behavior inconsistent with other SDKs, for example our node SDK and Python SDK. I understand the ask to change this, but changing the behavior like this would be a fairly consequential, arguably breaking, change.

@kevioke I believe the change you made to the beginSegment API should be sufficient to resolve #242 since the serviceName will no longer be absent. Our team will have to discuss this change which may delay this PR quite a bit. If you'd like to remove this change and only include the beginSegment change that could expedite this getting released.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@willarmiros thanks for the reply! I had a hunch this empty request logic was present for a reason, just didn't know quite why. In any case, I like your proposal of just including the changes to beginSegment to speed this PR along, and I believe you're correct that it should work for my use cases since now I can specify a rule with a ServiceName.

I'll update the PR later today or tomorrow.

Per
aws#244 (comment)
these changes require further consideration from the AWS team.
README.md Outdated
@@ -137,6 +137,7 @@ func init() {
```

**Start a custom segment/subsegment**
Note that the `xray.BeginSegment` function currently uses only the service name parameter to determine the applicability of any sampling rules.
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi can we rephrase this to something like customers using xray.BeginSegment API directly will only be able to evaluate sampling rules based on service name ? It's important to show that this behavior is not applicable for instrumented requests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is much clearer :)

I'll update it accordingly

Copy link
Contributor

@bhautikpip bhautikpip left a comment

Choose a reason for hiding this comment

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

Looks good

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.

LGTM

@bhautikpip bhautikpip merged commit 17f8b48 into aws:master Aug 3, 2020
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.

xray.BeginSegment does not honor sampling rule
3 participants