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

Update otel-exporter OTLP headers parsing to match format specs #1869

Merged
merged 16 commits into from
May 26, 2021

Conversation

eddyleelin
Copy link
Contributor

@eddyleelin eddyleelin commented May 24, 2021

Description

This PR fixes the way the otel Python Exporter parses OTEL_EXPORTER_OTLP_HEADERS to align with the otel exporter specs. Now, the headers, which are represented in a format matching the W3C Correlation-Context specs for HTTP header formats., are correctly parsed:

Fixes #1851 by casting key string to lowercase, and #1852 by correctly splitting on just the first equals sign '=' and removing optional white spaces (OWS).

Type of change

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

How Has This Been Tested?

  • tox -e test-exporter-otlp-proto-grpc successfully completes.

Does This PR Require a Contrib Repo Change?

No.

Checklist:

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

@eddyleelin eddyleelin requested review from a team, owais and srikanthccv and removed request for a team May 24, 2021 14:26
@@ -229,9 +229,15 @@ def __init__(

self._headers = headers or environ.get(OTEL_EXPORTER_OTLP_HEADERS)
if isinstance(self._headers, str):
self._headers = tuple(
tuple(item.split("=")) for item in self._headers.split(",")
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Your change is totally fine but you could just add another list comprehension to the existing one. Something like

[(key.lower(), value) for key, value in [item.split("=") for item in headers.split(",")]]

since this runs only once, the extra pass over the collection should not be problem. That said, a plain loop may be more obvious/readable to some over nested comprehension. I'll leave it to you.

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 appreciate the feedback! I also thought about just adding another nested list comp, but it was getting a bit out of hand, so I made it into a plain loop! Thanks again 😄

for header_pair in self._headers.split(","):
key, value = header_pair.split("=", maxsplit=1)
key = key.strip().lower()
value = value.strip()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sure if we should strip individual keys and values. Generally we just strip a pair as people tend to add whitespace after comma but don't do it before/after =. That said, I can't think of any case where someone would want whitespace at the beginning or end of a key/value so I guess it's fine.

eddyleelin and others added 2 commits May 25, 2021 15:40
Co-authored-by: Owais Lone <owais@users.noreply.github.com>
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.

Nice! Please just fix lint, @eddyleelin:

  1. tox -e lint
  2. source .tox/lint/bin/activate
  3. black .
  4. isort .
  5. deactivate

This should fix black and isort errors automatically. pylint may still fail, though.

Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

Please update the branch @eddyleelin

@codeboten codeboten merged commit 038bd24 into open-telemetry:main May 26, 2021
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.

Support OTLP_HEADERS with capital case
5 participants