Skip to content

Commit

Permalink
refactor(hook_gunicorn): added gunicorn support to otel
Browse files Browse the repository at this point in the history
Signed-off-by: Cagri Yonca <[email protected]>
  • Loading branch information
CagriYonca committed Oct 15, 2024
1 parent b1eb5b9 commit 2500054
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/instana/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions src/instana/hooks/hook_gunicorn.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2500054

Please sign in to comment.