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

Requests integration #94

Merged

Conversation

Oberon00
Copy link
Member

@Oberon00 Oberon00 commented Aug 19, 2019

This (draft) PR adds an integration for the requests libraray (see README.md).

For some reason, pylint insists on an __init__.py in the module root for requests, but not for wsgi. I'm again clueless as to why. 😅 I seem to have fixed this by renaming the module.

More tests will follow. ✔️

@Oberon00 Oberon00 force-pushed the requests-integration branch 2 times, most recently from 2f97089 to 45ecfa1 Compare August 21, 2019 14:42
@Oberon00 Oberon00 marked this pull request as ready for review August 21, 2019 14:44
# TODO: The OTel spec says "SpanKind" MUST be "Client" but that
# seems to be a leftover, as Spans have no explicit "Client"
# field.
span.set_attribute("http.method", method.upper())
Copy link
Member

Choose a reason for hiding this comment

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

Curious, when do we prefer double quote vs. single quote for literal strings?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, true. I have been inconsistent here. Normally I just use single quotes for everything but I read up on Black and it seems to format everything with double quotes.


return result

# TODO: How to handle exceptions? Should we create events for them? Set
Copy link
Member

Choose a reason for hiding this comment

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

We will need to capture exception, call Span.SetStatus, and rethrow if we got anything.
I guess we don't need to create event for exception as the user might not want it (e.g. the user might decide to capture exception at higher level and attach event). Having a TODO is good to go for now, this can be a separate PR.

import opentelemetry.ext.http_requests
from opentelemetry.trace import tracer

enable_requests_integration.enable(tracer())
Copy link
Member

Choose a reason for hiding this comment

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

Where is enable_requests_integration?


# TODO: We should also instrument requests.sessions.Session.send
# but to avoid doubled spans, we would need some context-local
# state (i.e., only create a Span if the current context's URL is
Copy link
Member

Choose a reason for hiding this comment

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

Would it work to instrument only requests.sessions.Session.send?

Copy link
Member Author

Choose a reason for hiding this comment

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

Then we would see each redirect separately but we wouldn't have an overarching span for the complete logical request including redirects.

Copy link
Member

@reyang reyang left a comment

Choose a reason for hiding this comment

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

I know we have several TODOs, but this is good enough to move forward :)

@Oberon00 Oberon00 requested review from reyang and a-feld August 22, 2019 12:10
Copy link
Member

@reyang reyang left a comment

Choose a reason for hiding this comment

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

LGTM, we can leave the TODOs for another PR.

Copy link
Member

@a-feld a-feld left a comment

Choose a reason for hiding this comment

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

Agree with @reyang , this seems like a good start for this integration and the limitations / TODOs are clearly spelled out! 🎉

@Oberon00
Copy link
Member Author

I will wait for #95 to be merged because I know that I at least used assert_called or something like that and after rebasing & fixing CI, I will merge.

# limitations under the License.
#
[metadata]
name = opentelemetry-ext-http-requests
Copy link
Member

Choose a reason for hiding this comment

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

why "http-requests"? It feels to me like calling this "opentelemetry-ext-requests" might be clearer.

For example, I imagine flask integration being along the lines of "opentelemetry-ext-flask" rather than "opentelemetry-ext-httpserver-requests".

Trying to put how I feel into words, I think one who is looking for an integration will find it via the package name alone, and the additional categorization is unneeded.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree but it seems that pylint gets confused when naming it requests. Please look at the commit history of this PR.

# if the SDK/tracer is already using `requests` they may, in theory, bypass our
# instrumentation when using `import from`, etc. (currently we only instrument
# a instance method so the probability for that is very low).
def enable(tracer):
Copy link
Member

Choose a reason for hiding this comment

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

could this be refactored to allow one to invoke injecting the tracer into a single requests Session?

I think the global patch should be a provided method as well, but I think it's important to allow consumers to configure multiple http clients. A global patch doesn't provide that flexibility.

packages=find:
install_requires =
opentelemetry-api >= 0.1.dev0
requests ~= 2.0
Copy link
Member

Choose a reason for hiding this comment

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

this may cause issues if a consumer wants to use requests greater than 2.0. I would argue we should err on the side of minimal version range restrictions to reduce friction as the requests projects revs their version for whatever reason.

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean, we should allow requests3? From reading its homepage it seems to be significantly different though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps >=2.0,<3.0 ?

Copy link
Member

Choose a reason for hiding this comment

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

I think requests ~= 2.0 is equivalent to >=2.0,<3.0.

From pep 440, I think this translates to >=2.0,==2.*

Oberon00 and others added 10 commits August 26, 2019 18:21
See previous CI failure for pylint issue:

************* Module ext/opentelemetry-ext-requests/src/__init__.py
ext/opentelemetry-ext-requests/src/__init__.py:1:0: F0001: No module named ext/opentelemetry-ext-requests/src/__init__.py (fatal)

It seems that pylint gets confused when there is more than one "requests" module??
@Oberon00 Oberon00 force-pushed the requests-integration branch from 5b56ec3 to 6a98f52 Compare August 26, 2019 16:24
@Oberon00 Oberon00 requested a review from lzchen as a code owner August 26, 2019 16:24
@Oberon00
Copy link
Member Author

I'm merging this now, but if someone finds a way to change the name from http-requests to requests without breaking pylint, I'm happy for follow-up PRs!

@Oberon00 Oberon00 merged commit b868b5a into open-telemetry:master Aug 27, 2019
@Oberon00
Copy link
Member Author

Implements #79

@Oberon00 Oberon00 deleted the requests-integration branch August 28, 2019 12:37
srikanthccv pushed a commit to srikanthccv/opentelemetry-python that referenced this pull request Nov 1, 2020
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.

5 participants