Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop/conditional server span pyramid #869

6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.8.0-0.27b0...HEAD)
- `opentelemetry-instrumentation-pyramid` Pyramid: Conditionally create SERVER spans
([#869](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/869))

### Added

- `opentelemetry-instrumentation-asgi` now returns a `traceresponse` response header.
([#817](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/817))
- `opentelemetry-instrumentation-kafka-python` added kafka-python module instrumentation.
([#814](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/814))

- `opentelemetry-instrumentation-falcon` Falcon: Conditionally create SERVER spans
([#867](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/867))
### Fixed

- `opentelemetry-instrumentation-django` Django: Conditionally create SERVER spans
Expand All @@ -25,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-asgi` ASGI: Conditionally create SERVER spans
([#843](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/843))

- `opentelemetry-instrumentation-django` Django: fix issue preventing detection of MIDDLEWARE_CLASSES

## [1.8.0-0.27b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.8.0-0.27b0) - 2021-12-17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _get_django_middleware_setting() -> str:
# In Django versions 1.x, setting MIDDLEWARE_CLASSES can be used as a legacy
# alternative to MIDDLEWARE. This is the case when `settings.MIDDLEWARE` has
# its default value (`None`).
if not DJANGO_2_0 and getattr(settings, "MIDDLEWARE", []) is None:
if not DJANGO_2_0 and getattr(settings, "MIDDLEWARE", None) is None:
return "MIDDLEWARE_CLASSES"
return "MIDDLEWARE"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def response_hook(span, req, resp):
get_global_response_propagator,
)
from opentelemetry.instrumentation.utils import (
_start_internal_or_server_span,
extract_attributes_from_object,
http_status_to_status_code,
)
from opentelemetry.propagate import extract
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace.status import Status
from opentelemetry.util._time import _time_ns
Expand Down Expand Up @@ -195,12 +195,14 @@ def __call__(self, env, start_response):

start_time = _time_ns()

token = context.attach(extract(env, getter=otel_wsgi.wsgi_getter))
span = self._tracer.start_span(
otel_wsgi.get_default_span_name(env),
kind=trace.SpanKind.SERVER,
span, token = _start_internal_or_server_span(
tracer=self._tracer,
span_name=otel_wsgi.get_default_span_name(env),
start_time=start_time,
context_carrier=env,
context_getter=otel_wsgi.wsgi_getter,
)

if span.is_recording():
attributes = otel_wsgi.collect_request_attributes(env)
for key, value in attributes.items():
Expand All @@ -216,7 +218,8 @@ def _start_response(status, response_headers, *args, **kwargs):
status, response_headers, *args, **kwargs
)
activation.__exit__(None, None, None)
context.detach(token)
if token is not None:
context.detach(token)
return response

try:
Expand All @@ -227,7 +230,8 @@ def _start_response(status, response_headers, *args, **kwargs):
exc,
getattr(exc, "__traceback__", None),
)
context.detach(token)
if token is not None:
context.detach(token)
raise


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from falcon import testing

from opentelemetry import trace
from opentelemetry.instrumentation.falcon import FalconInstrumentor
from opentelemetry.instrumentation.propagators import (
TraceResponsePropagator,
Expand Down Expand Up @@ -264,3 +265,18 @@ def test_hooks(self):
self.assertEqual(
span.attributes["request_hook_attr"], "value from hook"
)


class TestFalconInstrumentationWrappedWithOtherFramework(TestFalconBase):
owais marked this conversation as resolved.
Show resolved Hide resolved
def test_mark_span_internal_in_presence_of_span_from_other_framework(self):
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span(
"test", kind=trace.SpanKind.SERVER
) as parent_span:
self.client().simulate_request(method="GET", path="/hello")
span = self.memory_exporter.get_finished_spans()[0]
assert span.status.is_ok
self.assertEqual(trace.SpanKind.INTERNAL, span.kind)
self.assertEqual(
span.parent.span_id, parent_span.get_span_context().span_id
)
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,23 @@ def _before_traversal(event):

start_time = request_environ.get(_ENVIRON_STARTTIME_KEY)

token = context.attach(
extract(request_environ, getter=otel_wsgi.wsgi_getter)
)
token = ctx = None
span_kind = trace.SpanKind.INTERNAL
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(request_environ)

if trace.get_current_span() is trace.INVALID_SPAN:
ctx = extract(request_environ, getter=otel_wsgi.wsgi_getter)
token = context.attach(ctx)
span_kind = trace.SpanKind.SERVER
span = tracer.start_span(
span_name,
kind=trace.SpanKind.SERVER,
ctx,
kind=span_kind,
start_time=start_time,
)

Expand All @@ -111,7 +115,8 @@ def _before_traversal(event):
activation.__enter__() # pylint: disable=E1101
request_environ[_ENVIRON_ACTIVATION_KEY] = activation
request_environ[_ENVIRON_SPAN_KEY] = span
request_environ[_ENVIRON_TOKEN] = token
if token:
request_environ[_ENVIRON_TOKEN] = token


def trace_tween_factory(handler, registry):
Expand Down Expand Up @@ -180,7 +185,9 @@ def trace_tween(request):
else:
activation.__exit__(None, None, None)

context.detach(request.environ.get(_ENVIRON_TOKEN))
env_token = request.environ.get(_ENVIRON_TOKEN, None)
if env_token is not None:
context.detach(env_token)

return response

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from opentelemetry.instrumentation.pyramid import PyramidInstrumentor
from opentelemetry.test.test_base import TestBase
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.trace import SpanKind

# pylint: disable=import-error
from .pyramid_base_test import InstrumentationTest
Expand Down Expand Up @@ -77,3 +78,34 @@ def test_tween_list(self):
self.assertEqual([b"Hello: 123"], list(resp.response))
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)


class TestWrappedWithOtherFramework(
InstrumentationTest, TestBase, WsgiTestBase
):
def setUp(self):
super().setUp()
PyramidInstrumentor().instrument()
self.config = Configurator()
self._common_initialization(self.config)

def tearDown(self) -> None:
super().tearDown()
with self.disable_logging():
PyramidInstrumentor().uninstrument()

def test_with_existing_span(self):
tracer_provider, _ = self.create_tracer_provider()
tracer = tracer_provider.get_tracer(__name__)

with tracer.start_as_current_span(
"test", kind=SpanKind.SERVER
) as parent_span:
resp = self.client.get("/hello/123")
self.assertEqual(200, resp.status_code)
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(SpanKind.INTERNAL, span_list[0].kind)
self.assertEqual(
parent_span.get_span_context().span_id,
span_list[0].parent.span_id,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

from wrapt import ObjectProxy

from opentelemetry import context, trace

# pylint: disable=unused-import
# pylint: disable=E0611
from opentelemetry.context import _SUPPRESS_INSTRUMENTATION_KEY # noqa: F401
from opentelemetry.propagate import extract
from opentelemetry.trace import StatusCode


Expand Down Expand Up @@ -67,3 +70,39 @@ def unwrap(obj, attr: str):
func = getattr(obj, attr, None)
if func and isinstance(func, ObjectProxy) and hasattr(func, "__wrapped__"):
setattr(obj, attr, func.__wrapped__)


def _start_internal_or_server_span(
tracer, span_name, start_time, context_carrier, context_getter
):
"""Returns internal or server span along with the token which can be used by caller to reset context


Args:
tracer : tracer in use by given instrumentation library
name (string): name of the span
start_time : start time of the span
context_carrier : object which contains values that are
used to construct a Context. This object
must be paired with an appropriate getter
which understands how to extract a value from it.
context_getter : an object which contains a get function that can retrieve zero
or more values from the carrier and a keys function that can get all the keys
from carrier.
"""

token = ctx = span_kind = None
if trace.get_current_span() is trace.INVALID_SPAN:
ctx = extract(context_carrier, getter=context_getter)
token = context.attach(ctx)
span_kind = trace.SpanKind.SERVER
else:
ctx = context.get_current()
span_kind = trace.SpanKind.INTERNAL
span = tracer.start_span(
name=span_name,
context=ctx,
kind=span_kind,
start_time=start_time,
)
return span, token