mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-02 02:52:18 +08:00
Remove Configuration from instrumentations (#285)
This commit is contained in:
@ -40,6 +40,7 @@ package_dir=
|
||||
packages=find_namespace:
|
||||
install_requires =
|
||||
django >= 1.10
|
||||
opentelemetry-util-http == 0.18.dev0
|
||||
opentelemetry-instrumentation-wsgi == 0.18.dev0
|
||||
opentelemetry-instrumentation == 0.18.dev0
|
||||
opentelemetry-api == 0.18.dev0
|
||||
|
@ -13,10 +13,13 @@
|
||||
# limitations under the License.
|
||||
|
||||
from logging import getLogger
|
||||
from os import environ
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from opentelemetry.configuration import Configuration
|
||||
from opentelemetry.instrumentation.django.environment_variables import (
|
||||
OTEL_PYTHON_DJANGO_INSTRUMENT,
|
||||
)
|
||||
from opentelemetry.instrumentation.django.middleware import _DjangoMiddleware
|
||||
from opentelemetry.instrumentation.django.version import __version__
|
||||
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
|
||||
@ -43,11 +46,7 @@ class DjangoInstrumentor(BaseInstrumentor, MetricMixin):
|
||||
|
||||
# FIXME this is probably a pattern that will show up in the rest of the
|
||||
# ext. Find a better way of implementing this.
|
||||
# FIXME Probably the evaluation of strings into boolean values can be
|
||||
# built inside the Configuration class itself with the magic method
|
||||
# __bool__
|
||||
|
||||
if Configuration().DJANGO_INSTRUMENT is False:
|
||||
if environ.get(OTEL_PYTHON_DJANGO_INSTRUMENT) == "False":
|
||||
return
|
||||
|
||||
# This can not be solved, but is an inherent problem of this approach:
|
||||
|
@ -0,0 +1,15 @@
|
||||
# Copyright The OpenTelemetry Authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
OTEL_PYTHON_DJANGO_INSTRUMENT = "OTEL_PYTHON_DJANGO_INSTRUMENT"
|
@ -12,12 +12,11 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import time
|
||||
from logging import getLogger
|
||||
from time import time
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from opentelemetry.configuration import Configuration
|
||||
from opentelemetry.context import attach, detach
|
||||
from opentelemetry.instrumentation.django.version import __version__
|
||||
from opentelemetry.instrumentation.utils import extract_attributes_from_object
|
||||
@ -28,6 +27,7 @@ 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
|
||||
|
||||
try:
|
||||
from django.core.urlresolvers import ( # pylint: disable=no-name-in-module
|
||||
@ -61,9 +61,8 @@ class _DjangoMiddleware(MiddlewareMixin):
|
||||
_environ_span_key = "opentelemetry-instrumentor-django.span_key"
|
||||
_environ_exception_key = "opentelemetry-instrumentor-django.exception_key"
|
||||
|
||||
_excluded_urls = Configuration()._excluded_urls("django")
|
||||
|
||||
_traced_request_attrs = Configuration()._traced_request_attrs("django")
|
||||
_traced_request_attrs = get_traced_request_attrs("DJANGO")
|
||||
_excluded_urls = get_excluded_urls("DJANGO")
|
||||
|
||||
@staticmethod
|
||||
def _get_span_name(request):
|
||||
@ -111,23 +110,23 @@ class _DjangoMiddleware(MiddlewareMixin):
|
||||
return
|
||||
|
||||
# pylint:disable=W0212
|
||||
request._otel_start_time = time.time()
|
||||
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
|
||||
@ -215,7 +214,7 @@ class _DjangoMiddleware(MiddlewareMixin):
|
||||
if metric_recorder is not None:
|
||||
# pylint:disable=W0212
|
||||
metric_recorder.record_server_duration_range(
|
||||
request._otel_start_time, time.time(), request._otel_labels
|
||||
request._otel_start_time, time(), request._otel_labels
|
||||
)
|
||||
except Exception as ex: # pylint: disable=W0703
|
||||
_logger.warning("Error recording duration metrics: %s", ex)
|
||||
|
@ -21,13 +21,13 @@ 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.instrumentation.django import DjangoInstrumentor
|
||||
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 (
|
||||
@ -64,7 +64,6 @@ class TestMiddleware(TestBase, WsgiTestBase):
|
||||
super().setUp()
|
||||
setup_test_environment()
|
||||
_django_instrumentor.instrument()
|
||||
Configuration._reset() # pylint: disable=protected-access
|
||||
self.env_patch = patch.dict(
|
||||
"os.environ",
|
||||
{
|
||||
@ -75,11 +74,11 @@ class TestMiddleware(TestBase, WsgiTestBase):
|
||||
self.env_patch.start()
|
||||
self.exclude_patch = patch(
|
||||
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._excluded_urls",
|
||||
Configuration()._excluded_urls("django"),
|
||||
get_excluded_urls("DJANGO"),
|
||||
)
|
||||
self.traced_patch = patch(
|
||||
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
|
||||
Configuration()._traced_request_attrs("django"),
|
||||
get_traced_request_attrs("DJANGO"),
|
||||
)
|
||||
self.exclude_patch.start()
|
||||
self.traced_patch.start()
|
||||
|
Reference in New Issue
Block a user