Use Config class methods for common purposes (#220)

This commit is contained in:
Srikanth Chekuri
2020-12-04 18:18:25 +00:00
committed by GitHub
parent 609b025cd9
commit a403b65183
11 changed files with 130 additions and 135 deletions

View File

@ -6,7 +6,7 @@ on:
- 'release/*'
pull_request:
env:
CORE_REPO_SHA: 5de7ffd7cbb555fb04d0138361a188496557080d
CORE_REPO_SHA: ce6449accf315977dd1eab940f7f49b7d894618f
jobs:
build:

View File

@ -28,7 +28,6 @@ from opentelemetry.instrumentation.wsgi import (
)
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
@ -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):

View File

@ -28,7 +28,6 @@ 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 import ExcludeList
# pylint: disable=import-error
from .views import (
@ -66,9 +65,30 @@ class TestMiddleware(TestBase, WsgiTestBase):
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()
@ -227,10 +247,6 @@ class TestMiddleware(TestBase, WsgiTestBase):
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")
@ -288,28 +304,11 @@ class TestMiddleware(TestBase, WsgiTestBase):
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)
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)
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)

View File

@ -58,7 +58,7 @@ from opentelemetry.instrumentation.utils import (
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__)
@ -68,15 +68,8 @@ _ENVIRON_ACTIVATION_KEY = "opentelemetry-falcon.activation_key"
_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):
@ -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)

View File

@ -16,10 +16,10 @@ from unittest.mock import Mock, patch
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
@ -29,6 +29,30 @@ class TestFalconInstrumentation(TestBase):
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)
@ -37,6 +61,9 @@ class TestFalconInstrumentation(TestBase):
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")
@ -155,10 +182,6 @@ class TestFalconInstrumentation(TestBase):
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()
@ -171,19 +194,9 @@ class TestFalconInstrumentation(TestBase):
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()

View File

@ -55,7 +55,7 @@ import opentelemetry.instrumentation.wsgi as otel_wsgi
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__)
@ -65,14 +65,7 @@ _ENVIRON_ACTIVATION_KEY = "opentelemetry-flask.activation_key"
_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():

View File

@ -17,10 +17,10 @@ from unittest.mock import Mock, patch
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
@ -54,8 +54,23 @@ class TestProgrammatic(InstrumentationTest, TestBase, WsgiTestBase):
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)
@ -158,10 +173,6 @@ class TestProgrammatic(InstrumentationTest, TestBase, WsgiTestBase):
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()

View File

@ -8,7 +8,7 @@ from pyramid.tweens import EXCVIEW
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"
@ -22,14 +22,7 @@ _ENVIRON_TOKEN = "opentelemetry-pyramid.token"
_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):

View File

@ -17,10 +17,10 @@ from unittest.mock import Mock, patch
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
@ -54,6 +54,19 @@ class TestProgrammatic(InstrumentationTest, TestBase, WsgiTestBase):
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():
@ -187,10 +200,6 @@ class TestProgrammatic(InstrumentationTest, TestBase, WsgiTestBase):
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()

View File

@ -56,7 +56,7 @@ from opentelemetry.instrumentation.utils import (
)
from opentelemetry.trace.propagation.textmap import DictGetter
from opentelemetry.trace.status import Status
from opentelemetry.util import ExcludeList, time_ns
from opentelemetry.util import time_ns
from .client import fetch_async # pylint: disable=E0401
@ -65,25 +65,9 @@ _TraceContext = namedtuple("TraceContext", ["activation", "span", "token"])
_HANDLER_CONTEXT_KEY = "_otel_trace_context_key"
_OTEL_PATCHED_KEY = "_otel_patched_key"
def get_excluded_urls():
urls = configuration.Configuration().TORNADO_EXCLUDED_URLS or ""
if urls:
urls = str.split(urls, ",")
return ExcludeList(urls)
def get_traced_request_attrs():
attrs = configuration.Configuration().TORNADO_TRACED_REQUEST_ATTRS or ""
if attrs:
attrs = [attr.strip() for attr in attrs.split(",")]
else:
attrs = []
return attrs
_excluded_urls = get_excluded_urls()
_traced_attrs = get_traced_request_attrs()
cfg = configuration.Configuration()
_excluded_urls = cfg._excluded_urls("tornado")
_traced_attrs = cfg._traced_request_attrs("tornado")
carrier_getter = DictGetter()

View File

@ -18,6 +18,7 @@ from unittest.mock import Mock, patch
from tornado.testing import AsyncHTTPTestCase
from opentelemetry import trace
from opentelemetry.configuration import Configuration
from opentelemetry.instrumentation.tornado import (
TornadoInstrumentor,
patch_handler_class,
@ -25,7 +26,6 @@ from opentelemetry.instrumentation.tornado import (
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import SpanKind
from opentelemetry.util import ExcludeList
from .tornado_test_app import (
AsyncHandler,
@ -44,9 +44,32 @@ class TornadoTest(AsyncHTTPTestCase, TestBase):
def setUp(self):
TornadoInstrumentor().instrument()
super().setUp()
# pylint: disable=protected-access
Configuration()._reset()
self.env_patch = patch.dict(
"os.environ",
{
"OTEL_PYTHON_TORNADO_EXCLUDED_URLS": "healthz,ping",
"OTEL_PYTHON_TORNADO_TRACED_REQUEST_ATTRS": "uri,full_url,query",
},
)
self.env_patch.start()
self.exclude_patch = patch(
"opentelemetry.instrumentation.tornado._excluded_urls",
Configuration()._excluded_urls("tornado"),
)
self.traced_patch = patch(
"opentelemetry.instrumentation.tornado._traced_attrs",
Configuration()._traced_request_attrs("tornado"),
)
self.exclude_patch.start()
self.traced_patch.start()
def tearDown(self):
TornadoInstrumentor().uninstrument()
self.env_patch.stop()
self.exclude_patch.stop()
self.traced_patch.stop()
super().tearDown()
@ -326,10 +349,6 @@ class TestTornadoInstrumentation(TornadoTest):
},
)
@patch(
"opentelemetry.instrumentation.tornado._excluded_urls",
ExcludeList(["healthz", "ping"]),
)
def test_exclude_lists(self):
def test_excluded(path):
self.fetch(path)
@ -354,18 +373,14 @@ class TestTornadoInstrumentation(TornadoTest):
test_excluded("/healthz")
test_excluded("/ping")
@patch(
"opentelemetry.instrumentation.tornado._traced_attrs",
["uri", "full_url", "query"],
)
def test_traced_attrs(self):
self.fetch("/ping?q=abc&b=123")
self.fetch("/pong?q=abc&b=123")
spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
self.assertEqual(len(spans), 2)
server_span = spans[0]
self.assertEqual(server_span.kind, SpanKind.SERVER)
self.assert_span_has_attributes(
server_span, {"uri": "/ping?q=abc&b=123", "query": "q=abc&b=123"}
server_span, {"uri": "/pong?q=abc&b=123", "query": "q=abc&b=123"}
)
self.memory_exporter.clear()