Skip to content

Commit

Permalink
pyramid: enable auto-instrumentation
Browse files Browse the repository at this point in the history
Signed-off-by: Varsha GS <varsha.gs@ibm.com>
  • Loading branch information
Varsha GS committed Sep 17, 2024
1 parent 126e4ec commit 345aeda
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/instana/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def boot_agent():
# client, # noqa: F401
# server, # noqa: F401
# )
from instana.instrumentation import pyramid_inst

# Hooks
# from instana.hooks import hook_uwsgi # noqa: F401
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@


from pyramid.httpexceptions import HTTPException
from pyramid.path import caller_package
from pyramid.settings import aslist
from pyramid.tweens import EXCVIEW
from typing import TYPE_CHECKING, Dict, Any, Callable
import wrapt

from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import SpanKind
Expand Down Expand Up @@ -104,6 +108,34 @@ def __call__(self, request: "Request") -> "Response":
return response


INSTANA_TWEEN = __name__ + ".InstanaTweenFactory"


# implicit tween ordering
def includeme(config: "Configurator") -> None:
logger.debug("Instrumenting pyramid")
config.add_tween(__name__ + ".InstanaTweenFactory")
config.add_tween(INSTANA_TWEEN)


# explicit tween ordering
@wrapt.patch_function_wrapper("pyramid.config", "Configurator.__init__")
def init_with_instana(wrapped, instance, args, kwargs):
settings = kwargs.get("settings", {})
tweens = aslist(settings.get("pyramid.tweens", []))

if tweens and INSTANA_TWEEN not in settings:
# pyramid.tweens.EXCVIEW is the name of built-in exception view provided by
# pyramid. We need our tween to be before it, otherwise unhandled
# exceptions will be caught before they reach our tween.
if EXCVIEW in tweens:
tweens = [INSTANA_TWEEN] + tweens
else:
tweens = [INSTANA_TWEEN] + tweens + [EXCVIEW]
settings["pyramid.tweens"] = "\n".join(tweens)
kwargs["settings"] = settings

if not kwargs.get("package", None):
kwargs["package"] = caller_package()

wrapped(*args, **kwargs)
instance.include(__name__)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# (c) Copyright Instana Inc. 2020

import os
from tests.apps.pyramid_app.app import pyramid_server as server
from tests.apps.pyramid.pyramid_app.app import pyramid_server as server
from tests.apps.utils import launch_background_thread

app_thread = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ def hello_user(request):


app = None
with Configurator() as config:
config.include("instana.instrumentation.pyramid.tweens")
settings = {
"pyramid.tweens": "tests.apps.pyramid.pyramid_utils.tweens.timing_tween_factory",
}
with Configurator(settings=settings) as config:
config.add_route("hello", "/")
config.add_view(hello_world, route_name="hello")
config.add_route("fail", "/500")
Expand Down
16 changes: 16 additions & 0 deletions tests/apps/pyramid/pyramid_utils/tweens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# (c) Copyright IBM Corp. 2024

import time


def timing_tween_factory(handler, registry):
def timing_tween(request):
start = time.time()
try:
response = handler(request)
finally:
end = time.time()
print(f"The request took {end - start} seconds")
return response

return timing_tween
2 changes: 1 addition & 1 deletion tests/frameworks/test_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import urllib3
from typing import Generator

import tests.apps.pyramid_app
import tests.apps.pyramid.pyramid_app
from tests.helpers import testenv
from instana.singletons import tracer, agent
from instana.span.span import get_current_span
Expand Down

0 comments on commit 345aeda

Please sign in to comment.