Skip to content

Commit

Permalink
use deprecated attributes as fallback in matching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jj22ee committed Feb 14, 2024
1 parent ad597ab commit bc5617e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ def matches(self, resource: Resource, attributes: Attributes) -> bool:
service_name = None

if attributes is not None:
url_path = attributes.get(SpanAttributes.URL_PATH, None)
url_full = attributes.get(SpanAttributes.URL_FULL, None)
http_request_method = attributes.get(SpanAttributes.HTTP_REQUEST_METHOD, None)
server_address = attributes.get(SpanAttributes.SERVER_ADDRESS, None)
# If `URL_PATH/URL_FULL/HTTP_REQUEST_METHOD/SERVER_ADDRESS` are not populated
# also check `HTTP_TARGET/HTTP_URL/HTTP_METHOD/HTTP_HOST` respectively as backup
url_path = attributes.get(SpanAttributes.URL_PATH, attributes.get(SpanAttributes.HTTP_TARGET, None))
url_full = attributes.get(SpanAttributes.URL_FULL, attributes.get(SpanAttributes.HTTP_URL, None))
http_request_method = attributes.get(SpanAttributes.HTTP_REQUEST_METHOD, attributes.get(SpanAttributes.HTTP_METHOD, None))
server_address = attributes.get(SpanAttributes.SERVER_ADDRESS, attributes.get(SpanAttributes.HTTP_HOST, None))

# Resource shouldn't be none as it should default to empty resource
if resource is not None:
Expand All @@ -123,8 +125,8 @@ def matches(self, resource: Resource, attributes: Attributes) -> bool:
if url_path is None and url_full is not None:
scheme_end_index = url_full.find("://")
# For network calls, URL usually has `scheme://host[:port][path][?query][#fragment]` format
# Per spec, url.full is always populated with scheme://host/target.
# If scheme doesn't match, assume it's bad instrumentation and ignore.
# Per spec, url.full is always populated with scheme://
# If scheme is not present, assume it's bad instrumentation and ignore.
if scheme_end_index > -1:
# urlparse("scheme://netloc/path;parameters?query#fragment")
url_path = urlparse(url_full).path
Expand Down Expand Up @@ -161,10 +163,22 @@ def __get_arn(self, resource: Resource, attributes: Attributes) -> str:
arn = resource.attributes.get(ResourceAttributes.AWS_ECS_CONTAINER_ARN, None)
if arn is not None:
return arn
if attributes is not None and self.__get_service_type(resource=resource) == cloud_platform_mapping.get(
CloudPlatformValues.AWS_LAMBDA.value
):
arn = attributes.get(SpanAttributes.CLOUD_RESOURCE_ID, None)
if arn is not None:
return arn
if resource is not None and resource.attributes.get(ResourceAttributes.CLOUD_PLATFORM) == CloudPlatformValues.AWS_LAMBDA.value:
return self.__get_lambda_arn(resource, attributes)
return ""

def __get_lambda_arn(self, resource: Resource, attributes: Attributes) -> str:
arn = resource.attributes.get(ResourceAttributes.CLOUD_RESOURCE_ID,
resource.attributes.get(ResourceAttributes.FAAS_ID, None))
if arn is not None:
return arn

# Note from `SpanAttributes.CLOUD_RESOURCE_ID`:
# "On some cloud providers, it may not be possible to determine the full ID at startup,
# so it may be necessary to set cloud.resource_id as a span attribute instead."
arn = attributes.get(SpanAttributes.CLOUD_RESOURCE_ID,
attributes.get("faas.id", None))
if arn is not None:
return arn

return ""
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ def test_applier_attribute_matching_from_xray_response(self):
rule_applier = _SamplingRuleApplier(default_rule, CLIENT_ID, _Clock())
self.assertTrue(rule_applier.matches(res, attr))

# Test again using deprecated Span Attributes
attr: Attributes = {
SpanAttributes.HTTP_TARGET: "target",
SpanAttributes.HTTP_METHOD: "method",
SpanAttributes.HTTP_URL: "url",
SpanAttributes.HTTP_HOST: "host",
"foo": "bar",
"abc": "1234",
}
self.assertTrue(rule_applier.matches(res, attr))

