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

Recommend installing opentelemetry-instrumentation-asgi under certain situations (opentelemetry-instrumentation-django) #2408

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nenoNaninu
Copy link

@nenoNaninu nenoNaninu commented Apr 10, 2024

Description

When I added the opentelemetry-instrumentation-django package and started the server with python manage.py runserver calling DjangoInstrumentor().instrument() in manage.py, inbound HTTP request spans were correctly created.

However, when using Django with Gunicorn, there was a problem: inbound HTTP request spans were not created.
To solve this problem, I spent a lot of time reading the opentelemetry-instrumentation-django code and found that adding opentelemetry-instrumentation-asgi would solve this problem. I have arrived at the solution.

The current implementation silently turns off instrumentation in certain situations, such as using Gunicorn.

Instrumentation library users will expect the instrumentation to work correctly if the call to DjangoInstrumentor().instrument() succeeds and there are no warnings in the logs. Also, the documentation does not mention the need to add opentelemetry-instrumentation-asgi.

This wastes a lot of time for many developers. Therefore, I have added a log that shows how to deal with this problem instead of silently disabling instrumentation when it occurs.

Related: #2043 #2296

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • No test, because only adding log.

Does This PR Require a Core Repo Change?

  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Copy link

linux-foundation-easycla bot commented Apr 10, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@nenoNaninu nenoNaninu requested a review from a team as a code owner April 10, 2024 19:27
@xrmx
Copy link
Contributor

xrmx commented Apr 11, 2024

Thanks for the PR, care to add a test too?

@nenoNaninu
Copy link
Author

Is it essential to add test code to add a mere line of logging?

@xrmx
Copy link
Contributor

xrmx commented Apr 11, 2024

Is it essential to add test code to add a mere line of logging?

It's not essential of course, but it'll help :)

@nenoNaninu
Copy link
Author

If a test code is not essential, please review as is.

@srikanthccv
Copy link
Member

I don't think we should log such a warning more than once. Please use warnings.warn instead.

@nenoNaninu
Copy link
Author

If developers are using manage.py runserver in a local dev environment, it will probably output plain text logs, so warnings.warn will not be a problem. However, this problem only occurs when Django is started using like gunicorn. And it's not the local development environment that uses gunicorn etc.; it's the stg environment. The stg environment uses structured logs in most cases. So, it is easier to find the problem if the developer has a structured warning log using logger.warning rather than warnings.warn. I don't think it's worth throwing away the structured logs to warn only once.

Moreover, if multiple workers run using gunicorn, the warnings will be logged more than once even if we use warnings.warn.

@xrmx
Copy link
Contributor

xrmx commented Apr 11, 2024

Moreover, if multiple workers run using gunicorn, the warnings will be logged more than once even if we use warnings.warn.

This will warn at each request not for each worker

@nenoNaninu
Copy link
Author

This will warn at each request not for each worker

Of course, I know.

@@ -197,6 +197,9 @@ def process_request(self, request):

is_asgi_request = _is_asgi_request(request)
if not _is_asgi_supported and is_asgi_request:
Copy link
Contributor

@ocelotl ocelotl Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this?

Suggested change
if not _is_asgi_supported and is_asgi_request:
self._unsupported_asgi_warned = False
...
if not _is_asgi_supported and is_asgi_request and not self._unsupported_asgi_warned:
...
self._unsupported_asgi_warned = True

Could be a good idea, or a terrible one 🤷

Copy link
Contributor

@ocelotl ocelotl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment

@nenoNaninu nenoNaninu changed the title Add log to _DjangoMiddleware (opentelemetry-instrumentation-django) Recommend installing opentelemetry-instrumentation-asgi under certain situations (opentelemetry-instrumentation-django) Apr 21, 2024
@nenoNaninu nenoNaninu changed the title Recommend installing opentelemetry-instrumentation-asgi under certain situations (opentelemetry-instrumentation-django) Recommend installing opentelemetry-instrumentation-asgi under certain situations (opentelemetry-instrumentation-django) Apr 21, 2024
@nenoNaninu
Copy link
Author

@srikanthccv @ocelotl I fixed to log a warning only once.

@@ -197,6 +209,7 @@ def process_request(self, request):

is_asgi_request = _is_asgi_request(request)
if not _is_asgi_supported and is_asgi_request:
_OnceLogger.log_warning_asgi_instrumentation()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test case, see this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there already a test for a pattern where the request type is ASGIRequest and opentelemetry-instrumentation-asgi is not installed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants