Skip to content

Commit 045f745

Browse files
author
Michael Brewer
committed
feat: only call add_ignored when using the xray sdk
1 parent df44bb7 commit 045f745

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

aws_lambda_powertools/tracing/tracer.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,12 @@ def _disable_xray_trace_batching(self):
779779
def _is_xray_provider(self):
780780
return "aws_xray_sdk" in self.provider.__module__
781781

782-
@staticmethod
783-
def ignore_endpoint(hostname: Optional[str] = None, urls: Optional[List[str]] = None):
782+
def ignore_endpoint(self, hostname: Optional[str] = None, urls: Optional[List[str]] = None):
784783
"""If you want to ignore certain httplib requests you can do so based on the hostname or URL that is being
785784
requested.
786785
786+
> NOTE: If the provider is not xray, nothing will be added to ignore list
787+
787788
Documentation
788789
--------------
789790
- https://github.com/aws/aws-xray-sdk-python#ignoring-httplib-requests
@@ -795,6 +796,9 @@ def ignore_endpoint(hostname: Optional[str] = None, urls: Optional[List[str]] =
795796
urls: Optional, List[str]
796797
List of urls to ignore. Example `tracer.ignore_endpoint(urls=["/ignored-url"])`
797798
"""
799+
if not self._is_xray_provider():
800+
return
801+
798802
from aws_xray_sdk.ext.httplib import add_ignored # type: ignore
799803

800804
add_ignored(hostname=hostname, urls=urls)

tests/unit/test_tracing.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
from typing import NamedTuple
44
from unittest import mock
5+
from unittest.mock import MagicMock
56

67
import pytest
78

@@ -630,7 +631,22 @@ def handler(event, context):
630631
assert in_subsegment_mock.put_annotation.call_args == mocker.call(key="ColdStart", value=True)
631632

632633

633-
def test_ignore_endpoints(provider_stub, in_subsegment_mock):
634-
provider = provider_stub(in_subsegment=in_subsegment_mock.in_subsegment)
635-
tracer = Tracer(provider=provider)
636-
tracer.ignore_endpoint(hostname="https://foo.com/", urls=["/bar", "/ignored"])
634+
@mock.patch("aws_xray_sdk.ext.httplib.add_ignored")
635+
def test_ignore_endpoints_xray_sdk(mock_add_ignored: MagicMock):
636+
# GIVEN a xray sdk provider
637+
tracer = Tracer()
638+
# WHEN we call ignore_endpoint
639+
tracer.ignore_endpoint(hostname="https://www.foo.com/", urls=["/bar", "/ignored"])
640+
# THEN call xray add_ignored
641+
assert mock_add_ignored.call_count == 1
642+
mock_add_ignored.assert_called_with(hostname="https://www.foo.com/", urls=["/bar", "/ignored"])
643+
644+
645+
@mock.patch("aws_xray_sdk.ext.httplib.add_ignored")
646+
def test_ignore_endpoints_mocked_provider(mock_add_ignored: MagicMock, provider_stub, in_subsegment_mock):
647+
# GIVEN a mock provider
648+
tracer = Tracer(provider=provider_stub(in_subsegment=in_subsegment_mock.in_subsegment))
649+
# WHEN we call ignore_endpoint
650+
tracer.ignore_endpoint(hostname="https://foo.com/")
651+
# THEN don't call xray add_ignored
652+
assert mock_add_ignored.call_count == 0

0 commit comments

Comments
 (0)