Skip to content

Commit ec4abac

Browse files
author
Michael Brewer
committed
feat(tracer): Ignore tracing for certain hostname(s) or url(s)
1 parent 5362a16 commit ec4abac

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

aws_lambda_powertools/tracing/tracer.py

Lines changed: 22 additions & 2 deletions
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,23 @@ def _disable_xray_trace_batching(self):
778778

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

tests/unit/test_tracing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,9 @@ def handler(event, context):
628628
# THEN
629629
assert in_subsegment_mock.put_annotation.call_count == 1
630630
assert in_subsegment_mock.put_annotation.call_args == mocker.call(key="ColdStart", value=True)
631+
632+
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"])

0 commit comments

Comments
 (0)