From 1f7d1173425462ca869831728fcdd7cfe4a8cdfd Mon Sep 17 00:00:00 2001 From: Cagri Yonca Date: Tue, 15 Oct 2024 05:30:56 -0700 Subject: [PATCH] refactor(hook_gunicorn): added gunicorn support to otel Signed-off-by: Cagri Yonca --- src/instana/__init__.py | 2 +- src/instana/hooks/hook_gunicorn.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/instana/hooks/hook_gunicorn.py diff --git a/src/instana/__init__.py b/src/instana/__init__.py index 2d8bfb48..bb4d1d80 100644 --- a/src/instana/__init__.py +++ b/src/instana/__init__.py @@ -205,7 +205,7 @@ def boot_agent() -> None: ) # Hooks - from instana.hooks import hook_uwsgi # noqa: F401 + from instana.hooks import hook_uwsgi, hook_gunicorn # noqa: F401 if "INSTANA_DISABLE" not in os.environ: diff --git a/src/instana/hooks/hook_gunicorn.py b/src/instana/hooks/hook_gunicorn.py new file mode 100644 index 00000000..e3fb8086 --- /dev/null +++ b/src/instana/hooks/hook_gunicorn.py @@ -0,0 +1,25 @@ +# (c) Copyright IBM Corp. 2021 +# (c) Copyright Instana Inc. 2019 + +try: + from instana.log import logger + from instana.singletons import agent + + import gunicorn + from gunicorn.arbiter import Arbiter + from gunicorn.config import Config + from gunicorn.workers.sync import SyncWorker + + def pre_fork(config: Config, server: Arbiter, worker: SyncWorker) -> None: + """This is our gunicorn hook to detect and act when worker processes are forked off.""" + logger.debug("Handling gunicorn fork...") + agent.handle_fork() + + Config.pre_fork = pre_fork + + logger.debug("Gunicorn pre-fork hook applied") +except ImportError: + logger.debug( + "gunicorn hooks: decorators not available: likely not running under gunicorn" + ) + pass