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

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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

Expand Down
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,
)