Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OTel] gunicorn: Gunicorn Hook Support #650

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading