-
Notifications
You must be signed in to change notification settings - Fork 903
opentelemetry-exporter-otlp-proto-http: auto-append signal path when base URL is passed as endpoint #5273
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
Open
aleksei140888
wants to merge
3
commits into
open-telemetry:main
Choose a base branch
from
aleksei140888:fix/otlp-http-base-url-auto-path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+116
−18
Open
opentelemetry-exporter-otlp-proto-http: auto-append signal path when base URL is passed as endpoint #5273
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-exporter-otlp-proto-http`: auto-append signal path when a base URL (no path or path `/`) is passed as `endpoint` to HTTP OTLP exporters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
|
|
||
| from os import environ | ||
| from typing import Literal | ||
| from urllib.parse import urlparse, urlunparse | ||
|
|
||
| import requests | ||
|
|
||
|
|
@@ -12,6 +13,19 @@ | |
| from opentelemetry.util._importlib_metadata import entry_points | ||
|
|
||
|
|
||
| def _resolve_endpoint_to_signal(endpoint: str, signal_path: str) -> str: | ||
| """Append signal_path to endpoint if it has no signal-specific path. | ||
|
|
||
| Uses proper URL manipulation so query strings and fragments are preserved. | ||
| If the endpoint already has a path other than '/', it is returned unchanged. | ||
| """ | ||
| parsed = urlparse(endpoint) | ||
| if not parsed.path or parsed.path == "/": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use hasattr to check if path exists or not. |
||
| base = parsed.path.rstrip("/") | ||
| return urlunparse(parsed._replace(path=f"{base}/{signal_path}")) | ||
| return endpoint | ||
|
|
||
|
|
||
| def _is_retryable(resp: requests.Response) -> bool: | ||
| if resp.status_code == 408: | ||
| return True | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,6 +259,22 @@ def test_exporter_env(self): | |
| ) | ||
| self.assertIsInstance(exporter._session, requests.Session) | ||
|
|
||
| def test_endpoint_base_url_no_path(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add tests for some malformed, invalid urls |
||
| exporter = OTLPLogExporter(endpoint="http://collector:4318") | ||
| self.assertEqual(exporter._endpoint, "http://collector:4318/v1/logs") | ||
|
|
||
| def test_endpoint_base_url_trailing_slash(self): | ||
| exporter = OTLPLogExporter(endpoint="http://collector:4318/") | ||
| self.assertEqual(exporter._endpoint, "http://collector:4318/v1/logs") | ||
|
|
||
| def test_endpoint_full_url_unchanged(self): | ||
| exporter = OTLPLogExporter(endpoint="http://collector:4318/v1/logs") | ||
| self.assertEqual(exporter._endpoint, "http://collector:4318/v1/logs") | ||
|
|
||
| def test_endpoint_custom_path_unchanged(self): | ||
| exporter = OTLPLogExporter(endpoint="http://collector:4318/custom") | ||
| self.assertEqual(exporter._endpoint, "http://collector:4318/custom") | ||
|
|
||
| @staticmethod | ||
| def export_log_and_deserialize(log): | ||
| with patch("requests.Session.post") as mock_post: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably embed this in a try-catch to avoid crashes.