aws/aws-xray-sdk-ruby

Sampling rules matching when they shouldn't

LarsFronius opened this issue · 1 comments

Say I have a sampling rule like this with priority 1:
screenshot 2019-03-05 at 13 44 02
The only other rule configured is the Default rule.

I'd expect for this sampling rule to apply to a service, which starts it's segment like
XRay.recorder.begin_segment 'anotherService' and no others, in order to throttle tracing data originating from anotherService.

However, it appears to also match in our case to other documents than originally intended and thus is effectively becoming the new default.
From our document I can see that the rule has been applied (censored some bits):

{
    "Duration": 0.095,
    "Id": "1-5c7e774b-8423ee74fd64ab8c0540f810",
    "Segments": [
        {
            "Document": {
                "id": "1799765cda88d70c",
                "name": "aDifferentServiceName",
                "start_time": 1551791947.8924115,
                "end_time": 1551791947.9878953,
                "http": {
                    "request": {
                        "url": "XXXXX",
                        "method": "GET",
                        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36",
                        "client_ip": "XXXXXX",
                        "x_forwarded_for": true
                    },
                    "response": {
                        "status": 200,
                        "content_length": 12345
                    }
                },
                "aws": {
                    "xray": {
                        "sdk_version": "0.11.1",
                        "sdk": "X-Ray for Ruby",
                        "sampling_rule_name": "foo"
                    }
                },

I'd have expected for my rule not to match in this case and only the Default rule to match.

I confirmed this is a bug. The sampling rule definition is mostly http centric (host, url, path etc) and the matching is done at middleware level if the rack middleware is used for generating segments. But as you can see when manually creating segment the SDK doesn't match against anything so the rule with highest priority always matches: https://github.com/aws/aws-xray-sdk-ruby/blob/master/lib/aws-xray-sdk/recorder.rb#L31-L32

But the field service_name should apply to all scenarios such that manually creating a segment also respects that pattern. We will fix this.