Merge pull request #122 from NathanielRN/move-instrumentation-wsgi

Move instrumentation wsgi
This commit is contained in:
alrex
2020-11-02 13:24:10 -08:00
committed by GitHub
4 changed files with 35 additions and 26 deletions

View File

@ -39,12 +39,12 @@ package_dir=
=src
packages=find_namespace:
install_requires =
opentelemetry-api == 0.15.dev0
opentelemetry-instrumentation == 0.15.dev0
opentelemetry-api == 0.15b0
opentelemetry-instrumentation == 0.15b0
[options.extras_require]
test =
opentelemetry-test == 0.15.dev0
opentelemetry-test == 0.15b0
[options.packages.find]
where = src

View File

@ -59,26 +59,37 @@ import typing
import wsgiref.util as wsgiref_util
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.trace.status import Status, StatusCanonicalCode
from opentelemetry.trace.propagation.textmap import DictGetter
from opentelemetry.trace.status import Status, StatusCode
_HTTP_VERSION_PREFIX = "HTTP/"
def get_header_from_environ(
environ: dict, header_name: str
) -> typing.List[str]:
"""Retrieve a HTTP header value from the PEP3333-conforming WSGI environ.
class CarrierGetter(DictGetter):
def get(self, carrier: dict, key: str) -> typing.List[str]:
"""Getter implementation to retrieve a HTTP header value from the
PEP3333-conforming WSGI environ
Returns:
A list with a single string with the header value if it exists, else an empty list.
"""
environ_key = "HTTP_" + header_name.upper().replace("-", "_")
value = environ.get(environ_key)
if value is not None:
return [value]
return []
Args:
carrier: WSGI environ object
key: header name in environ object
Returns:
A list with a single string with the header value if it exists,
else an empty list.
"""
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):
@ -146,13 +157,13 @@ def add_response_attributes(
except ValueError:
span.set_status(
Status(
StatusCanonicalCode.UNKNOWN,
StatusCode.ERROR,
"Non-integer HTTP status: " + repr(status_code),
)
)
else:
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):
@ -195,9 +206,7 @@ class OpenTelemetryMiddleware:
start_response: The WSGI start_response callable.
"""
token = context.attach(
propagators.extract(get_header_from_environ, environ)
)
token = context.attach(propagators.extract(carrier_getter, environ))
span_name = self.name_callback(environ)
span = self.tracer.start_span(
@ -217,7 +226,7 @@ class OpenTelemetryMiddleware:
)
except Exception as ex:
if span.is_recording():
span.set_status(Status(StatusCanonicalCode.INTERNAL, str(ex)))
span.set_status(Status(StatusCode.ERROR, str(ex)))
span.end()
context.detach(token)
raise

View File

@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
__version__ = "0.15.dev0"
__version__ = "0.15b0"

View File

@ -21,7 +21,7 @@ from urllib.parse import urlsplit
import opentelemetry.instrumentation.wsgi as otel_wsgi
from opentelemetry import trace as trace_api
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.trace.status import StatusCanonicalCode
from opentelemetry.trace.status import StatusCode
class Response:
@ -177,7 +177,7 @@ class TestWsgiApplication(WsgiTestBase):
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
self.assertEqual(
span_list[0].status.canonical_code, StatusCanonicalCode.INTERNAL,
span_list[0].status.status_code, StatusCode.ERROR,
)
def test_override_span_name(self):