Skip to content

Commit

Permalink
Add support for generic OTEL_PYTHON_EXCLUDED_URLS variable
Browse files Browse the repository at this point in the history
Use `OTEL_PYTHON_EXCLUDED_URLS` environment variable as a fallback of
`OTEL_PYTHON_{instrumentation}_EXCLUDED_URLS`.
  • Loading branch information
adamantike committed Nov 2, 2021
1 parent 671aea3 commit f0a3bce
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#781](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/781))
- `opentelemetry-instrumentation-aws-lambda` Add instrumentation for AWS Lambda Service - Implementation (Part 2/2)
([#777](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/777))
- Add support for generic OTEL_PYTHON_EXCLUDED_URLS variable
([#790](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/790))

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_DJANGO_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_DJANGO_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.
For example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Configuration

Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_FALCON_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_FALCON_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.

For example,

Expand All @@ -34,7 +35,7 @@ will exclude requests such as ``https://site/client/123/info`` and ``https://sit
Request attributes
********************
To extract certain attributes from Falcon's request object and use them as span attributes, set the environment variable ``OTEL_PYTHON_FALCON_TRACED_REQUEST_ATTRS`` to a comma
delimited list of request attribute names.
delimited list of request attribute names.

For example,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_FALCON_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_FALCON_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.
For example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ async def foobar():
Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_FASTAPI_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_FASTAPI_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.
For example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Configuration

Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_FLASK_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_FLASK_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.

For example,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def hello():
Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_FLASK_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_FLASK_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.
For example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_PYRAMID_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_PYRAMID_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.
For example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ Installation
::

pip install opentelemetry-instrumentation-requests

Configuration
-------------

Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_REQUESTS_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_REQUESTS_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.

For example,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_REQUESTS_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_REQUESTS_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.
For example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def home(request):
Exclude lists
*************
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_STARLETTE_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_EXCLUDED_URLS`` or ``OTEL_PYTHON_STARLETTE_EXCLUDED_URLS``
with comma delimited regexes representing which URLs to exclude.
For example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
from os import environ
from re import compile as re_compile
from re import search
from typing import Iterable
from urllib.parse import urlparse, urlunparse


class ExcludeList:
"""Class to exclude certain paths (given as a list of regexes) from tracing requests"""

def __init__(self, excluded_urls):
def __init__(self, excluded_urls: Iterable[str]):
self._excluded_urls = excluded_urls
if self._excluded_urls:
self._regex = re_compile("|".join(excluded_urls))
Expand All @@ -47,24 +48,28 @@ def get_traced_request_attrs(instrumentation):
return traced_request_attrs


def get_excluded_urls(instrumentation):
def get_excluded_urls(instrumentation: str) -> ExcludeList:
excluded_urls = environ.get(
_root.format(f"{instrumentation}_EXCLUDED_URLS"), []
_root.format(f"{instrumentation}_EXCLUDED_URLS"), ""
)
if not excluded_urls:
excluded_urls = environ.get(_root.format("EXCLUDED_URLS"), "")

return parse_excluded_urls(excluded_urls)


def parse_excluded_urls(excluded_urls):
def parse_excluded_urls(excluded_urls: str) -> ExcludeList:
"""
Small helper to put an arbitrary url list inside of ExcludeList
"""
if excluded_urls:
excluded_urls = [
excluded_url_list = [
excluded_url.strip() for excluded_url in excluded_urls.split(",")
]
else:
excluded_url_list = []

return ExcludeList(excluded_urls)
return ExcludeList(excluded_url_list)


def remove_url_credentials(url: str) -> str:
Expand Down

0 comments on commit f0a3bce

Please sign in to comment.