Skip to content

Commit ccefa87

Browse files
feat(tracer): ignore tracing for certain hostname(s) or url(s) (aws-powertools#910)
1 parent 9abeb32 commit ccefa87

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Diff for: aws_lambda_powertools/tracing/tracer.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
import numbers
77
import os
8-
from typing import Any, Callable, Dict, Optional, Sequence, Union, cast, overload
8+
from typing import Any, Callable, Dict, List, Optional, Sequence, Union, cast, overload
99

1010
from ..shared import constants
1111
from ..shared.functions import resolve_env_var_choice, resolve_truthy_env_var_choice
@@ -758,7 +758,7 @@ def _patch_xray_provider(self):
758758
# Due to Lazy Import, we need to activate `core` attrib via import
759759
# we also need to include `patch`, `patch_all` methods
760760
# to ensure patch calls are done via the provider
761-
from aws_xray_sdk.core import xray_recorder
761+
from aws_xray_sdk.core import xray_recorder # type: ignore
762762

763763
provider = xray_recorder
764764
provider.patch = aws_xray_sdk.core.patch
@@ -778,3 +778,27 @@ def _disable_xray_trace_batching(self):
778778

779779
def _is_xray_provider(self):
780780
return "aws_xray_sdk" in self.provider.__module__
781+
782+
def ignore_endpoint(self, hostname: Optional[str] = None, urls: Optional[List[str]] = None):
783+
"""If you want to ignore certain httplib requests you can do so based on the hostname or URL that is being
784+
requested.
785+
786+
> NOTE: If the provider is not xray, nothing will be added to ignore list
787+
788+
Documentation
789+
--------------
790+
- https://github.com/aws/aws-xray-sdk-python#ignoring-httplib-requests
791+
792+
Parameters
793+
----------
794+
hostname : Optional, str
795+
The hostname is matched using the Python fnmatch library which does Unix glob style matching.
796+
urls: Optional, List[str]
797+
List of urls to ignore. Example `tracer.ignore_endpoint(urls=["/ignored-url"])`
798+
"""
799+
if not self._is_xray_provider():
800+
return
801+
802+
from aws_xray_sdk.ext.httplib import add_ignored # type: ignore
803+
804+
add_ignored(hostname=hostname, urls=urls)

Diff for: tests/unit/test_tracing.py

+22
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

@@ -628,3 +629,24 @@ def handler(event, context):
628629
# THEN
629630
assert in_subsegment_mock.put_annotation.call_count == 1
630631
assert in_subsegment_mock.put_annotation.call_args == mocker.call(key="ColdStart", value=True)
632+
633+
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)