Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jan 12, 2021
1 parent 432ae70 commit c4a36ad
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

from django.conf import settings

Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(self, excluded_urls):
def url_disabled(self, url: str) -> bool:
return bool(self._excluded_urls and search(self._regex, url))


_root = r"OTEL_PYTHON_{}"


Expand All @@ -85,8 +87,7 @@ def _get_excluded_urls():

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

return _ExcludeList(excluded_urls)
Expand Down Expand Up @@ -153,21 +154,21 @@ def process_request(self, request):
# pylint:disable=W0212
request._otel_start_time = time()

environ = request.META
request_meta = request.META

token = attach(extract(carrier_getter, environ))
token = attach(extract(carrier_getter, request_meta))

tracer = get_tracer(__name__, __version__)

span = tracer.start_span(
self._get_span_name(request),
kind=SpanKind.SERVER,
start_time=environ.get(
start_time=request_meta.get(
"opentelemetry-instrumentor-django.starttime_key"
),
)

attributes = collect_request_attributes(environ)
attributes = collect_request_attributes(request_meta)
# pylint:disable=W0212
request._otel_labels = self._get_metric_labels_from_attributes(
attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.django.middleware import (
_get_excluded_urls, _get_traced_request_attrs
_get_excluded_urls,
_get_traced_request_attrs,
)
from opentelemetry.sdk.util import get_dict_as_key
from opentelemetry.test.test_base import TestBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ def on_get(self, req, resp):
---
"""

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

import falcon

Expand Down Expand Up @@ -81,6 +82,7 @@ def __init__(self, excluded_urls):
def url_disabled(self, url: str) -> bool:
return bool(self._excluded_urls and search(self._regex, url))


_root = r"OTEL_PYTHON_{}"


Expand All @@ -103,8 +105,7 @@ def _get_excluded_urls():

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

return _ExcludeList(excluded_urls)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from falcon import testing

from opentelemetry.instrumentation.falcon import (
FalconInstrumentor, _get_excluded_urls, _get_traced_request_attrs
FalconInstrumentor,
_get_excluded_urls,
_get_traced_request_attrs,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace.status import StatusCode
Expand Down Expand Up @@ -47,9 +49,7 @@ def setUp(self):
0
].__self__
self.traced_patch = patch.object(
middleware,
"_traced_request_attrs",
_get_traced_request_attrs(),
middleware, "_traced_request_attrs", _get_traced_request_attrs(),
)
self.exclude_patch.start()
self.traced_patch.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

from starlette.routing import Match
import fastapi
from starlette.routing import Match

from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
from opentelemetry.instrumentation.fastapi.version import __version__ # noqa
Expand All @@ -40,8 +41,7 @@ def _get_excluded_urls():

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

return _ExcludeList(excluded_urls)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def hello():
"""

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

import flask

Expand Down Expand Up @@ -84,8 +85,7 @@ def _get_excluded_urls():

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

return _ExcludeList(excluded_urls)
Expand All @@ -104,12 +104,12 @@ def get_default_span_name():


def _rewrapped_app(wsgi_app):
def _wrapped_app(environ, start_response):
def _wrapped_app(wrapped_app_environ, start_response):
# We want to measure the time for route matching, etc.
# In theory, we could start the span here and use
# update_name later but that API is "highly discouraged" so
# we better avoid it.
environ[_ENVIRON_STARTTIME_KEY] = time_ns()
wrapped_app_environ[_ENVIRON_STARTTIME_KEY] = time_ns()

def _start_response(status, response_headers, *args, **kwargs):
if not _excluded_urls.url_disabled(flask.request.url):
Expand All @@ -128,7 +128,7 @@ def _start_response(status, response_headers, *args, **kwargs):

return start_response(status, response_headers, *args, **kwargs)

return wsgi_app(environ, _start_response)
return wsgi_app(wrapped_app_environ, _start_response)

return _wrapped_app

Expand All @@ -138,21 +138,25 @@ def _before_request():
if _excluded_urls.url_disabled(flask.request.url):
return

environ = flask.request.environ
flask_request_environ = flask.request.environ
span_name = name_callback()
token = context.attach(
propagators.extract(otel_wsgi.carrier_getter, environ)
propagators.extract(
otel_wsgi.carrier_getter, flask_request_environ
)
)

tracer = trace.get_tracer(__name__, __version__)

span = tracer.start_span(
span_name,
kind=trace.SpanKind.SERVER,
start_time=environ.get(_ENVIRON_STARTTIME_KEY),
start_time=flask_request_environ.get(_ENVIRON_STARTTIME_KEY),
)
if span.is_recording():
attributes = otel_wsgi.collect_request_attributes(environ)
attributes = otel_wsgi.collect_request_attributes(
flask_request_environ
)
if flask.request.url_rule:
# For 404 that result from no route found, etc, we
# don't have a url_rule.
Expand All @@ -162,9 +166,9 @@ def _before_request():

activation = tracer.use_span(span, end_on_exit=True)
activation.__enter__()
environ[_ENVIRON_ACTIVATION_KEY] = activation
environ[_ENVIRON_SPAN_KEY] = span
environ[_ENVIRON_TOKEN] = token
flask_request_environ[_ENVIRON_ACTIVATION_KEY] = activation
flask_request_environ[_ENVIRON_SPAN_KEY] = span
flask_request_environ[_ENVIRON_TOKEN] = token

return _before_request

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@


class InstrumentationTest:
def setUp(self): # pylint: disable=invalid-name
super().setUp() # pylint: disable=no-member

@staticmethod
def _hello_endpoint(helloid):
if helloid == 500:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

from opentelemetry import trace
from opentelemetry.instrumentation.flask import (
FlaskInstrumentor, _get_excluded_urls
FlaskInstrumentor,
_get_excluded_urls,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.test.wsgitestutil import WsgiTestBase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from logging import getLogger
from re import compile as re_compile, search
from os import environ
from re import compile as re_compile
from re import search

from pyramid.events import BeforeTraversal
from pyramid.httpexceptions import HTTPException
Expand Down Expand Up @@ -41,12 +42,12 @@ def _get_excluded_urls():

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

return _ExcludeList(excluded_urls)


_excluded_urls = _get_excluded_urls()


Expand All @@ -69,10 +70,10 @@ def _insert_tween(config):

def _before_traversal(event):
request = event.request
environ = request.environ
span_name = otel_wsgi.get_default_span_name(environ)
request_environ = request.environ
span_name = otel_wsgi.get_default_span_name(request_environ)

enabled = environ.get(_ENVIRON_ENABLED_KEY)
enabled = request_environ.get(_ENVIRON_ENABLED_KEY)
if enabled is None:
_logger.warning(
"Opentelemetry pyramid tween 'opentelemetry.instrumentation.pyramid.trace_tween_factory'"
Expand All @@ -85,34 +86,34 @@ def _before_traversal(event):
# Tracing not enabled, return
return

start_time = environ.get(_ENVIRON_STARTTIME_KEY)
start_time = request_environ.get(_ENVIRON_STARTTIME_KEY)

token = context.attach(
propagators.extract(otel_wsgi.carrier_getter, environ)
propagators.extract(otel_wsgi.carrier_getter, request_environ)
)
tracer = trace.get_tracer(__name__, __version__)

if request.matched_route:
span_name = request.matched_route.pattern
else:
span_name = otel_wsgi.get_default_span_name(environ)
span_name = otel_wsgi.get_default_span_name(request_environ)

span = tracer.start_span(
span_name, kind=trace.SpanKind.SERVER, start_time=start_time,
)

if span.is_recording():
attributes = otel_wsgi.collect_request_attributes(environ)
attributes = otel_wsgi.collect_request_attributes(request_environ)
if request.matched_route:
attributes["http.route"] = request.matched_route.pattern
for key, value in attributes.items():
span.set_attribute(key, value)

activation = tracer.use_span(span, end_on_exit=True)
activation.__enter__()
environ[_ENVIRON_ACTIVATION_KEY] = activation
environ[_ENVIRON_SPAN_KEY] = span
environ[_ENVIRON_TOKEN] = token
request_environ[_ENVIRON_ACTIVATION_KEY] = activation
request_environ[_ENVIRON_SPAN_KEY] = span
request_environ[_ENVIRON_TOKEN] = token


def trace_tween_factory(handler, registry):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@


class InstrumentationTest:
def setUp(self): # pylint: disable=invalid-name
super().setUp() # pylint: disable=no-member

@staticmethod
def _hello_endpoint(request):
helloid = int(request.matchdict["helloid"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

from starlette import applications
from starlette.routing import Match
Expand All @@ -40,8 +41,7 @@ def _get_excluded_urls():

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

return _ExcludeList(excluded_urls)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def get(self):
from functools import partial
from logging import getLogger
from os import environ
from re import compile as re_compile, search
from re import compile as re_compile
from re import search

import tornado.web
import wrapt
Expand Down Expand Up @@ -99,8 +100,7 @@ def _get_excluded_urls():

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

return _ExcludeList(excluded_urls)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
from opentelemetry import trace
from opentelemetry.instrumentation.tornado import (
TornadoInstrumentor,
_get_excluded_urls,
_get_traced_request_attrs,
patch_handler_class,
unpatch_handler_class,
_get_traced_request_attrs,
_get_excluded_urls,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import SpanKind
Expand Down

0 comments on commit c4a36ad

Please sign in to comment.