mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-01 09:13:23 +08:00
Merge pull request #122 from NathanielRN/move-instrumentation-wsgi
Move instrumentation wsgi
This commit is contained in:
@ -39,12 +39,12 @@ package_dir=
|
|||||||
=src
|
=src
|
||||||
packages=find_namespace:
|
packages=find_namespace:
|
||||||
install_requires =
|
install_requires =
|
||||||
opentelemetry-api == 0.15.dev0
|
opentelemetry-api == 0.15b0
|
||||||
opentelemetry-instrumentation == 0.15.dev0
|
opentelemetry-instrumentation == 0.15b0
|
||||||
|
|
||||||
[options.extras_require]
|
[options.extras_require]
|
||||||
test =
|
test =
|
||||||
opentelemetry-test == 0.15.dev0
|
opentelemetry-test == 0.15b0
|
||||||
|
|
||||||
[options.packages.find]
|
[options.packages.find]
|
||||||
where = src
|
where = src
|
||||||
|
@ -59,26 +59,37 @@ import typing
|
|||||||
import wsgiref.util as wsgiref_util
|
import wsgiref.util as wsgiref_util
|
||||||
|
|
||||||
from opentelemetry import context, propagators, trace
|
from opentelemetry import context, propagators, trace
|
||||||
from opentelemetry.instrumentation.utils import http_status_to_canonical_code
|
from opentelemetry.instrumentation.utils import http_status_to_status_code
|
||||||
from opentelemetry.instrumentation.wsgi.version import __version__
|
from opentelemetry.instrumentation.wsgi.version import __version__
|
||||||
from opentelemetry.trace.status import Status, StatusCanonicalCode
|
from opentelemetry.trace.propagation.textmap import DictGetter
|
||||||
|
from opentelemetry.trace.status import Status, StatusCode
|
||||||
|
|
||||||
_HTTP_VERSION_PREFIX = "HTTP/"
|
_HTTP_VERSION_PREFIX = "HTTP/"
|
||||||
|
|
||||||
|
|
||||||
def get_header_from_environ(
|
class CarrierGetter(DictGetter):
|
||||||
environ: dict, header_name: str
|
def get(self, carrier: dict, key: str) -> typing.List[str]:
|
||||||
) -> typing.List[str]:
|
"""Getter implementation to retrieve a HTTP header value from the
|
||||||
"""Retrieve a HTTP header value from the PEP3333-conforming WSGI environ.
|
PEP3333-conforming WSGI environ
|
||||||
|
|
||||||
Returns:
|
Args:
|
||||||
A list with a single string with the header value if it exists, else an empty list.
|
carrier: WSGI environ object
|
||||||
"""
|
key: header name in environ object
|
||||||
environ_key = "HTTP_" + header_name.upper().replace("-", "_")
|
Returns:
|
||||||
value = environ.get(environ_key)
|
A list with a single string with the header value if it exists,
|
||||||
if value is not None:
|
else an empty list.
|
||||||
return [value]
|
"""
|
||||||
return []
|
environ_key = "HTTP_" + key.upper().replace("-", "_")
|
||||||
|
value = carrier.get(environ_key)
|
||||||
|
if value is not None:
|
||||||
|
return [value]
|
||||||
|
return []
|
||||||
|
|
||||||
|
def keys(self, carrier):
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
carrier_getter = CarrierGetter()
|
||||||
|
|
||||||
|
|
||||||
def setifnotnone(dic, key, value):
|
def setifnotnone(dic, key, value):
|
||||||
@ -146,13 +157,13 @@ def add_response_attributes(
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
span.set_status(
|
span.set_status(
|
||||||
Status(
|
Status(
|
||||||
StatusCanonicalCode.UNKNOWN,
|
StatusCode.ERROR,
|
||||||
"Non-integer HTTP status: " + repr(status_code),
|
"Non-integer HTTP status: " + repr(status_code),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
span.set_attribute("http.status_code", status_code)
|
span.set_attribute("http.status_code", status_code)
|
||||||
span.set_status(Status(http_status_to_canonical_code(status_code)))
|
span.set_status(Status(http_status_to_status_code(status_code)))
|
||||||
|
|
||||||
|
|
||||||
def get_default_span_name(environ):
|
def get_default_span_name(environ):
|
||||||
@ -195,9 +206,7 @@ class OpenTelemetryMiddleware:
|
|||||||
start_response: The WSGI start_response callable.
|
start_response: The WSGI start_response callable.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
token = context.attach(
|
token = context.attach(propagators.extract(carrier_getter, environ))
|
||||||
propagators.extract(get_header_from_environ, environ)
|
|
||||||
)
|
|
||||||
span_name = self.name_callback(environ)
|
span_name = self.name_callback(environ)
|
||||||
|
|
||||||
span = self.tracer.start_span(
|
span = self.tracer.start_span(
|
||||||
@ -217,7 +226,7 @@ class OpenTelemetryMiddleware:
|
|||||||
)
|
)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
if span.is_recording():
|
if span.is_recording():
|
||||||
span.set_status(Status(StatusCanonicalCode.INTERNAL, str(ex)))
|
span.set_status(Status(StatusCode.ERROR, str(ex)))
|
||||||
span.end()
|
span.end()
|
||||||
context.detach(token)
|
context.detach(token)
|
||||||
raise
|
raise
|
||||||
|
@ -12,4 +12,4 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
__version__ = "0.15.dev0"
|
__version__ = "0.15b0"
|
||||||
|
@ -21,7 +21,7 @@ from urllib.parse import urlsplit
|
|||||||
import opentelemetry.instrumentation.wsgi as otel_wsgi
|
import opentelemetry.instrumentation.wsgi as otel_wsgi
|
||||||
from opentelemetry import trace as trace_api
|
from opentelemetry import trace as trace_api
|
||||||
from opentelemetry.test.wsgitestutil import WsgiTestBase
|
from opentelemetry.test.wsgitestutil import WsgiTestBase
|
||||||
from opentelemetry.trace.status import StatusCanonicalCode
|
from opentelemetry.trace.status import StatusCode
|
||||||
|
|
||||||
|
|
||||||
class Response:
|
class Response:
|
||||||
@ -177,7 +177,7 @@ class TestWsgiApplication(WsgiTestBase):
|
|||||||
span_list = self.memory_exporter.get_finished_spans()
|
span_list = self.memory_exporter.get_finished_spans()
|
||||||
self.assertEqual(len(span_list), 1)
|
self.assertEqual(len(span_list), 1)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
span_list[0].status.canonical_code, StatusCanonicalCode.INTERNAL,
|
span_list[0].status.status_code, StatusCode.ERROR,
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_override_span_name(self):
|
def test_override_span_name(self):
|
||||||
|
Reference in New Issue
Block a user