-
Notifications
You must be signed in to change notification settings - Fork 22
Auto-Configure ADOT SDK Defaults for Genesis #392
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
yiyuan-he
wants to merge
11
commits into
aws-observability:main
Choose a base branch
from
yiyuan-he:configure-genesis-defaults
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.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0581402
configure adot sdk defaults for genesis
yiyuan-he 2c5ffd6
fix lint checks
yiyuan-he 6fc172e
re-use botocore aws region fallback logic as much as possible
yiyuan-he 86d6a36
Bump OTel Dependency Versions to 1.33.0/0.54b0 (#388)
yiyuan-he 9291dad
Revert "Bump OTel Dependency Versions to 1.33.0/0.54b0" (#397)
yiyuan-he 42dcae7
adding release safety via environment (#384)
srprash 4f345e7
Add Explicit Distro and Configurator Env Var Config to Contract Test …
yiyuan-he ddaf5b4
manually fix lint check
yiyuan-he e158cff
Merge branch 'main' into configure-genesis-defaults
yiyuan-he e7eca9b
Merge branch 'main' into configure-genesis-defaults
yiyuan-he 325fa41
Merge branch 'main' into configure-genesis-defaults
yiyuan-he 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
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
63 changes: 63 additions & 0 deletions
63
aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelemetry_distro.py
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 |
---|---|---|
@@ -1,13 +1,76 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import os | ||
from unittest import TestCase | ||
from unittest.mock import patch | ||
|
||
from pkg_resources import DistributionNotFound, require | ||
|
||
from amazon.opentelemetry.distro.aws_opentelemetry_distro import AwsOpenTelemetryDistro | ||
|
||
|
||
class TestAwsOpenTelemetryDistro(TestCase): | ||
def test_package_available(self): | ||
try: | ||
require(["aws-opentelemetry-distro"]) | ||
except DistributionNotFound: | ||
self.fail("aws-opentelemetry-distro not installed") | ||
|
||
def setUp(self): | ||
# Store original env vars for agent observability tests | ||
self.original_env = {} | ||
env_vars = [ | ||
"AGENT_OBSERVABILITY_ENABLED", | ||
"OTEL_TRACES_SAMPLER", | ||
"OTEL_PYTHON_DISABLED_INSTRUMENTATIONS", | ||
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED", | ||
"OTEL_AWS_APPLICATION_SIGNALS_ENABLED", | ||
] | ||
for var in env_vars: | ||
self.original_env[var] = os.environ.get(var) | ||
os.environ.pop(var, None) | ||
|
||
def tearDown(self): | ||
# Restore original env vars | ||
for var, value in self.original_env.items(): | ||
if value is None: | ||
os.environ.pop(var, None) | ||
else: | ||
os.environ[var] = value | ||
|
||
def test_agent_observability_sets_new_defaults(self): | ||
# Set up the environment to trigger agent observability | ||
os.environ["AGENT_OBSERVABILITY_ENABLED"] = "true" | ||
|
||
# Import and configure | ||
with patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches"): | ||
with patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.get_aws_region", return_value="us-west-2"): | ||
# We need to mock the parent class to avoid its side effects | ||
with patch("opentelemetry.distro.OpenTelemetryDistro._configure"): | ||
distro = AwsOpenTelemetryDistro() | ||
distro._configure() | ||
|
||
# Check the new defaults are set | ||
self.assertEqual(os.environ.get("OTEL_TRACES_SAMPLER"), "parentbased_always_on") | ||
self.assertEqual( | ||
os.environ.get("OTEL_PYTHON_DISABLED_INSTRUMENTATIONS"), | ||
"http,sqlalchemy,psycopg2,pymysql,sqlite3,aiopg,asyncpg,mysql_connector," | ||
"botocore,boto3,urllib3,requests,starlette", | ||
) | ||
self.assertEqual(os.environ.get("OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED"), "true") | ||
self.assertEqual(os.environ.get("OTEL_AWS_APPLICATION_SIGNALS_ENABLED"), "false") | ||
|
||
def test_new_defaults_not_set_when_agent_observability_disabled(self): | ||
# Don't set AGENT_OBSERVABILITY_ENABLED or set it to false | ||
os.environ.pop("AGENT_OBSERVABILITY_ENABLED", None) | ||
|
||
with patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches"): | ||
with patch("opentelemetry.distro.OpenTelemetryDistro._configure"): | ||
distro = AwsOpenTelemetryDistro() | ||
distro._configure() | ||
|
||
# These should not be set when agent observability is disabled | ||
self.assertNotIn("OTEL_TRACES_SAMPLER", os.environ) | ||
self.assertNotIn("OTEL_PYTHON_DISABLED_INSTRUMENTATIONS", os.environ) | ||
self.assertNotIn("OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED", os.environ) | ||
self.assertNotIn("OTEL_AWS_APPLICATION_SIGNALS_ENABLED", os.environ) |
147 changes: 147 additions & 0 deletions
147
aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_utils.py
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,147 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
import sys | ||
from unittest import TestCase | ||
from unittest.mock import MagicMock, patch | ||
|
||
from amazon.opentelemetry.distro._utils import get_aws_region | ||
|
||
|
||
class TestGetAwsRegion(TestCase): | ||
def setUp(self): | ||
# Clear environment variables before each test | ||
os.environ.pop("AWS_REGION", None) | ||
os.environ.pop("AWS_DEFAULT_REGION", None) | ||
|
||
def tearDown(self): | ||
# Clean up environment variables after each test | ||
os.environ.pop("AWS_REGION", None) | ||
os.environ.pop("AWS_DEFAULT_REGION", None) | ||
|
||
@patch("amazon.opentelemetry.distro._utils.is_installed") | ||
def test_get_aws_region_from_aws_region_env(self, mock_is_installed): | ||
mock_is_installed.return_value = True | ||
|
||
# Mock botocore module and session | ||
mock_botocore = MagicMock() | ||
mock_session_instance = MagicMock() | ||
mock_session_instance.region_name = "us-west-2" | ||
mock_botocore.session.Session.return_value = mock_session_instance | ||
sys.modules["botocore"] = mock_botocore | ||
sys.modules["botocore.session"] = mock_botocore.session | ||
|
||
os.environ["AWS_REGION"] = "us-west-2" | ||
|
||
try: | ||
self.assertEqual(get_aws_region(), "us-west-2") | ||
finally: | ||
# Clean up mock | ||
if "botocore" in sys.modules: | ||
del sys.modules["botocore"] | ||
if "botocore.session" in sys.modules: | ||
del sys.modules["botocore.session"] | ||
|
||
@patch("amazon.opentelemetry.distro._utils.is_installed") | ||
def test_get_aws_region_from_aws_default_region_env(self, mock_is_installed): | ||
mock_is_installed.return_value = True | ||
|
||
# Mock botocore module and session | ||
mock_botocore = MagicMock() | ||
mock_session_instance = MagicMock() | ||
mock_session_instance.region_name = "eu-central-1" | ||
mock_botocore.session.Session.return_value = mock_session_instance | ||
sys.modules["botocore"] = mock_botocore | ||
sys.modules["botocore.session"] = mock_botocore.session | ||
|
||
os.environ["AWS_DEFAULT_REGION"] = "eu-central-1" | ||
|
||
try: | ||
self.assertEqual(get_aws_region(), "eu-central-1") | ||
finally: | ||
# Clean up mock | ||
if "botocore" in sys.modules: | ||
del sys.modules["botocore"] | ||
if "botocore.session" in sys.modules: | ||
del sys.modules["botocore.session"] | ||
|
||
@patch("amazon.opentelemetry.distro._utils.is_installed") | ||
def test_get_aws_region_prefers_aws_region_over_default(self, mock_is_installed): | ||
mock_is_installed.return_value = True | ||
|
||
# Mock botocore module and session | ||
mock_botocore = MagicMock() | ||
mock_session_instance = MagicMock() | ||
mock_session_instance.region_name = "us-east-1" | ||
mock_botocore.session.Session.return_value = mock_session_instance | ||
sys.modules["botocore"] = mock_botocore | ||
sys.modules["botocore.session"] = mock_botocore.session | ||
|
||
os.environ["AWS_REGION"] = "us-east-1" | ||
os.environ["AWS_DEFAULT_REGION"] = "eu-west-1" | ||
|
||
try: | ||
self.assertEqual(get_aws_region(), "us-east-1") | ||
finally: | ||
# Clean up mock | ||
if "botocore" in sys.modules: | ||
del sys.modules["botocore"] | ||
if "botocore.session" in sys.modules: | ||
del sys.modules["botocore.session"] | ||
|
||
@patch("amazon.opentelemetry.distro._utils.is_installed") | ||
def test_get_aws_region_from_botocore_session(self, mock_is_installed): | ||
mock_is_installed.return_value = True | ||
|
||
# Mock botocore module and session | ||
mock_botocore = MagicMock() | ||
mock_session_instance = MagicMock() | ||
mock_session_instance.region_name = "ap-southeast-1" | ||
mock_botocore.session.Session.return_value = mock_session_instance | ||
sys.modules["botocore"] = mock_botocore | ||
sys.modules["botocore.session"] = mock_botocore.session | ||
|
||
try: | ||
result = get_aws_region() | ||
self.assertEqual(result, "ap-southeast-1") | ||
finally: | ||
# Clean up mock | ||
if "botocore" in sys.modules: | ||
del sys.modules["botocore"] | ||
if "botocore.session" in sys.modules: | ||
del sys.modules["botocore.session"] | ||
|
||
@patch("amazon.opentelemetry.distro._utils.is_installed") | ||
@patch("amazon.opentelemetry.distro._utils._logger") | ||
def test_get_aws_region_returns_none_when_no_region_found(self, mock_logger, mock_is_installed): | ||
mock_is_installed.return_value = False | ||
|
||
result = get_aws_region() | ||
|
||
self.assertIsNone(result) | ||
mock_logger.warning.assert_called_once() | ||
|
||
@patch("amazon.opentelemetry.distro._utils.is_installed") | ||
@patch("amazon.opentelemetry.distro._utils._logger") | ||
def test_get_aws_region_returns_none_when_botocore_has_no_region(self, mock_logger, mock_is_installed): | ||
mock_is_installed.return_value = True | ||
|
||
# Mock botocore module with no region | ||
mock_botocore = MagicMock() | ||
mock_session_instance = MagicMock() | ||
mock_session_instance.region_name = None | ||
mock_botocore.session.Session.return_value = mock_session_instance | ||
sys.modules["botocore"] = mock_botocore | ||
sys.modules["botocore.session"] = mock_botocore.session | ||
|
||
try: | ||
result = get_aws_region() | ||
self.assertIsNone(result) | ||
mock_logger.warning.assert_called_once() | ||
finally: | ||
# Clean up mock | ||
if "botocore" in sys.modules: | ||
del sys.modules["botocore"] | ||
if "botocore.session" in sys.modules: | ||
del sys.modules["botocore.session"] |
Oops, something went wrong.
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.
If region and aws otlp endpoints are not set, ADOT will pick the default otlp exporter with default endpoints, correct?