Skip to content

Commit 0187389

Browse files
committed
refactor tween list check, tween exception handling
1 parent d106094 commit 0187389

2 files changed

Lines changed: 67 additions & 68 deletions

File tree

ext/opentelemetry-ext-pyramid/src/opentelemetry/ext/pyramid/__init__.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
from pyramid.config import Configurator
7474
from pyramid.path import caller_package
75-
from pyramid.tweens import EXCVIEW
75+
from pyramid.settings import aslist
7676
from wrapt import ObjectProxy
7777
from wrapt import wrap_function_wrapper as _wrap
7878

@@ -88,18 +88,15 @@
8888

8989
def traced_init(wrapped, instance, args, kwargs):
9090
settings = kwargs.get("settings", {})
91-
tweens = settings.get("pyramid.tweens")
91+
tweens = aslist(settings.get("pyramid.tweens", []))
9292

93-
if tweens and tweens.strip() and TWEEN_NAME not in settings:
93+
if tweens and TWEEN_NAME not in settings:
9494
# pyramid.tweens.EXCVIEW is the name of built-in exception view provided by
9595
# pyramid. We need our tween to be before it, otherwise unhandled
9696
# exceptions will be caught before they reach our tween.
97-
idx = tweens.find(EXCVIEW)
98-
if idx == -1:
99-
tween_list = tweens + "\n" + TWEEN_NAME
100-
else:
101-
tween_list = tweens[:idx] + TWEEN_NAME + "\n" + tweens[idx:]
102-
settings["pyramid.tweens"] = tween_list
97+
tweens = [TWEEN_NAME] + tweens
98+
99+
settings["pyramid.tweens"] = "\n".join(tweens)
103100

104101
kwargs["settings"] = settings
105102

ext/opentelemetry-ext-pyramid/src/opentelemetry/ext/pyramid/callbacks.py

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -108,63 +108,65 @@ def trace_tween_factory(handler, registry):
108108
settings = registry.settings
109109
enabled = asbool(settings.get(SETTING_TRACE_ENABLED, True))
110110

111-
if enabled:
112-
# make a request tracing function
113-
def trace_tween(request):
114-
if disable_trace(request.url, _excluded_hosts, _excluded_paths):
115-
request.environ[_ENVIRON_ENABLED_KEY] = False
116-
# short-circuit when we don't want to trace anything
117-
return handler(request)
118-
119-
request.environ[_ENVIRON_ENABLED_KEY] = True
120-
request.environ[_ENVIRON_STARTTIME_KEY] = time_ns()
121-
122-
response = None
123-
try:
124-
response = handler(request)
125-
except HTTPException as exc:
126-
# If the exception is a pyramid HTTPException,
127-
# that's still valuable information that isn't necessarily
128-
# a 500. For instance, HTTPFound is a 302.
129-
# As described in docs, Pyramid exceptions are all valid
130-
# response types
131-
response = exc
132-
raise
133-
finally:
134-
span = request.environ.get(_ENVIRON_SPAN_KEY)
135-
enabled = request.environ.get(_ENVIRON_ENABLED_KEY)
136-
if not span and enabled:
137-
_logger.warning(
138-
"Pyramid environ's OpenTelemetry span missing."
139-
"If the OpenTelemetry tween was added manually, make sure"
140-
"PyramidInstrumentor().instrument_config(config) is called"
111+
if not enabled:
112+
# If disabled, make a tween that signals to the
113+
# BeforeTraversal subscriber that tracing is disabled
114+
def disabled_tween(request):
115+
request.environ[_ENVIRON_ENABLED_KEY] = False
116+
return handler(request)
117+
118+
return disabled_tween
119+
120+
# make a request tracing function
121+
def trace_tween(request):
122+
if disable_trace(request.url, _excluded_hosts, _excluded_paths):
123+
request.environ[_ENVIRON_ENABLED_KEY] = False
124+
# short-circuit when we don't want to trace anything
125+
return handler(request)
126+
127+
request.environ[_ENVIRON_ENABLED_KEY] = True
128+
request.environ[_ENVIRON_STARTTIME_KEY] = time_ns()
129+
130+
try:
131+
response = handler(request)
132+
response_or_exception = response
133+
except HTTPException as exc:
134+
# If the exception is a pyramid HTTPException,
135+
# that's still valuable information that isn't necessarily
136+
# a 500. For instance, HTTPFound is a 302.
137+
# As described in docs, Pyramid exceptions are all valid
138+
# response types
139+
response_or_exception = exc
140+
raise
141+
finally:
142+
span = request.environ.get(_ENVIRON_SPAN_KEY)
143+
enabled = request.environ.get(_ENVIRON_ENABLED_KEY)
144+
if not span and enabled:
145+
_logger.warning(
146+
"Pyramid environ's OpenTelemetry span missing."
147+
"If the OpenTelemetry tween was added manually, make sure"
148+
"PyramidInstrumentor().instrument_config(config) is called"
149+
)
150+
elif enabled:
151+
otel_wsgi.add_response_attributes(
152+
span,
153+
response_or_exception.status,
154+
response_or_exception.headers,
155+
)
156+
157+
activation = request.environ.get(_ENVIRON_ACTIVATION_KEY)
158+
159+
if isinstance(response_or_exception, HTTPException):
160+
activation.__exit__(
161+
type(response_or_exception),
162+
response_or_exception,
163+
getattr(response_or_exception, "__traceback__", None),
141164
)
142-
elif enabled:
143-
if response:
144-
otel_wsgi.add_response_attributes(
145-
span, response.status, response.headers
146-
)
147-
148-
activation = request.environ.get(_ENVIRON_ACTIVATION_KEY)
149-
150-
if isinstance(response, HTTPException):
151-
activation.__exit__(
152-
type(response),
153-
response,
154-
getattr(response, "__traceback__", None),
155-
)
156-
else:
157-
activation.__exit__(None, None, None)
158-
159-
context.detach(request.environ.get(_ENVIRON_TOKEN))
160-
return response
161-
162-
return trace_tween
163-
164-
# If not enabled, make a tween that signals to the
165-
# BeforeTraversal subscriber that tracing is disabled
166-
def disabled_tween(request):
167-
request.environ[_ENVIRON_ENABLED_KEY] = False
168-
return handler(request)
169-
170-
return disabled_tween
165+
else:
166+
activation.__exit__(None, None, None)
167+
168+
context.detach(request.environ.get(_ENVIRON_TOKEN))
169+
170+
return response
171+
172+
return trace_tween

0 commit comments

Comments
 (0)