Skip to content

Commit

Permalink
Use Config class methods for common purposes (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanthccv authored Dec 4, 2020
1 parent 609b025 commit a403b65
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'release/*'
pull_request:
env:
CORE_REPO_SHA: 5de7ffd7cbb555fb04d0138361a188496557080d
CORE_REPO_SHA: ce6449accf315977dd1eab940f7f49b7d894618f

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
)
from opentelemetry.propagators import extract
from opentelemetry.trace import SpanKind, get_tracer
from opentelemetry.util import ExcludeList

try:
from django.core.urlresolvers import ( # pylint: disable=no-name-in-module
Expand Down Expand Up @@ -62,18 +61,9 @@ class _DjangoMiddleware(MiddlewareMixin):
_environ_span_key = "opentelemetry-instrumentor-django.span_key"
_environ_exception_key = "opentelemetry-instrumentor-django.exception_key"

_excluded_urls = Configuration().DJANGO_EXCLUDED_URLS or []
if _excluded_urls:
_excluded_urls = ExcludeList(str.split(_excluded_urls, ","))
else:
_excluded_urls = ExcludeList(_excluded_urls)
_excluded_urls = Configuration()._excluded_urls("django")

_traced_request_attrs = [
attr.strip()
for attr in (Configuration().DJANGO_TRACED_REQUEST_ATTRS or "").split(
","
)
]
_traced_request_attrs = Configuration()._traced_request_attrs("django")

@staticmethod
def _get_span_name(request):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import StatusCode
from opentelemetry.util import ExcludeList

# pylint: disable=import-error
from .views import (
Expand Down Expand Up @@ -66,9 +65,30 @@ def setUp(self):
setup_test_environment()
_django_instrumentor.instrument()
Configuration._reset() # pylint: disable=protected-access
self.env_patch = patch.dict(
"os.environ",
{
"OTEL_PYTHON_DJANGO_EXCLUDED_URLS": "http://testserver/excluded_arg/123,excluded_noarg",
"OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS": "path_info,content_type,non_existing_variable",
},
)
self.env_patch.start()
self.exclude_patch = patch(
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._excluded_urls",
Configuration()._excluded_urls("django"),
)
self.traced_patch = patch(
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
Configuration()._traced_request_attrs("django"),
)
self.exclude_patch.start()
self.traced_patch.start()

def tearDown(self):
super().tearDown()
self.env_patch.stop()
self.exclude_patch.stop()
self.traced_patch.stop()
teardown_test_environment()
_django_instrumentor.uninstrument()

Expand Down Expand Up @@ -227,10 +247,6 @@ def test_error(self):
self.assertEqual(view_data.labels, key)
self.assertEqual(view_data.aggregator.current.count, 1)

@patch(
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._excluded_urls",
ExcludeList(["http://testserver/excluded_arg/123", "excluded_noarg"]),
)
def test_exclude_lists(self):
client = Client()
client.get("/excluded_arg/123")
Expand Down Expand Up @@ -288,28 +304,11 @@ def test_span_name_404(self):
self.assertEqual(span.name, "HTTP GET")

def test_traced_request_attrs(self):
with patch(
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
[],
):
Client().get("/span_name/1234/", CONTENT_TYPE="test/ct")
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

span = span_list[0]
self.assertNotIn("path_info", span.attributes)
self.assertNotIn("content_type", span.attributes)
self.memory_exporter.clear()

with patch(
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
["path_info", "content_type", "non_existing_variable"],
):
Client().get("/span_name/1234/", CONTENT_TYPE="test/ct")
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

span = span_list[0]
self.assertEqual(span.attributes["path_info"], "/span_name/1234/")
self.assertEqual(span.attributes["content_type"], "test/ct")
self.assertNotIn("non_existing_variable", span.attributes)
Client().get("/span_name/1234/", CONTENT_TYPE="test/ct")
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

span = span_list[0]
self.assertEqual(span.attributes["path_info"], "/span_name/1234/")
self.assertEqual(span.attributes["content_type"], "test/ct")
self.assertNotIn("non_existing_variable", span.attributes)
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def on_get(self, req, resp):
http_status_to_status_code,
)
from opentelemetry.trace.status import Status
from opentelemetry.util import ExcludeList, time_ns
from opentelemetry.util import time_ns

_logger = getLogger(__name__)

Expand All @@ -68,15 +68,8 @@ def on_get(self, req, resp):
_ENVIRON_TOKEN = "opentelemetry-falcon.token"
_ENVIRON_EXC = "opentelemetry-falcon.exc"


def get_excluded_urls():
urls = configuration.Configuration().FALCON_EXCLUDED_URLS or ""
if urls:
urls = str.split(urls, ",")
return ExcludeList(urls)


_excluded_urls = get_excluded_urls()
cfg = configuration.Configuration()
_excluded_urls = cfg._excluded_urls("falcon")


class FalconInstrumentor(BaseInstrumentor):
Expand Down Expand Up @@ -156,12 +149,7 @@ class _TraceMiddleware:

def __init__(self, tracer=None, traced_request_attrs=None):
self.tracer = tracer
self._traced_request_attrs = traced_request_attrs or [
attr.strip()
for attr in (
Configuration().FALCON_TRACED_REQUEST_ATTRS or ""
).split(",")
]
self._traced_request_attrs = cfg._traced_request_attrs("falcon")

def process_request(self, req, resp):
span = req.env.get(_ENVIRON_SPAN_KEY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

from falcon import testing

from opentelemetry.configuration import Configuration
from opentelemetry.instrumentation.falcon import FalconInstrumentor
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace.status import StatusCode
from opentelemetry.util import ExcludeList

from .app import make_app

Expand All @@ -29,6 +29,30 @@ def setUp(self):
super().setUp()
FalconInstrumentor().instrument()
self.app = make_app()
# pylint: disable=protected-access
Configuration()._reset()
self.env_patch = patch.dict(
"os.environ",
{
"OTEL_PYTHON_FALCON_EXCLUDED_URLS": "ping",
"OTEL_PYTHON_FALCON_TRACED_REQUEST_ATTRS": "query_string",
},
)
self.env_patch.start()
self.exclude_patch = patch(
"opentelemetry.instrumentation.falcon._excluded_urls",
Configuration()._excluded_urls("falcon"),
)
middleware = self.app._middleware[0][ # pylint:disable=W0212
0
].__self__
self.traced_patch = patch.object(
middleware,
"_traced_request_attrs",
Configuration()._traced_request_attrs("falcon"),
)
self.exclude_patch.start()
self.traced_patch.start()

def client(self):
return testing.TestClient(self.app)
Expand All @@ -37,6 +61,9 @@ def tearDown(self):
super().tearDown()
with self.disable_logging():
FalconInstrumentor().uninstrument()
self.env_patch.stop()
self.exclude_patch.stop()
self.traced_patch.stop()

def test_get(self):
self._test_method("GET")
Expand Down Expand Up @@ -155,10 +182,6 @@ def test_uninstrument(self):
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)

@patch(
"opentelemetry.instrumentation.falcon._excluded_urls",
ExcludeList(["ping"]),
)
def test_exclude_lists(self):
self.client().simulate_get(path="/ping")
span_list = self.memory_exporter.get_finished_spans()
Expand All @@ -171,19 +194,9 @@ def test_exclude_lists(self):
def test_traced_request_attributes(self):
self.client().simulate_get(path="/hello?q=abc")
span = self.memory_exporter.get_finished_spans()[0]
self.assertNotIn("query_string", span.attributes)
self.memory_exporter.clear()

middleware = self.app._middleware[0][ # pylint:disable=W0212
0
].__self__
with patch.object(
middleware, "_traced_request_attrs", ["query_string"]
):
self.client().simulate_get(path="/hello?q=abc")
span = self.memory_exporter.get_finished_spans()[0]
self.assertIn("query_string", span.attributes)
self.assertEqual(span.attributes["query_string"], "q=abc")
self.assertIn("query_string", span.attributes)
self.assertEqual(span.attributes["query_string"], "q=abc")
self.assertNotIn("not_available_attr", span.attributes)

def test_traced_not_recording(self):
mock_tracer = Mock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def hello():
from opentelemetry import configuration, context, propagators, trace
from opentelemetry.instrumentation.flask.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.util import ExcludeList, time_ns
from opentelemetry.util import time_ns

_logger = getLogger(__name__)

Expand All @@ -65,14 +65,7 @@ def hello():
_ENVIRON_TOKEN = "opentelemetry-flask.token"


def get_excluded_urls():
urls = configuration.Configuration().FLASK_EXCLUDED_URLS or []
if urls:
urls = str.split(urls, ",")
return ExcludeList(urls)


_excluded_urls = get_excluded_urls()
_excluded_urls = configuration.Configuration()._excluded_urls("flask")


def get_default_span_name():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from flask import Flask, request

from opentelemetry import trace
from opentelemetry.configuration import Configuration
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.test.test_base import TestBase
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.util import ExcludeList

# pylint: disable=import-error
from .base_test import InstrumentationTest
Expand Down Expand Up @@ -54,8 +54,23 @@ def setUp(self):

self._common_initialization()

self.env_patch = patch.dict(
"os.environ",
{
"OTEL_PYTHON_FLASK_EXCLUDED_URLS": "http://localhost/excluded_arg/123,excluded_noarg"
},
)
self.env_patch.start()
self.exclude_patch = patch(
"opentelemetry.instrumentation.flask._excluded_urls",
Configuration()._excluded_urls("flask"),
)
self.exclude_patch.start()

def tearDown(self):
super().tearDown()
self.env_patch.stop()
self.exclude_patch.stop()
with self.disable_logging():
FlaskInstrumentor().uninstrument_app(self.app)

Expand Down Expand Up @@ -158,10 +173,6 @@ def test_internal_error(self):
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
self.assertEqual(span_list[0].attributes, expected_attrs)

@patch(
"opentelemetry.instrumentation.flask._excluded_urls",
ExcludeList(["http://localhost/excluded_arg/123", "excluded_noarg"]),
)
def test_exclude_lists(self):
self.client.get("/excluded_arg/123")
span_list = self.memory_exporter.get_finished_spans()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import opentelemetry.instrumentation.wsgi as otel_wsgi
from opentelemetry import configuration, context, propagators, trace
from opentelemetry.instrumentation.pyramid.version import __version__
from opentelemetry.util import ExcludeList, time_ns
from opentelemetry.util import time_ns

TWEEN_NAME = "opentelemetry.instrumentation.pyramid.trace_tween_factory"
SETTING_TRACE_ENABLED = "opentelemetry-pyramid.trace_enabled"
Expand All @@ -22,14 +22,7 @@
_logger = getLogger(__name__)


def get_excluded_urls():
urls = configuration.Configuration().PYRAMID_EXCLUDED_URLS or []
if urls:
urls = str.split(urls, ",")
return ExcludeList(urls)


_excluded_urls = get_excluded_urls()
_excluded_urls = configuration.Configuration()._excluded_urls("pyramid")


def includeme(config):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from pyramid.config import Configurator

from opentelemetry import trace
from opentelemetry.configuration import Configuration
from opentelemetry.instrumentation.pyramid import PyramidInstrumentor
from opentelemetry.test.test_base import TestBase
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.util import ExcludeList

# pylint: disable=import-error
from .pyramid_base_test import InstrumentationTest
Expand Down Expand Up @@ -54,6 +54,19 @@ def setUp(self):

self._common_initialization(self.config)

self.env_patch = patch.dict(
"os.environ",
{
"OTEL_PYTHON_PYRAMID_EXCLUDED_URLS": "http://localhost/excluded_arg/123,excluded_noarg"
},
)
self.env_patch.start()
self.exclude_patch = patch(
"opentelemetry.instrumentation.pyramid.callbacks._excluded_urls",
Configuration()._excluded_urls("pyramid"),
)
self.exclude_patch.start()

def tearDown(self):
super().tearDown()
with self.disable_logging():
Expand Down Expand Up @@ -187,10 +200,6 @@ def test_warnings(self, mock_logger):
self.assertEqual(len(span_list), 0)
self.assertEqual(mock_logger.warning.called, True)

@patch(
"opentelemetry.instrumentation.pyramid.callbacks._excluded_urls",
ExcludeList(["http://localhost/excluded_arg/123", "excluded_noarg"]),
)
def test_exclude_lists(self):
self.client.get("/excluded_arg/123")
span_list = self.memory_exporter.get_finished_spans()
Expand Down
Loading

0 comments on commit a403b65

Please sign in to comment.