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

botocore: switch setter for injecting headers into propagator #264

Conversation

alertedsnake
Copy link
Contributor

Description

Occasionally not all of the headers available to botocore are strings - I've noticed this when a call is being made with an active span passed from a different application (specifically via gRPC). Botocore does allow this, using the add_header method rather than __setitem__. According to the docs for botocore.compat.HTTPHeaders:

If a parameter value contains non-ASCII characters it can be specified
as a three-tuple of (charset, language, value), in which case it will be
encoded according to RFC2231 rules.  Otherwise it will be encoded using
the utf-8 charset and a language of ''

Whereas using __setitem__ does not do this conversion.

I'm not convinced this is the right place to fix this, however - I've only encountered it in the situation described in #262, and haven't been able to find what would be passing in a non-string header in the first place. I'll be happy to dig more if anyone has a better suggestion, but as far as I can tell, this is also an innocuous change.

One question I have though is about how I could test this - I don't know how to simulate (in the botocore tests) having an active trace passed through from another application, so advice on this would be welcome.

Fixes #262

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

All existing tests still pass, and I've been working with the situation described in #262

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • 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

Occasionally not all of the headers available to botocore are strings.
It does allow this, using the `add_header` method rather than
`__setitem__`.  According to the docs for `botocore.compat.HTTPHeaders`:

```
If a parameter value contains non-ASCII characters it can be specified
as a three-tuple of (charset, language, value), in which case it will be
encoded according to RFC2231 rules.  Otherwise it will be encoded using
the utf-8 charset and a language of ''
```

Whereas using `__setitem__` does not do this conversion.

Fixes open-telemetry#262
@alertedsnake alertedsnake requested review from a team, codeboten and toumorokoshi and removed request for a team December 18, 2020 03:57
@alertedsnake alertedsnake changed the title switch setter for injecting headers into propagator botocore: switch setter for injecting headers into propagator Dec 18, 2020
Copy link
Member

@toumorokoshi toumorokoshi left a comment

Choose a reason for hiding this comment

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

The code seems innocuous, but we should probably root cause + add a test to make sure this is right, as you mentioned. I'll comment more on the other issue.

@@ -67,7 +67,7 @@
def _patched_endpoint_prepare_request(wrapped, instance, args, kwargs):
request = args[0]
headers = request.headers
propagators.inject(type(headers).__setitem__, headers)
propagators.inject(type(headers).add_header, headers)
Copy link
Member

Choose a reason for hiding this comment

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

I don't have a lot of context on this, but we should probably add at least a test for the case you're running into.

From #262, it sounds like the most relevant part is the DyanamoDB API call. have you tried adding a mock DynamiDB api call (moto looks to support dynamodb)? It seems like a call that maps what you're executing in your service should trigger the error.

Copy link
Member

Choose a reason for hiding this comment

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

Taking a closer look, the part I'm a little unclear on is, as you mentioned, what is actually causing this. I have a feeling something was injected that should not have been by the propagator. I'll comment more on the issue.

@alertedsnake
Copy link
Contributor Author

Closing this, it's not the root cause of #262 and so doesn't seem necessary.

nstawski pushed a commit to nstawski/ns-opentelemetry-python-contrib that referenced this pull request May 10, 2023
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.

gRPC client + botocore + datadog exporter instrumentation error
2 participants