Skip to content
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

Add exclude lists for Django #670

Merged
merged 19 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
10 changes: 10 additions & 0 deletions ext/opentelemetry-ext-django/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ Installation

pip install opentelemetry-ext-django

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.
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved

Excluded hosts: OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_HOSTS
Excluded paths: OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_PATHS

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

from logging import getLogger

from opentelemetry import configuration
from opentelemetry.context import attach, detach
from opentelemetry.ext.django.version import __version__
from opentelemetry.ext.wsgi import (
Expand All @@ -23,6 +24,7 @@
)
from opentelemetry.propagators import extract
from opentelemetry.trace import SpanKind, get_tracer
from opentelemetry.util import disable_tracing_hostname, disable_tracing_path

try:
from django.utils.deprecation import MiddlewareMixin
Expand All @@ -32,6 +34,21 @@
_logger = getLogger(__name__)


def _disable_trace(url):
excluded_hosts = configuration.Configuration().DJANGO_EXCLUDED_HOSTS
excluded_paths = configuration.Configuration().DJANGO_EXCLUDED_PATHS

if excluded_hosts:
excluded_hosts = str.split(excluded_hosts, ",")
lzchen marked this conversation as resolved.
Show resolved Hide resolved
if disable_tracing_hostname(url, excluded_hosts):
return True
if excluded_paths:
excluded_paths = str.split(excluded_paths, ",")
lzchen marked this conversation as resolved.
Show resolved Hide resolved
if disable_tracing_path(url, excluded_paths):
return True
return False


class _DjangoMiddleware(MiddlewareMixin):
"""Django Middleware for OpenTelemetry
"""
Expand All @@ -53,6 +70,8 @@ def process_view(
# key.lower().replace('_', '-').replace("http-", "", 1): value
# for key, value in request.META.items()
# }
if _disable_trace(request.build_absolute_uri("?")):
return

environ = request.META

Expand Down Expand Up @@ -82,6 +101,8 @@ def process_exception(self, request, exception):
# Django can call this method and process_response later. In order
# to avoid __exit__ and detach from being called twice then, the
# respective keys are being removed here.
if _disable_trace(request.build_absolute_uri("?")):
return
if self._environ_activation_key in request.META.keys():
request.META[self._environ_activation_key].__exit__(
type(exception),
Expand All @@ -94,6 +115,8 @@ def process_exception(self, request, exception):
request.META.pop(self._environ_token, None)

def process_response(self, request, response):
if _disable_trace(request.build_absolute_uri("?")):
return response
if (
self._environ_activation_key in request.META.keys()
and self._environ_span_key in request.META.keys()
Expand Down
38 changes: 37 additions & 1 deletion ext/opentelemetry-ext-django/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,27 @@
# limitations under the License.

from sys import modules
from unittest.mock import patch

from django.conf import settings
from django.conf.urls import url
from django.test import Client
from django.test.utils import setup_test_environment, teardown_test_environment

from opentelemetry.configuration import Configuration
from opentelemetry.ext.django import DjangoInstrumentor
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import StatusCanonicalCode

from .views import error, traced # pylint: disable=import-error
# pylint: disable=import-error
from .views import error, excluded, excluded2, traced

urlpatterns = [
url(r"^traced/", traced),
url(r"^error/", error),
url(r"^excluded/", excluded),
url(r"^excluded2/", excluded2),
]
_django_instrumentor = DjangoInstrumentor()

Expand All @@ -43,6 +48,7 @@ def setUp(self):
super().setUp()
setup_test_environment()
_django_instrumentor.instrument()
Configuration._reset() # pylint: disable=protected-access

def tearDown(self):
super().tearDown()
Expand Down Expand Up @@ -106,3 +112,33 @@ def test_error(self):
span.attributes["http.url"], "http://testserver/error/"
)
self.assertEqual(span.attributes["http.scheme"], "http")

@patch.dict(
"os.environ", # type: ignore
{
"OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_HOSTS": (
"http://testserver/excluded/"
),
"OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_PATHS": "excluded2/",
},
)
def test_exclude(self):
Client().get("/traced/")
Client().get("/excluded/")
Client().get("/excluded2")

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)

span = spans[0]

self.assertEqual(span.name, "traced")
self.assertEqual(span.kind, SpanKind.SERVER)
self.assertEqual(span.status.canonical_code, StatusCanonicalCode.OK)
self.assertEqual(span.attributes["http.method"], "GET")
self.assertEqual(
span.attributes["http.url"], "http://testserver/traced/"
)
self.assertEqual(span.attributes["http.scheme"], "http")
self.assertEqual(span.attributes["http.status_code"], 200)
self.assertEqual(span.attributes["http.status_text"], "OK")
8 changes: 8 additions & 0 deletions ext/opentelemetry-ext-django/tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ def traced(request): # pylint: disable=unused-argument

def error(request): # pylint: disable=unused-argument
raise ValueError("error")


def excluded(request): # pylint: disable=unused-argument
return HttpResponse()


def excluded2(request): # pylint: disable=unused-argument
return HttpResponse()
30 changes: 15 additions & 15 deletions ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ def hello():
_ENVIRON_TOKEN = "opentelemetry-flask.token"


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, ",")
if disable_tracing_hostname(url, excluded_hosts):
return True
if excluded_paths:
excluded_paths = str.split(excluded_paths, ",")
if disable_tracing_path(url, excluded_paths):
return True
return False


