Skip to content

feat(tracer): ignore tracing for certain hostname(s) or url(s) #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(tracer): Ignore tracing for certain hostname(s) or url(s)
  • Loading branch information
Michael Brewer committed Dec 19, 2021
commit ec4abacfab8739046acf4170fd85624d63fc371e
24 changes: 22 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,23 @@ def _disable_xray_trace_batching(self):

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

@staticmethod
def ignore_endpoint(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.

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"])`
"""
from aws_xray_sdk.ext.httplib import add_ignored # type: ignore

add_ignored(hostname=hostname, urls=urls)
6 changes: 6 additions & 0 deletions tests/unit/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,9 @@ 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)


def test_ignore_endpoints(provider_stub, in_subsegment_mock):
provider = provider_stub(in_subsegment=in_subsegment_mock.in_subsegment)
tracer = Tracer(provider=provider)
tracer.ignore_endpoint(hostname="https://foo.com/", urls=["/bar", "/ignored"])