Skip to content

Commit

Permalink
Develop/conditional server span pyramid (#869)
Browse files Browse the repository at this point in the history
* code change to resolve the bug #449

* modifying the changelog file to add entry for PR #869

* removing redundent get statement

* Conditionally create server spans for falcon (#867)

* Making span as internal for falcon in presence of a span in current context

* Updating changelog

* Fixing lint and generate build failures

* Resolving comments: Converting snippet to re-usable function

* Fixing build failures

* Resolving comments: Creating wrapper for start span to make internal/server span

* Rerun docker tests

* Resolving comments: Refactoring

* Fix Django 1.9 issue preventing use of MIDDLEWARE_CLASSES (#870)

* Update CHANGELOG.md

* Fix Django 1.9 issue preventing use of MIDDLEWARE_CLASSES

Co-authored-by: Srikanth Chekuri <[email protected]>

* changing the import trace statement to resolve issue with unit test cases

Co-authored-by: Ashutosh Goel <[email protected]>
Co-authored-by: Dan <[email protected]>
Co-authored-by: Srikanth Chekuri <[email protected]>
Co-authored-by: Owais Lone <[email protected]>
  • Loading branch information
5 people authored Jan 25, 2022
1 parent a2098c3 commit b32be74
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
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,
)

0 comments on commit b32be74

Please sign in to comment.