def _rewrapped_app(wsgi_app):
def _wrapped_app(environ, start_response):
# We want to measure the time for route matching, etc.
Expand Down Expand Up @@ -163,21 +178,6 @@ def __init__(self, *args, **kwargs):
self.teardown_request(_teardown_request)


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, ",")
if disable_tracing_hostname(url, excluded_hosts):
return True
if excluded_paths:
excluded_paths = str.split(excluded_paths, ",")
if disable_tracing_path(url, excluded_paths):
return True
return False


class FlaskInstrumentor(BaseInstrumentor):
# pylint: disable=protected-access,attribute-defined-outside-init
"""An instrumentor for flask.Flask
Expand Down
108 changes: 0 additions & 108 deletions ext/opentelemetry-ext-flask/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import patch

from flask import request
from werkzeug.test import Client
from werkzeug.wrappers import BaseResponse

from opentelemetry import trace
from opentelemetry.configuration import Configuration


def expected_attributes(override_attributes):
default_attributes = {
"component": "http",
"http.method": "GET",
"http.server_name": "localhost",
"http.scheme": "http",
"host.port": 80,
"http.host": "localhost",
"http.target": "/",
"http.flavor": "1.1",
"http.status_text": "OK",
"http.status_code": 200,
}
for key, val in override_attributes.items():
default_attributes[key] = val
return default_attributes


class InstrumentationTest:
def setUp(self): # pylint: disable=invalid-name
super().setUp() # pylint: disable=no-member
Expand All @@ -66,89 +44,3 @@ def excluded2_endpoint():

# pylint: disable=attribute-defined-outside-init
self.client = Client(self.app, BaseResponse)

# pylint: disable=no-member
def test_only_strings_in_environ(self):
"""
Some WSGI servers (such as Gunicorn) expect keys in the environ object
to be strings

OpenTelemetry should adhere to this convention.
"""
nonstring_keys = set()

def assert_environ():
for key in request.environ:
if not isinstance(key, str):
nonstring_keys.add(key)
return "hi"

self.app.route("/assert_environ")(assert_environ)
self.client.get("/assert_environ")
self.assertEqual(nonstring_keys, set())

def test_simple(self):
expected_attrs = expected_attributes(
{"http.target": "/hello/123", "http.route": "/hello/<int:helloid>"}
)
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")
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
self.assertEqual(span_list[0].attributes, expected_attrs)

def test_404(self):
expected_attrs = expected_attributes(
{
"http.method": "POST",
"http.target": "/bye",
"http.status_text": "NOT FOUND",
"http.status_code": 404,
}
)

resp = self.client.post("/bye")
self.assertEqual(404, resp.status_code)
resp.close()
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
self.assertEqual(span_list[0].name, "/bye")
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
self.assertEqual(span_list[0].attributes, expected_attrs)

def test_internal_error(self):
expected_attrs = expected_attributes(
{
"http.target": "/hello/500",
"http.route": "/hello/<int:helloid>",
"http.status_text": "INTERNAL SERVER ERROR",
"http.status_code": 500,
}
)
resp = self.client.get("/hello/500")
self.assertEqual(500, resp.status_code)
resp.close()
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
self.assertEqual(span_list[0].name, "_hello_endpoint")
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
self.assertEqual(span_list[0].attributes, expected_attrs)

@patch.dict(
"os.environ", # type: ignore
{
"OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_HOSTS": (
"http://localhost/excluded"
),
"OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_PATHS": "excluded2",
},
)
def test_excluded_path(self):
self.client.get("/hello/123")
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")
Loading