Skip to content

Commit

Permalink
Add HTTP util
Browse files Browse the repository at this point in the history
This moves the WSGI and ASGI instrumentations and some other
functionality to a new package for HTTP-related functionality.
  • Loading branch information
ocelotl committed Jan 21, 2021
1 parent 55a5861 commit 2be6c6e
Show file tree
Hide file tree
Showing 43 changed files with 160 additions and 584 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ disable=missing-docstring,
super-init-not-called, # temp-pylint-upgrade
invalid-overridden-method, # temp-pylint-upgrade
missing-module-docstring, # temp-pylint-upgrade
import-error, # needed as a workaround as reported here: https://github.com/open-telemetry/opentelemetry-python-contrib/issues/290

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ package_dir=
packages=find_namespace:
install_requires =
django >= 1.10
opentelemetry-instrumentation-wsgi == 0.18.dev0
opentelemetry-util-http == 0.18.dev0
opentelemetry-instrumentation == 0.18.dev0
opentelemetry-api == 0.18.dev0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@
# limitations under the License.

from logging import getLogger
from os import environ
from re import compile as re_compile
from re import search
from time import time

from django.conf import settings

from opentelemetry.context import attach, detach
from opentelemetry.instrumentation.django.version import __version__
from opentelemetry.instrumentation.utils import extract_attributes_from_object
from opentelemetry.instrumentation.wsgi import (
from opentelemetry.propagators import extract
from opentelemetry.trace import SpanKind, get_tracer
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs
from opentelemetry.util.http.wsgi import (
add_response_attributes,
carrier_getter,
collect_request_attributes,
)
from opentelemetry.propagators import extract
from opentelemetry.trace import SpanKind, get_tracer

try:
from django.core.urlresolvers import ( # pylint: disable=no-name-in-module
Expand All @@ -53,46 +51,6 @@
]


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))


_root = r"OTEL_PYTHON_{}"


def _get_traced_request_attrs():
traced_request_attrs = environ.get(
_root.format("DJANGO_TRACED_REQUEST_ATTRS"), []
)

if traced_request_attrs:
traced_request_attrs = [
traced_request_attr.strip()
for traced_request_attr in traced_request_attrs.split(",")
]

return traced_request_attrs


def _get_excluded_urls():
excluded_urls = environ.get(_root.format("DJANGO_EXCLUDED_URLS"), [])

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

return _ExcludeList(excluded_urls)


class _DjangoMiddleware(MiddlewareMixin):
"""Django Middleware for OpenTelemetry"""

Expand All @@ -103,8 +61,8 @@ class _DjangoMiddleware(MiddlewareMixin):
_environ_span_key = "opentelemetry-instrumentor-django.span_key"
_environ_exception_key = "opentelemetry-instrumentor-django.exception_key"

_traced_request_attrs = _get_traced_request_attrs()
_excluded_urls = _get_excluded_urls()
_traced_request_attrs = get_traced_request_attrs("DJANGO")
_excluded_urls = get_excluded_urls("DJANGO")

@staticmethod
def _get_span_name(request):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@
from django.test.utils import setup_test_environment, teardown_test_environment

from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.django.middleware import (
_get_excluded_urls,
_get_traced_request_attrs,
)
from opentelemetry.sdk.util import get_dict_as_key
from opentelemetry.test.test_base import TestBase
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import StatusCode
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs

# pylint: disable=import-error
from .views import (
Expand Down Expand Up @@ -77,11 +74,11 @@ def setUp(self):
self.env_patch.start()
self.exclude_patch = patch(
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._excluded_urls",
_get_excluded_urls(),
get_excluded_urls("DJANGO"),
)
self.traced_patch = patch(
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
_get_traced_request_attrs(),
get_traced_request_attrs("DJANGO"),
)
self.exclude_patch.start()
self.traced_patch.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ package_dir=
packages=find_namespace:
install_requires =
falcon ~= 2.0
opentelemetry-instrumentation-wsgi == 0.18.dev0
opentelemetry-util-http == 0.18.dev0
opentelemetry-instrumentation == 0.18.dev0
opentelemetry-api == 0.18.dev0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"""
This library builds on the OpenTelemetry WSGI middleware to track web requests
in Falcon applications. In addition to opentelemetry-instrumentation-wsgi,
in Falcon applications. In addition to opentelemetry-util-http,
it supports falcon-specific features such as:
* The Falcon resource and method name is used as the Span name.
Expand Down Expand Up @@ -44,14 +44,11 @@ def on_get(self, req, resp):
"""

from logging import getLogger
from os import environ
from re import compile as re_compile
from re import search
from sys import exc_info

import falcon

import opentelemetry.instrumentation.wsgi as otel_wsgi
import opentelemetry.util.http.wsgi as otel_wsgi
from opentelemetry import context, propagators, trace
from opentelemetry.instrumentation.falcon.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
Expand All @@ -61,6 +58,7 @@ def on_get(self, req, resp):
)
from opentelemetry.trace.status import Status
from opentelemetry.util import time_ns
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs

_logger = getLogger(__name__)

Expand All @@ -71,48 +69,8 @@ def on_get(self, req, resp):
_ENVIRON_EXC = "opentelemetry-falcon.exc"


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))


_root = r"OTEL_PYTHON_{}"


def _get_traced_request_attrs():
traced_request_attrs = environ.get(
_root.format("FALCON_TRACED_REQUEST_ATTRS"), []
)

if traced_request_attrs:
traced_request_attrs = [
traced_request_attr.strip()
for traced_request_attr in traced_request_attrs.split(",")
]

return traced_request_attrs


def _get_excluded_urls():
excluded_urls = environ.get(_root.format("FALCON_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()
_traced_request_attrs = _get_traced_request_attrs()
_excluded_urls = get_excluded_urls("FALCON")
_traced_request_attrs = get_traced_request_attrs("FALCON")


class FalconInstrumentor(BaseInstrumentor):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

from falcon import testing

from opentelemetry.instrumentation.falcon import (
FalconInstrumentor,
_get_excluded_urls,
_get_traced_request_attrs,
)
from opentelemetry.instrumentation.falcon import FalconInstrumentor
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace.status import StatusCode
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs

from .app import make_app

Expand All @@ -43,13 +40,15 @@ def setUp(self):
self.env_patch.start()
self.exclude_patch = patch(
"opentelemetry.instrumentation.falcon._excluded_urls",
_get_excluded_urls(),
get_excluded_urls("FALCON"),
)
middleware = self.app._middleware[0][ # pylint:disable=W0212
0
].__self__
self.traced_patch = patch.object(
middleware, "_traced_request_attrs", _get_traced_request_attrs(),
middleware,
"_traced_request_attrs",
get_traced_request_attrs("FALCON"),
)
self.exclude_patch.start()
self.traced_patch.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ package_dir=
packages=find_namespace:
install_requires =
opentelemetry-api == 0.18.dev0
opentelemetry-instrumentation-asgi == 0.18.dev0
opentelemetry-util-http == 0.18.dev0

[options.entry_points]
opentelemetry_instrumentor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

import fastapi
from starlette.routing import Match

from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
from opentelemetry.instrumentation.fastapi.version import __version__ # noqa
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.util.http import get_excluded_urls
from opentelemetry.util.http.asgi import OpenTelemetryMiddleware


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_FASTAPI_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()
_excluded_urls = get_excluded_urls("FASTAPI")


class FastAPIInstrumentor(BaseInstrumentor):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import opentelemetry.instrumentation.fastapi as otel_fastapi
from opentelemetry.test.test_base import TestBase
from opentelemetry.util.http import get_excluded_urls


class TestFastAPIManualInstrumentation(TestBase):
Expand All @@ -37,7 +38,7 @@ def setUp(self):
self.env_patch.start()
self.exclude_patch = patch(
"opentelemetry.instrumentation.fastapi._excluded_urls",
otel_fastapi._get_excluded_urls(),
get_excluded_urls("FASTAPI"),
)
self.exclude_patch.start()
self._instrumentor = otel_fastapi.FastAPIInstrumentor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ package_dir=
packages=find_namespace:
install_requires =
flask ~= 1.0
opentelemetry-instrumentation-wsgi == 0.18.dev0
opentelemetry-util-http == 0.18.dev0
opentelemetry-instrumentation == 0.18.dev0
opentelemetry-api == 0.18.dev0

Expand Down
Loading

0 comments on commit 2be6c6e

Please sign in to comment.