From 44e8a9d08ece9c2f8f46edc0e51814cff423093d Mon Sep 17 00:00:00 2001 From: Leighton Date: Wed, 29 Apr 2020 15:47:48 -0700 Subject: [PATCH 01/13] blacklist --- ext/opentelemetry-ext-flask/CHANGELOG.md | 3 ++ ext/opentelemetry-ext-flask/README.rst | 10 ++++ .../src/opentelemetry/ext/flask/__init__.py | 51 ++++++++++++++----- .../tests/test_flask_integration.py | 35 +++++++++++-- .../src/opentelemetry/util/__init__.py | 40 +++++++++++++++ 5 files changed, 123 insertions(+), 16 deletions(-) diff --git a/ext/opentelemetry-ext-flask/CHANGELOG.md b/ext/opentelemetry-ext-flask/CHANGELOG.md index f7523f36c4d..bd2fc266944 100644 --- a/ext/opentelemetry-ext-flask/CHANGELOG.md +++ b/ext/opentelemetry-ext-flask/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Add blacklisting for paths and hosts + ([#327](https://github.com/open-telemetry/opentelemetry-python/pull/327)) + ## 0.6b0 Released 2020-03-30 diff --git a/ext/opentelemetry-ext-flask/README.rst b/ext/opentelemetry-ext-flask/README.rst index 135b2c398c7..02bbc70b671 100644 --- a/ext/opentelemetry-ext-flask/README.rst +++ b/ext/opentelemetry-ext-flask/README.rst @@ -16,6 +16,16 @@ Installation pip install opentelemetry-ext-flask +Configuration +------------- + +Blacklist +********* +Excludes certain hosts and paths from being tracked. Pass in comma delimited string into environment variables. + +Blacklisted hosts: OPENTELEMETRY_PYTHON_FLASK_BLACKLIST_HOSTS +Blacklisted paths: OPENTELEMETRY_PYTHON_FLASK_BLACKLIST_PATHS + References ---------- diff --git a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py index eb008eeadc4..8dfbd7b1b25 100644 --- a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py +++ b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py @@ -51,10 +51,14 @@ def hello(): import flask import opentelemetry.ext.wsgi as otel_wsgi -from opentelemetry import context, propagators, trace +from opentelemetry import configuration, context, propagators, trace from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor from opentelemetry.ext.flask.version import __version__ -from opentelemetry.util import time_ns +from opentelemetry.util import ( + disable_tracing_hostname, + disable_tracing_url, + time_ns +) logger = logging.getLogger(__name__) @@ -80,17 +84,18 @@ def wrapped_app(environ, start_response): environ[_ENVIRON_STARTTIME_KEY] = time_ns() def _start_response(status, response_headers, *args, **kwargs): - span = flask.request.environ.get(_ENVIRON_SPAN_KEY) - if span: - otel_wsgi.add_response_attributes( - span, status, response_headers - ) - else: - logger.warning( - "Flask environ's OpenTelemetry span " - "missing at _start_response(%s)", - status, - ) + if not _disable_trace(flask.request.url): + span = flask.request.environ.get(_ENVIRON_SPAN_KEY) + if span: + otel_wsgi.add_response_attributes( + span, status, response_headers + ) + else: + logger.warning( + "Flask environ's OpenTelemetry span " + "missing at _start_response(%s)", + status, + ) return start_response( status, response_headers, *args, **kwargs @@ -102,6 +107,9 @@ def _start_response(status, response_headers, *args, **kwargs): @self.before_request def _before_flask_request(): + # Do not trace if the url is blacklisted + if _disable_trace(flask.request.url): + return environ = flask.request.environ span_name = ( flask.request.endpoint @@ -132,6 +140,9 @@ def _before_flask_request(): @self.teardown_request def _teardown_flask_request(exc): + # Do not trace if the url is blacklisted + if _disable_trace(flask.request.url): + return activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY) if not activation: logger.warning( @@ -150,6 +161,20 @@ def _teardown_flask_request(exc): context.detach(flask.request.environ.get(_ENVIRON_TOKEN)) +def _disable_trace(url): + blacklist_hosts = configuration.Configuration().FLASK_BLACKLIST_HOSTS + blacklist_paths = configuration.Configuration().FLASK_BLACKLIST_PATHS + if blacklist_hosts: + blacklist_hosts = str.split(blacklist_hosts, ',') + if disable_tracing_hostname(url, blacklist_hosts): + return True + if blacklist_paths: + blacklist_paths = str.split(blacklist_paths, ',') + if disable_tracing_url(url, blacklist_paths): + return True + return False + + class FlaskInstrumentor(BaseInstrumentor): """A instrumentor for flask.Flask diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index 34432be3dd7..8a75948cb88 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -13,12 +13,14 @@ # limitations under the License. import unittest +from unittest.mock import patch from flask import Flask, request from werkzeug.test import Client from werkzeug.wrappers import BaseResponse from opentelemetry import trace as trace_api +from opentelemetry.configuration import Configuration from opentelemetry.test.wsgitestutil import WsgiTestBase @@ -45,7 +47,8 @@ def setUp(self): # No instrumentation code is here because it is present in the # conftest.py file next to this file. super().setUp() - + Configuration._instance = None # pylint:disable=protected-access + Configuration.__slots__ = [] self.app = Flask(__name__) def hello_endpoint(helloid): @@ -53,10 +56,22 @@ def hello_endpoint(helloid): raise ValueError(":-(") return "Hello: " + str(helloid) + def blacklist_endpoint(): + return "blacklist" + + def blacklist2_endpoint(): + return "blacklist2" + self.app.route("/hello/")(hello_endpoint) + self.app.route("/blacklist")(blacklist_endpoint) + self.app.route("/blacklist2")(blacklist2_endpoint) self.client = Client(self.app, BaseResponse) + def tearDown(self): + Configuration._instance = None # pylint:disable=protected-access + Configuration.__slots__ = [] + def test_only_strings_in_environ(self): """ Some WSGI servers (such as Gunicorn) expect keys in the environ object @@ -81,8 +96,7 @@ def test_simple(self): {"http.target": "/hello/123", "http.route": "/hello/"} ) resp = self.client.get("/hello/123") - self.assertEqual(200, resp.status_code) - self.assertEqual([b"Hello: 123"], list(resp.response)) + span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1) self.assertEqual(span_list[0].name, "hello_endpoint") @@ -126,6 +140,21 @@ def test_internal_error(self): self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER) self.assertEqual(span_list[0].attributes, expected_attrs) + @patch.dict( + "os.environ", # type: ignore + { + "OPENTELEMETRY_PYTHON_FLASK_BLACKLIST_HOSTS": "http://localhost/blacklist", + "OPENTELEMETRY_PYTHON_FLASK_BLACKLIST_PATHS": "blacklist2", + }, + ) + def test_blacklist_path(self): + self.client.get("/hello/123") + self.client.get("/blacklist") + self.client.get("/blacklist2") + span_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(span_list), 1) + self.assertEqual(span_list[0].name, "hello_endpoint") + if __name__ == "__main__": unittest.main() diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index 8701d9ffbac..46c79b98906 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import re import time from logging import getLogger from typing import Union @@ -48,3 +49,42 @@ def _load_provider(provider: str) -> Union["TracerProvider", "MeterProvider"]: "Failed to load configured provider %s", provider, ) raise + +# Pattern for matching the 'https://', 'http://', 'ftp://' part. +URL_PATTERN = '^(https?|ftp):\\/\\/' + + +def disable_tracing_url(url, blacklist_paths): + """Disable tracing on the provided blacklist paths + + If the url path starts with the blacklisted path, return True. + + :type blacklist_paths: list + :param blacklist_paths: Paths to prevent from tracing + + :rtype: bool + :returns: True if not tracing, False if tracing + """ + # Remove the 'https?|ftp://' if exists + url = re.sub(URL_PATTERN, '', url) + + # Split the url by the first '/' and get the path part + url_path = url.split('/', 1)[1] + + for path in blacklist_paths: + if url_path.startswith(path): + return True + + return False + + +def disable_tracing_hostname(url, blacklist_hostnames): + """Disable tracing for the provided blacklist URLs. + + :type blacklist_hostnames: list + :param blacklist_hostnames: URLs to prevent tracing + + :rtype: bool + :returns: True if not tracing, False if tracing + """ + return url in blacklist_hostnames From a08a4c4e223609004a19c1887224052c64534de7 Mon Sep 17 00:00:00 2001 From: Leighton Date: Wed, 29 Apr 2020 15:53:56 -0700 Subject: [PATCH 02/13] changelog --- ext/opentelemetry-ext-flask/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-flask/CHANGELOG.md b/ext/opentelemetry-ext-flask/CHANGELOG.md index bd2fc266944..3ff02def353 100644 --- a/ext/opentelemetry-ext-flask/CHANGELOG.md +++ b/ext/opentelemetry-ext-flask/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased - Add blacklisting for paths and hosts - ([#327](https://github.com/open-telemetry/opentelemetry-python/pull/327)) + ([#6300](https://github.com/open-telemetry/opentelemetry-python/pull/630)) ## 0.6b0 From 97b57deb71187bf658cbc5208404f74632d09a0f Mon Sep 17 00:00:00 2001 From: Leighton Date: Thu, 30 Apr 2020 11:43:48 -0700 Subject: [PATCH 03/13] change to excluded --- ext/opentelemetry-ext-flask/CHANGELOG.md | 2 +- ext/opentelemetry-ext-flask/README.rst | 8 +++---- .../src/opentelemetry/ext/flask/__init__.py | 20 ++++++++--------- .../tests/test_flask_integration.py | 22 +++++++++---------- .../src/opentelemetry/util/__init__.py | 22 +++++++++---------- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/ext/opentelemetry-ext-flask/CHANGELOG.md b/ext/opentelemetry-ext-flask/CHANGELOG.md index 3ff02def353..0e4641a029c 100644 --- a/ext/opentelemetry-ext-flask/CHANGELOG.md +++ b/ext/opentelemetry-ext-flask/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -- Add blacklisting for paths and hosts +- Add exclude list for paths and hosts ([#6300](https://github.com/open-telemetry/opentelemetry-python/pull/630)) ## 0.6b0 diff --git a/ext/opentelemetry-ext-flask/README.rst b/ext/opentelemetry-ext-flask/README.rst index 02bbc70b671..ee4da09bc54 100644 --- a/ext/opentelemetry-ext-flask/README.rst +++ b/ext/opentelemetry-ext-flask/README.rst @@ -19,12 +19,12 @@ Installation Configuration ------------- -Blacklist -********* +Exclude lists +************* Excludes certain hosts and paths from being tracked. Pass in comma delimited string into environment variables. -Blacklisted hosts: OPENTELEMETRY_PYTHON_FLASK_BLACKLIST_HOSTS -Blacklisted paths: OPENTELEMETRY_PYTHON_FLASK_BLACKLIST_PATHS +Excluded hosts: OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_HOSTS +Excluded paths: OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_PATHS References diff --git a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py index 8dfbd7b1b25..e8d87f04571 100644 --- a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py +++ b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py @@ -107,7 +107,7 @@ def _start_response(status, response_headers, *args, **kwargs): @self.before_request def _before_flask_request(): - # Do not trace if the url is blacklisted + # Do not trace if the url is excluded if _disable_trace(flask.request.url): return environ = flask.request.environ @@ -140,7 +140,7 @@ def _before_flask_request(): @self.teardown_request def _teardown_flask_request(exc): - # Do not trace if the url is blacklisted + # Not traced if the url is excluded if _disable_trace(flask.request.url): return activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY) @@ -162,15 +162,15 @@ def _teardown_flask_request(exc): def _disable_trace(url): - blacklist_hosts = configuration.Configuration().FLASK_BLACKLIST_HOSTS - blacklist_paths = configuration.Configuration().FLASK_BLACKLIST_PATHS - if blacklist_hosts: - blacklist_hosts = str.split(blacklist_hosts, ',') - if disable_tracing_hostname(url, blacklist_hosts): + excluded_hosts = configuration.Configuration().FLASK_EXCLUDED_HOSTS + excluded_paths = configuration.Configuration().FLASK_EXCLUDED_PATHS + if excluded_hosts: + excluded_hosts = str.split(excluded_hosts, ',') + if disable_tracing_hostname(url, excluded_hosts): return True - if blacklist_paths: - blacklist_paths = str.split(blacklist_paths, ',') - if disable_tracing_url(url, blacklist_paths): + if excluded_paths: + excluded_paths = str.split(excluded_paths, ',') + if disable_tracing_url(url, excluded_paths): return True return False diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index 8a75948cb88..d6e10bb92bc 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -56,15 +56,15 @@ def hello_endpoint(helloid): raise ValueError(":-(") return "Hello: " + str(helloid) - def blacklist_endpoint(): - return "blacklist" + def excluded_endpoint(): + return "excluded" - def blacklist2_endpoint(): - return "blacklist2" + def excluded2_endpoint(): + return "excluded2" self.app.route("/hello/")(hello_endpoint) - self.app.route("/blacklist")(blacklist_endpoint) - self.app.route("/blacklist2")(blacklist2_endpoint) + self.app.route("/excluded")(excluded_endpoint) + self.app.route("/excluded2")(excluded2_endpoint) self.client = Client(self.app, BaseResponse) @@ -143,14 +143,14 @@ def test_internal_error(self): @patch.dict( "os.environ", # type: ignore { - "OPENTELEMETRY_PYTHON_FLASK_BLACKLIST_HOSTS": "http://localhost/blacklist", - "OPENTELEMETRY_PYTHON_FLASK_BLACKLIST_PATHS": "blacklist2", + "OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_HOSTS": "http://localhost/excluded", + "OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_PATHS": "excluded2", }, ) - def test_blacklist_path(self): + def test_excluded_path(self): self.client.get("/hello/123") - self.client.get("/blacklist") - self.client.get("/blacklist2") + self.client.get("/excluded") + self.client.get("/excluded2") span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1) self.assertEqual(span_list[0].name, "hello_endpoint") diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index 46c79b98906..27fee4065d6 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -54,13 +54,13 @@ def _load_provider(provider: str) -> Union["TracerProvider", "MeterProvider"]: URL_PATTERN = '^(https?|ftp):\\/\\/' -def disable_tracing_url(url, blacklist_paths): - """Disable tracing on the provided blacklist paths +def disable_tracing_path(url, excluded_paths): + """Disable tracing on the provided excluded paths - If the url path starts with the blacklisted path, return True. + If the path starts with the excluded path, return True. - :type blacklist_paths: list - :param blacklist_paths: Paths to prevent from tracing + :type excluded_paths: list + :param excluded_paths: Paths to exclude from tracing :rtype: bool :returns: True if not tracing, False if tracing @@ -71,20 +71,20 @@ def disable_tracing_url(url, blacklist_paths): # Split the url by the first '/' and get the path part url_path = url.split('/', 1)[1] - for path in blacklist_paths: + for path in excluded_paths: if url_path.startswith(path): return True return False -def disable_tracing_hostname(url, blacklist_hostnames): - """Disable tracing for the provided blacklist URLs. +def disable_tracing_hostname(url, excluded_hostnames): + """Disable tracing for the provided excluded hostnames. - :type blacklist_hostnames: list - :param blacklist_hostnames: URLs to prevent tracing + :type excluded_hostnames: list + :param excluded_hostnames: hostnames to exclude from tracing :rtype: bool :returns: True if not tracing, False if tracing """ - return url in blacklist_hostnames + return url in excluded_hostnames From 3d25b532ea5785ae5fd8e2b1372139e72f4ef60d Mon Sep 17 00:00:00 2001 From: Leighton Date: Thu, 30 Apr 2020 12:00:27 -0700 Subject: [PATCH 04/13] fix test --- .../src/opentelemetry/ext/flask/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py index e8d87f04571..15d68d2d376 100644 --- a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py +++ b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py @@ -56,7 +56,7 @@ def hello(): from opentelemetry.ext.flask.version import __version__ from opentelemetry.util import ( disable_tracing_hostname, - disable_tracing_url, + disable_tracing_path, time_ns ) @@ -170,7 +170,7 @@ def _disable_trace(url): return True if excluded_paths: excluded_paths = str.split(excluded_paths, ',') - if disable_tracing_url(url, excluded_paths): + if disable_tracing_path(url, excluded_paths): return True return False From d2ab9f773167380a8ce5d79e732e2c654c2a3198 Mon Sep 17 00:00:00 2001 From: Leighton Date: Thu, 30 Apr 2020 12:22:09 -0700 Subject: [PATCH 05/13] typing --- .../src/opentelemetry/util/__init__.py | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index 27fee4065d6..201a9658c99 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -14,7 +14,7 @@ import re import time from logging import getLogger -from typing import Union +from typing import Sequence, Union from pkg_resources import iter_entry_points @@ -54,17 +54,9 @@ def _load_provider(provider: str) -> Union["TracerProvider", "MeterProvider"]: URL_PATTERN = '^(https?|ftp):\\/\\/' -def disable_tracing_path(url, excluded_paths): - """Disable tracing on the provided excluded paths - - If the path starts with the excluded path, return True. - - :type excluded_paths: list - :param excluded_paths: Paths to exclude from tracing - - :rtype: bool - :returns: True if not tracing, False if tracing - """ +def disable_tracing_path( + url: str, + excluded_paths: Sequence[str]) -> bool: # Remove the 'https?|ftp://' if exists url = re.sub(URL_PATTERN, '', url) @@ -78,13 +70,7 @@ def disable_tracing_path(url, excluded_paths): return False -def disable_tracing_hostname(url, excluded_hostnames): - """Disable tracing for the provided excluded hostnames. - - :type excluded_hostnames: list - :param excluded_hostnames: hostnames to exclude from tracing - - :rtype: bool - :returns: True if not tracing, False if tracing - """ +def disable_tracing_hostname( + url: str, + excluded_hostnames: Sequence[str]) -> bool: return url in excluded_hostnames From 0c913e112838efa76f5ade55dfd81b4662dc6127 Mon Sep 17 00:00:00 2001 From: Leighton Date: Thu, 30 Apr 2020 12:25:23 -0700 Subject: [PATCH 06/13] black --- .../src/opentelemetry/ext/flask/__init__.py | 6 +++--- ext/opentelemetry-ext-flask/tests/test_flask_integration.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py index 15d68d2d376..1e936da115a 100644 --- a/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py +++ b/ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py @@ -57,7 +57,7 @@ def hello(): from opentelemetry.util import ( disable_tracing_hostname, disable_tracing_path, - time_ns + time_ns, ) logger = logging.getLogger(__name__) @@ -165,11 +165,11 @@ def _disable_trace(url): excluded_hosts = configuration.Configuration().FLASK_EXCLUDED_HOSTS excluded_paths = configuration.Configuration().FLASK_EXCLUDED_PATHS if excluded_hosts: - excluded_hosts = str.split(excluded_hosts, ',') + excluded_hosts = str.split(excluded_hosts, ",") if disable_tracing_hostname(url, excluded_hosts): return True if excluded_paths: - excluded_paths = str.split(excluded_paths, ',') + excluded_paths = str.split(excluded_paths, ",") if disable_tracing_path(url, excluded_paths): return True return False diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index d6e10bb92bc..875a4972dc7 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -20,7 +20,7 @@ from werkzeug.wrappers import BaseResponse from opentelemetry import trace as trace_api -from opentelemetry.configuration import Configuration +from opentelemetry.configuration import Configuration from opentelemetry.test.wsgitestutil import WsgiTestBase @@ -96,7 +96,7 @@ def test_simple(self): {"http.target": "/hello/123", "http.route": "/hello/"} ) resp = self.client.get("/hello/123") - + span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1) self.assertEqual(span_list[0].name, "hello_endpoint") From 1c97d72af1583e92538edfa5dbebceffe2c9d83d Mon Sep 17 00:00:00 2001 From: Leighton Date: Thu, 30 Apr 2020 13:50:05 -0700 Subject: [PATCH 07/13] black --- .../src/opentelemetry/util/__init__.py | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index 201a9658c99..705336cffd3 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -34,34 +34,35 @@ def time_ns() -> int: return int(time.time() * 1e9) -def _load_provider(provider: str) -> Union["TracerProvider", "MeterProvider"]: # type: ignore +def _load_provider( + provider: str +) -> Union["TracerProvider", "MeterProvider"]: # type: ignore try: return next( # type: ignore iter_entry_points( "opentelemetry_{}".format(provider), name=getattr( # type: ignore - Configuration(), provider, "default_{}".format(provider), # type: ignore + Configuration(), + provider, + "default_{}".format(provider), # type: ignore ), ) ).load()() except Exception: # pylint: disable=broad-except - logger.error( - "Failed to load configured provider %s", provider, - ) + logger.error("Failed to load configured provider %s", provider) raise + # Pattern for matching the 'https://', 'http://', 'ftp://' part. -URL_PATTERN = '^(https?|ftp):\\/\\/' +URL_PATTERN = "^(https?|ftp):\\/\\/" -def disable_tracing_path( - url: str, - excluded_paths: Sequence[str]) -> bool: +def disable_tracing_path(url: str, excluded_paths: Sequence[str]) -> bool: # Remove the 'https?|ftp://' if exists - url = re.sub(URL_PATTERN, '', url) + url = re.sub(URL_PATTERN, "", url) # Split the url by the first '/' and get the path part - url_path = url.split('/', 1)[1] + url_path = url.split("/", 1)[1] for path in excluded_paths: if url_path.startswith(path): @@ -71,6 +72,6 @@ def disable_tracing_path( def disable_tracing_hostname( - url: str, - excluded_hostnames: Sequence[str]) -> bool: + url: str, excluded_hostnames: Sequence[str] +) -> bool: return url in excluded_hostnames From f199d74d4efa16bbf96b498732c6ad8fdc033990 Mon Sep 17 00:00:00 2001 From: Leighton Date: Thu, 30 Apr 2020 15:10:54 -0700 Subject: [PATCH 08/13] black --- opentelemetry-api/src/opentelemetry/util/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index 705336cffd3..406d50dacd7 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -35,7 +35,7 @@ def time_ns() -> int: def _load_provider( - provider: str + provider: str, ) -> Union["TracerProvider", "MeterProvider"]: # type: ignore try: return next( # type: ignore From 043fe165d5a1dca642a29d561e0fe2d3641ee6f2 Mon Sep 17 00:00:00 2001 From: Leighton Date: Thu, 30 Apr 2020 15:24:59 -0700 Subject: [PATCH 09/13] fix test --- ext/opentelemetry-ext-flask/tests/test_flask_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py index 875a4972dc7..1babfff2f56 100644 --- a/ext/opentelemetry-ext-flask/tests/test_flask_integration.py +++ b/ext/opentelemetry-ext-flask/tests/test_flask_integration.py @@ -95,7 +95,7 @@ def test_simple(self): expected_attrs = expected_attributes( {"http.target": "/hello/123", "http.route": "/hello/"} ) - resp = self.client.get("/hello/123") + self.client.get("/hello/123") span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1) From 4c1fc68e70e66684973a7e513310b13f9459dda5 Mon Sep 17 00:00:00 2001 From: Leighton Date: Thu, 30 Apr 2020 15:39:55 -0700 Subject: [PATCH 10/13] mypy --- opentelemetry-api/src/opentelemetry/util/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index 406d50dacd7..1f3a4945af3 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -41,10 +41,10 @@ def _load_provider( return next( # type: ignore iter_entry_points( "opentelemetry_{}".format(provider), - name=getattr( # type: ignore - Configuration(), + name=getattr( + Configuration(), # type: ignore provider, - "default_{}".format(provider), # type: ignore + "default_{}".format(provider), ), ) ).load()() From 67a1cd2f45b6b9a6008b73c5e19060cd6f1b4a0d Mon Sep 17 00:00:00 2001 From: Leighton Date: Fri, 1 May 2020 12:58:28 -0700 Subject: [PATCH 11/13] fix regex --- ext/opentelemetry-ext-flask/CHANGELOG.md | 2 +- .../src/opentelemetry/util/__init__.py | 20 ++++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/ext/opentelemetry-ext-flask/CHANGELOG.md b/ext/opentelemetry-ext-flask/CHANGELOG.md index 0e4641a029c..7d4d85b7194 100644 --- a/ext/opentelemetry-ext-flask/CHANGELOG.md +++ b/ext/opentelemetry-ext-flask/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased - Add exclude list for paths and hosts - ([#6300](https://github.com/open-telemetry/opentelemetry-python/pull/630)) + ([#630](https://github.com/open-telemetry/opentelemetry-python/pull/630)) ## 0.6b0 diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index 1f3a4945af3..d5e3680e5c3 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -53,22 +53,18 @@ def _load_provider( raise -# Pattern for matching the 'https://', 'http://', 'ftp://' part. -URL_PATTERN = "^(https?|ftp):\\/\\/" +# Pattern for matching up until the first '/' after the 'https://' part. +URL_PATTERN = "^(https?|ftp):\\/\\/.*\\/" def disable_tracing_path(url: str, excluded_paths: Sequence[str]) -> bool: - # Remove the 'https?|ftp://' if exists - url = re.sub(URL_PATTERN, "", url) - - # Split the url by the first '/' and get the path part - url_path = url.split("/", 1)[1] - - for path in excluded_paths: - if url_path.startswith(path): + if excluded_paths: + # Match only the part after the first '/' that is not in URL_PATTERN + regex = "{}({})".format(URL_PATTERN, "|".join(excluded_paths)) + if re.match(regex, url): return True - - return False + else: + return False def disable_tracing_hostname( From 472b86c47549d8d5e31df9b18a7bd632ba3f1454 Mon Sep 17 00:00:00 2001 From: Leighton Date: Fri, 1 May 2020 13:37:45 -0700 Subject: [PATCH 12/13] fix lint --- opentelemetry-api/src/opentelemetry/util/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index d5e3680e5c3..5a2dfc40260 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -63,8 +63,7 @@ def disable_tracing_path(url: str, excluded_paths: Sequence[str]) -> bool: regex = "{}({})".format(URL_PATTERN, "|".join(excluded_paths)) if re.match(regex, url): return True - else: - return False + return False def disable_tracing_hostname( From d7045b5424f2a83153563e162ea322fccc566a3a Mon Sep 17 00:00:00 2001 From: Leighton Date: Fri, 1 May 2020 16:36:51 -0700 Subject: [PATCH 13/13] fix regex --- ext/opentelemetry-ext-flask/README.rst | 1 + opentelemetry-api/src/opentelemetry/util/__init__.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ext/opentelemetry-ext-flask/README.rst b/ext/opentelemetry-ext-flask/README.rst index ee4da09bc54..0a4c8940770 100644 --- a/ext/opentelemetry-ext-flask/README.rst +++ b/ext/opentelemetry-ext-flask/README.rst @@ -22,6 +22,7 @@ Configuration Exclude lists ************* Excludes certain hosts and paths from being tracked. Pass in comma delimited string into environment variables. +Host refers to the entire url and path refers to the part of the url after the domain. Host matches the exact string that is given, where as path matches if the url starts with the given excluded path. Excluded hosts: OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_HOSTS Excluded paths: OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_PATHS diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index 5a2dfc40260..48c350730ea 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -54,13 +54,13 @@ def _load_provider( # Pattern for matching up until the first '/' after the 'https://' part. -URL_PATTERN = "^(https?|ftp):\\/\\/.*\\/" +_URL_PATTERN = r"(https?|ftp)://.*?/" def disable_tracing_path(url: str, excluded_paths: Sequence[str]) -> bool: if excluded_paths: - # Match only the part after the first '/' that is not in URL_PATTERN - regex = "{}({})".format(URL_PATTERN, "|".join(excluded_paths)) + # Match only the part after the first '/' that is not in _URL_PATTERN + regex = "{}({})".format(_URL_PATTERN, "|".join(excluded_paths)) if re.match(regex, url): return True return False