-
Notifications
You must be signed in to change notification settings - Fork 636
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
added gunicorn production server documentation #1272
added gunicorn production server documentation #1272
Conversation
|
.. code-block:: python | ||
|
||
def post_fork(server, worker): | ||
worker(DjangoInstrumentor().instrument()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why is result of instrument() being passed on to worker? Is it even expected or required for a worker to be called like this in post_fork? AFAIK, you can totally ignore doing anything with the server and worker object, and just perform general initialization but may be I'm wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right actually. I was thinking about that too but I thought instrumentation should pass through a worker. I see now and agree that passing it into post_fork is quite sufficient
.. code-block:: python | ||
|
||
def post_fork(server, worker): | ||
worker(DjangoInstrumentor().instrument()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- As far as I remember, tracing as a whole needs to be setup in post_fork including creating the tracer provider, span processors, instrumentation etc. Especially if batch span processor is used. Any reason to only mention instrumentation here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you suggesting that I create a pipeline in post_fork? I was thinking more of using opentelemetry as an agent. I could be wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that you recommended a pipeline. Let me put that in effect.
|
||
.. code-block:: python | ||
|
||
def post_fork(server, worker): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have seen some problems regarding the forking process of Gunicorn when working with Opencensus like here and here. The main issue was that the worker thread that was responsible for sending spans to the exporter (BatchSpanProcessor in this case) was not copied to the child processes due to Gunicorn using os.fork()
to spawn them. Does this post_fork()
configuration fix this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, this is not enough. The entire trace pipeline (particularly batch processor) needs to be setup in post_fork for it to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The django example docs (and example code) should be updated too I believe. https://opentelemetry-python.readthedocs.io/en/latest/examples/django/README.html
@owais is there an elegant way to handle test vs. production to avoid instrumenting the application twice?
@aabmass one could initialize tracing in I found that initializing tracing in manage.py and in gunicorn.config.py both works well for both dev and production. Adding a tracing.py file to your django project that looks something like this: from logging import getLogger
from pkg_resources import iter_entry_points
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
BatchExportSpanProcessor,
SimpleExportSpanProcessor,
)
logger = getLogger(__file__)
def init_tracing():
provider = TracerProvider()
trace.set_tracer_provider(provider)
provider.add_span_processor(BatchExportSpanProcessor(ConsoleSpanExporter()))
auto_instrument()
# function should be provided out of box as `opentelemetry.instrumentation.auto_instrumentation.auto_instrument`
def auto_instrument():
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
try:
entry_point.load()().instrument()
except Exception:
logger.exception("Instrumenting of %s failed", entry_point.name) and then import and call manage.py #!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
from djtest.tracing import init_tracing # change 1
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djtest.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
init_tracing() # change 2
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main() gunicorn.config.py from djtest.tracing import init_tracing
def post_fork(server, worker):
init_tracing() I've seen some projects start gunicorn using manage.py like |
Please check out my changes in #1286 |
Co-authored-by: Mayur Kale <[email protected]>
Closing this in favour of #1286 |
Description
Achieving OpenTelemetry instrumentation in a Django production servers like gunicorn, since instrumentation of the application on works on a development server. You have to install gunicorn in your application to effect this change.
Fixes # (1197)
Documentation
Please delete options that are not relevant.
How Has This Been Tested?
I have applied tox lint checks