Skip to content

Commit

Permalink
Remove Configuration from starlette
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jan 12, 2021
1 parent 1791371 commit e484642
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,43 @@
# 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.
from typing import Optional

from re import compile as re_compile, search
from os import environ

from starlette import applications
from starlette.routing import Match

from opentelemetry.configuration import Configuration
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.starlette.version import __version__ # noqa

_excluded_urls = Configuration()._excluded_urls("starlette")

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

def __init__(self, excluded_urls):
self._excluded_urls = excluded_urls
if self._excluded_urls:
self._regex = re_compile("|".join(excluded_urls))

def url_disabled(self, url: str) -> bool:
return bool(self._excluded_urls and search(self._regex, url))


def _get_excluded_urls():
excluded_urls = environ.get("OTEL_PYTHON_STARLETTE_EXCLUDED_URLS", [])

if excluded_urls:
excluded_urls = [
excluded_url.strip()
for excluded_url in excluded_urls.split(",")
]

return _ExcludeList(excluded_urls)


_excluded_urls = _get_excluded_urls()


class StarletteInstrumentor(BaseInstrumentor):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from starlette.testclient import TestClient

import opentelemetry.instrumentation.starlette as otel_starlette
from opentelemetry.configuration import Configuration
from opentelemetry.test.test_base import TestBase


Expand All @@ -33,15 +32,14 @@ def _create_app(self):

def setUp(self):
super().setUp()
Configuration()._reset()
self.env_patch = patch.dict(
"os.environ",
{"OTEL_PYTHON_STARLETTE_EXCLUDED_URLS": "/exclude/123,healthzz"},
)
self.env_patch.start()
self.exclude_patch = patch(
"opentelemetry.instrumentation.starlette._excluded_urls",
Configuration()._excluded_urls("starlette"),
otel_starlette._get_excluded_urls(),
)
self.exclude_patch.start()
self._instrumentor = otel_starlette.StarletteInstrumentor()
Expand Down

0 comments on commit e484642

Please sign in to comment.