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

Conditionally create server spans for flask #828

Merged
merged 7 commits into from
Dec 20, 2021

Conversation

ashu658
Copy link
Member

@ashu658 ashu658 commented Dec 8, 2021

Description

Adds support to make spans as INTERNAL if a span is already present in current context in flask.

Fixes #446

Type of change

Please delete options that are not relevant.

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
    Currently flask only makes SERVER spans. With this PR it can make INTERNAL spans in presence of a span in current context.

How Has This Been Tested?

Added unit test for the changes.
Manually tested the scenario.
To reproduce the issue: Add two instrumentors i.e. FlaskInstrumentor and OpenTelemetryMiddleware in the same app. This creates two separate traces instead of one.

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

@ashu658 ashu658 requested a review from a team December 8, 2021 06:57
if trace.get_current_span() is trace.INVALID_SPAN:
ctx = extract(flask_request_environ, getter=otel_wsgi.wsgi_getter)
token = context.attach(ctx)
span_kind = trace.SpanKind.SERVER

Copy link
Contributor

@lzchen lzchen Dec 9, 2021

Choose a reason for hiding this comment

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

If span is found in current context, I believe you'll have to set the current context as the parent. Currently, ctx would be None.

Copy link
Member Author

@ashu658 ashu658 Dec 9, 2021

Choose a reason for hiding this comment

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

Done.
I think it was passing earlier as tracer.start_span() makes current span as parent if context passed is none.
@lzchen Should we set parent explicitly for readability or let start_span() handle it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yes you are correct. Either way is fine with me.

@ashu658 ashu658 requested a review from lzchen December 9, 2021 19:26
@ashu658 ashu658 requested a review from lzchen December 10, 2021 12:52
@@ -59,7 +59,7 @@
if DJANGO_2_0:
from django.urls import re_path
else:
from django.conf.urls import url as re_path
from django.conf.urls import url as re_path # pylint: disable=E0611
Copy link
Member Author

@ashu658 ashu658 Dec 10, 2021

Choose a reason for hiding this comment

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

Added this as django.conf.urls.url() is removed in django 4.0. leading to lint check failure (release notes).
cc @lzchen

@owais
Copy link
Contributor

owais commented Dec 20, 2021

The title says flask but contains code for Django.

@ashu658 ashu658 closed this Dec 20, 2021
@ashu658 ashu658 reopened this Dec 20, 2021
@ashu658
Copy link
Member Author

ashu658 commented Dec 20, 2021

The title says flask but contains code for Django.

Added them to pass lint checks. Just saw those changes have already been merged to master. Removed the django related code.

@owais owais enabled auto-merge (squash) December 20, 2021 11:39
@owais owais merged commit 30d0c2e into open-telemetry:main Dec 20, 2021
ItayGibel-helios pushed a commit to helios/opentelemetry-python-contrib that referenced this pull request Dec 26, 2021
* Making span as internal in presence of a span in current context

* Updating changelog

* Removing extra print statements

* Resolving comments: Setting current context as parent in its presence

* Ignoring pylint check as django.conf.urls.url is removed in django 4.0
Django release notes: https://docs.djangoproject.com/en/4.0/releases/4.0/

* Removing changes in django files
ItayGibel-helios pushed a commit to helios/opentelemetry-python-contrib that referenced this pull request Dec 28, 2021
* Making span as internal in presence of a span in current context

* Updating changelog

* Removing extra print statements

* Resolving comments: Setting current context as parent in its presence

* Ignoring pylint check as django.conf.urls.url is removed in django 4.0
Django release notes: https://docs.djangoproject.com/en/4.0/releases/4.0/

* Removing changes in django files
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.

Flask: Conditionally create SERVER spans
3 participants