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 f945a7b commit 64b7e6c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/instana/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ def key_to_bool(k: str) -> bool:

def get_aws_lambda_handler() -> Tuple[str, str]:
"""
For instrumenting AWS Lambda, users specify their original lambda handler
in the LAMBDA_HANDLER environment variable. This function searches for and
For instrumenting AWS Lambda, users specify their original lambda handler
in the LAMBDA_HANDLER environment variable. This function searches for and
parses that environment variable or returns the defaults.
The default handler value for AWS Lambda is 'lambda_function.lambda_handler'
which equates to the function "lambda_handler in a file named
lambda_function.py" or in Python terms
which equates to the function "lambda_handler in a file named
lambda_function.py" or in Python terms
"from lambda_function import lambda_handler"
"""
handler_module = "lambda_function"
Expand Down Expand Up @@ -206,6 +206,7 @@ def boot_agent() -> None:

# Hooks
# from instana.hooks import hook_uwsgi # noqa: F401
from instana.hooks import 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 64b7e6c

Please sign in to comment.