Skip to content

Commit

Permalink
feat(tracer): ignore tracing for certain hostname(s) or url(s) (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Brewer authored Dec 22, 2021
1 parent 9abeb32 commit ccefa87
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
28 changes: 26 additions & 2 deletions aws_lambda_powertools/tracing/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import numbers
import os
from typing import Any, Callable, Dict, Optional, Sequence, Union, cast, overload
from typing import Any, Callable, Dict, List, Optional, Sequence, Union, cast, overload

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

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

def _is_xray_provider(self):
return "aws_xray_sdk" in self.provider.__module__

def ignore_endpoint(self, hostname: Optional[str] = None, urls: Optional[List[str]] = None):
"""If you want to ignore certain httplib requests you can do so based on the hostname or URL that is being
requested.
> NOTE: If the provider is not xray, nothing will be added to ignore list
Documentation
--------------
- https://github.com/aws/aws-xray-sdk-python#ignoring-httplib-requests
Parameters
----------
hostname : Optional, str
The hostname is matched using the Python fnmatch library which does Unix glob style matching.
urls: Optional, List[str]
List of urls to ignore. Example `tracer.ignore_endpoint(urls=["/ignored-url"])`
"""
if not self._is_xray_provider():
return

from aws_xray_sdk.ext.httplib import add_ignored # type: ignore

add_ignored(hostname=hostname, urls=urls)
22 changes: 22 additions & 0 deletions tests/unit/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from typing import NamedTuple
from unittest import mock
from unittest.mock import MagicMock

import pytest

Expand Down Expand Up @@ -628,3 +629,24 @@ def handler(event, context):
# THEN
assert in_subsegment_mock.put_annotation.call_count == 1
assert in_subsegment_mock.put_annotation.call_args == mocker.call(key="ColdStart", value=True)


@mock.patch("aws_xray_sdk.ext.httplib.add_ignored")
def test_ignore_endpoints_xray_sdk(mock_add_ignored: MagicMock):
# GIVEN a xray sdk provider
tracer = Tracer()
# WHEN we call ignore_endpoint
tracer.ignore_endpoint(hostname="https://www.foo.com/", urls=["/bar", "/ignored"])
# THEN call xray add_ignored
assert mock_add_ignored.call_count == 1
mock_add_ignored.assert_called_with(hostname="https://www.foo.com/", urls=["/bar", "/ignored"])


@mock.patch("aws_xray_sdk.ext.httplib.add_ignored")
def test_ignore_endpoints_mocked_provider(mock_add_ignored: MagicMock, provider_stub, in_subsegment_mock):
# GIVEN a mock provider
tracer = Tracer(provider=provider_stub(in_subsegment=in_subsegment_mock.in_subsegment))
# WHEN we call ignore_endpoint
tracer.ignore_endpoint(hostname="https://foo.com/")
# THEN don't call xray add_ignored
assert mock_add_ignored.call_count == 0

0 comments on commit ccefa87

Please sign in to comment.