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 library spec fidelity #746

Merged

Conversation

victoraugustolls
Copy link
Contributor

@victoraugustolls victoraugustolls commented Aug 5, 2019

Updated requests integration for better fidelity, adding attributes:

  • http host
  • http method
  • http path

Updated span status created by requests integration.
Updated span name to follow specs.

Added http status code to grpc status code mapping as an utility.

else:
# Add the status code to attributes
_tracer.add_attribute_to_current_span(
HTTP_STATUS_CODE, str(result.status_code)
Copy link
Contributor

Choose a reason for hiding this comment

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

According to the spec, status_code is an int64. https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/HTTP.md#mapping-from-http-status-codes-to-trace-status-codes

This again seems to be a spec bug in the test-cases section, where status_code is put as a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is better to leave as int64, following specs. But open for discussion!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to the spec, status_code is an int64. https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/HTTP.md#mapping-from-http-status-codes-to-trace-status-codes

Right now AzureExporter for tracer is broken because Flask, Django and Pyramid integrations set status_code as string, and azures expect int, we can typecast on azure or change the other integrations, which way is preferred?

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/census-instrumentation/opencensus-python/blame/master/contrib/opencensus-ext-azure/opencensus/ext/azure/trace_exporter/__init__.py#L86

My understanding is that we'll still get the right status_code, but the success flag will be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding is that we'll still get the right status_code, but the success flag will be wrong.

Yes, and with this we have both options described above. I can take any of them, I prefer to put the status_code as int, it makes more sense to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with you that we should use int. Converting int to string and then parse it to int sounds like crazy :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will open a new PR to fix this into Flask, Django and Pyramid integrations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did it here.

opencensus/trace/utils.py Outdated Show resolved Hide resolved
Copy link
Contributor

@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.

Looks good in general. I've left a few comments.
I'm a bit surprised to see the bugs in specs. Wish we will do a better job in OpenTelemetry.

Copy link
Contributor

@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.
Thanks @victoraugustolls! Let's wait for one day before merge, see if @c24t has comments.


CANCELLED = Status(code_pb2.CANCELLED)
INVALID_URL = Status(code_pb2.INVALID_ARGUMENT, message='invalid URL')
TIMEOUT = Status(code_pb2.DEADLINE_EXCEEDED, message='request timed out')
Copy link
Member

Choose a reason for hiding this comment

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

@victoraugustolls there's an ongoing discussion about using grpc error codes in OpenTelemetry, if you've got an opinion here you may want to tune into the SIG meeting or follow the OT specs repo.

Copy link
Member

@c24t c24t left a comment

Choose a reason for hiding this comment

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

Code and tests look excellent, and this is a clear improvement.

@reyang reyang merged commit ab47474 into census-instrumentation:master Aug 6, 2019
@victoraugustolls victoraugustolls deleted the feature/requests-specs branch August 6, 2019 22:38
@reyang reyang mentioned this pull request Aug 7, 2019
try:
result = wrapped(*args, **kwargs)
except requests.Timeout:
_span.set_status(exceptions_status.TIMEOUT)
Copy link

Choose a reason for hiding this comment

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

@reyang Isn't this except clause and the ones following going to eat the raised exception and returns None in case of any requests error? Unless I'm missing something this change is going to break many things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but wont requests return None in case it raises an Exception? If so, the returned value is the same

Copy link

Choose a reason for hiding this comment

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

Raising an exception should not "return" at all.

try:
  requests.get("http://not-a-host")
  unreacheable() #actually reached with this PR
except Exception:
  reached() # actually unreachable with this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What can be changed, and is a good idea actually, is to threat the Exception and raise it again, and let the user do whatever it wants. That's what you meant?

Copy link

Choose a reason for hiding this comment

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

This should be fixed by adding a raise statement in every except block.

Copy link

Choose a reason for hiding this comment

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

The main goal here is to keep this hook transparent and keep 1:1 interface as requests (Exceptions are part of that interface).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What can be changed, and is a good idea actually, is to threat the Exception and raise it again, and let the user do whatever it wants. That's what you meant?

So you agreed with this, right? Just to be clear. If so, I do agree with you and I will open a PR to change this.

Copy link

Choose a reason for hiding this comment

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

Yes 👍 !

Copy link
Contributor

Choose a reason for hiding this comment

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

@isra17 You're right, we've missed this. It is a bug.

url = 'http://localhost:8080/test'

with patch, patch_thread:
wrapped(url)
Copy link

Choose a reason for hiding this comment

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

To be clearer, given mock_func raise an exception, this test case should fail gith here and never go further. The test should actually assert the requests.TooManyRedirects exception is raised from wrapped(url).

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

Successfully merging this pull request may close these issues.

None yet

5 participants