def test_applier_matches_with_all_attributes(self):
sampling_rule = _SamplingRule(
Attributes={"abc": "123", "def": "4?6", "ghi": "*89"},
Expand Down Expand Up @@ -80,6 +91,12 @@ def test_applier_matches_with_all_attributes(self):
"abc": "123",
"def": "456",
"ghi": "789",

# Test that deprecated attributes are not used in matching when above new attributes are set
"http.host": "deprecated and will not be used in matching",
SpanAttributes.HTTP_METHOD: "deprecated and will not be used in matching",
"faas.id": "deprecated and will not be used in matching",
"http.url": "deprecated and will not be used in matching",
}

resource_attr: Resource = {
Expand All @@ -91,6 +108,18 @@ def test_applier_matches_with_all_attributes(self):
rule_applier = _SamplingRuleApplier(sampling_rule, CLIENT_ID, _Clock())
self.assertTrue(rule_applier.matches(resource, attributes))

# Test using deprecated Span Attributes
attributes: Attributes = {
"http.host": "localhost",
SpanAttributes.HTTP_METHOD: "GET",
"faas.id": "arn:aws:lambda:us-west-2:123456789012:function:my-function",
"http.url": "http://127.0.0.1:5000/helloworld",
"abc": "123",
"def": "456",
"ghi": "789",
}
self.assertTrue(rule_applier.matches(resource, attributes))

def test_applier_wild_card_attributes_matches_span_attributes(self):
sampling_rule = _SamplingRule(
Attributes={
Expand Down Expand Up @@ -159,6 +188,15 @@ def test_applier_wild_card_attributes_matches_http_span_attributes(self):
rule_applier = _SamplingRuleApplier(sampling_rule, CLIENT_ID, _Clock())
self.assertTrue(rule_applier.matches(Resource.get_empty(), attributes))

# Test using deprecated Span Attributes
attributes: Attributes = {
SpanAttributes.HTTP_HOST: "localhost",
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_URL: "http://127.0.0.1:5000/helloworld",
}

self.assertTrue(rule_applier.matches(Resource.get_empty(), attributes))

def test_applier_wild_card_attributes_matches_with_empty_attributes(self):
sampling_rule = _SamplingRule(
Attributes={},
Expand Down Expand Up @@ -245,6 +283,10 @@ def test_applier_matches_with_http_target(self):
rule_applier = _SamplingRuleApplier(sampling_rule, CLIENT_ID, _Clock())
self.assertTrue(rule_applier.matches(resource, attributes))

# Test again using deprecated Span Attributes
attributes: Attributes = {SpanAttributes.HTTP_TARGET: "/helloworld"}
self.assertTrue(rule_applier.matches(resource, attributes))

def test_applier_matches_with_span_attributes(self):
sampling_rule = _SamplingRule(
Attributes={"abc": "123", "def": "456", "ghi": "789"},
Expand All @@ -263,7 +305,7 @@ def test_applier_matches_with_span_attributes(self):
)

attributes: Attributes = {
"http.host": "localhost",
"server.address": "localhost",
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
"url.full": "http://127.0.0.1:5000/helloworld",
"abc": "123",
Expand All @@ -280,6 +322,17 @@ def test_applier_matches_with_span_attributes(self):
rule_applier = _SamplingRuleApplier(sampling_rule, CLIENT_ID, _Clock())
self.assertTrue(rule_applier.matches(resource, attributes))

# Test again using deprecated Span Attributes
attributes: Attributes = {
"http.host": "localhost",
SpanAttributes.HTTP_METHOD: "GET",
"http.url": "http://127.0.0.1:5000/helloworld",
"abc": "123",
"def": "456",
"ghi": "789",
}
self.assertTrue(rule_applier.matches(resource, attributes))

def test_applier_does_not_match_with_less_span_attributes(self):
sampling_rule = _SamplingRule(
Attributes={"abc": "123", "def": "456", "ghi": "789"},
Expand Down

0 comments on commit bc5617e

Please sign in to comment.