Skip to content

Commit b32be74

Browse files
sanketmehta28ashu658pezzer55srikanthccvowais
authoredJan 25, 2022
Develop/conditional server span pyramid (#869)
* 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]>
1 parent a2098c3 commit b32be74

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

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

1012
### Added
1113

‎instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,23 @@ def _before_traversal(event):
8282

8383
start_time = request_environ.get(_ENVIRON_STARTTIME_KEY)
8484

85-
token = context.attach(
86-
extract(request_environ, getter=otel_wsgi.wsgi_getter)
87-
)
85+
token = ctx = None
86+
span_kind = trace.SpanKind.INTERNAL
8887
tracer = trace.get_tracer(__name__, __version__)
8988

9089
if request.matched_route:
9190
span_name = request.matched_route.pattern
9291
else:
9392
span_name = otel_wsgi.get_default_span_name(request_environ)
9493

94+
if trace.get_current_span() is trace.INVALID_SPAN:
95+
ctx = extract(request_environ, getter=otel_wsgi.wsgi_getter)
96+
token = context.attach(ctx)
97+
span_kind = trace.SpanKind.SERVER
9598
span = tracer.start_span(
9699
span_name,
97-
kind=trace.SpanKind.SERVER,
100+
ctx,
101+
kind=span_kind,
98102
start_time=start_time,
99103
)
100104

@@ -111,7 +115,8 @@ def _before_traversal(event):
111115
activation.__enter__() # pylint: disable=E1101
112116
request_environ[_ENVIRON_ACTIVATION_KEY] = activation
113117
request_environ[_ENVIRON_SPAN_KEY] = span
114-
request_environ[_ENVIRON_TOKEN] = token
118+
if token:
119+
request_environ[_ENVIRON_TOKEN] = token
115120

116121

117122
def trace_tween_factory(handler, registry):
@@ -180,7 +185,9 @@ def trace_tween(request):
180185
else:
181186
activation.__exit__(None, None, None)
182187

183-
context.detach(request.environ.get(_ENVIRON_TOKEN))
188+
env_token = request.environ.get(_ENVIRON_TOKEN, None)
189+
if env_token is not None:
190+
context.detach(env_token)
184191

185192
return response
186193

‎instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py

+32
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from opentelemetry.instrumentation.pyramid import PyramidInstrumentor
1818
from opentelemetry.test.test_base import TestBase
1919
from opentelemetry.test.wsgitestutil import WsgiTestBase
20+
from opentelemetry.trace import SpanKind
2021

2122
# pylint: disable=import-error
2223
from .pyramid_base_test import InstrumentationTest
@@ -77,3 +78,34 @@ def test_tween_list(self):
7778
self.assertEqual([b"Hello: 123"], list(resp.response))
7879
span_list = self.memory_exporter.get_finished_spans()
7980
self.assertEqual(len(span_list), 1)
81+
82+
83+
class TestWrappedWithOtherFramework(
84+
InstrumentationTest, TestBase, WsgiTestBase
85+
):
86+
def setUp(self):
87+
super().setUp()
88+
PyramidInstrumentor().instrument()
89+
self.config = Configurator()
90+
self._common_initialization(self.config)
91+
92+
def tearDown(self) -> None:
93+
super().tearDown()
94+
with self.disable_logging():
95+
PyramidInstrumentor().uninstrument()
96+
97+
def test_with_existing_span(self):
98+
tracer_provider, _ = self.create_tracer_provider()
99+
tracer = tracer_provider.get_tracer(__name__)
100+
101+
with tracer.start_as_current_span(
102+
"test", kind=SpanKind.SERVER
103+
) as parent_span:
104+
resp = self.client.get("/hello/123")
105+
self.assertEqual(200, resp.status_code)
106+
span_list = self.memory_exporter.get_finished_spans()
107+
self.assertEqual(SpanKind.INTERNAL, span_list[0].kind)
108+
self.assertEqual(
109+
parent_span.get_span_context().span_id,
110+
span_list[0].parent.span_id,
111+
)

0 commit comments

Comments
 (0)
Please sign in to comment.