Skip to content

Commit e9842e6

Browse files
authored
span code attributes feature support (#536)
*Issue #, if available:* * Support OpenTelemetry code attributes for flask, starlette, fastapi, django, pika, aip-pika, celery and aws lambda by default. * User can enable enhanced code attributes for client, producer and internal spans by environment variable `OTEL_AWS_ENHANCED_CODE_ATTRIBUTES=true` *Description of changes:* By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 9fdc363 commit e9842e6

File tree

21 files changed

+219
-439
lines changed

21 files changed

+219
-439
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
9696
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
9797
OTEL_EXPORTER_OTLP_LOGS_HEADERS = "OTEL_EXPORTER_OTLP_LOGS_HEADERS"
98-
CODE_CORRELATION_ENABLED_CONFIG = "OTEL_AWS_CODE_CORRELATION_ENABLED"
98+
OTEL_AWS_ENHANCED_CODE_ATTRIBUTES = "OTEL_AWS_ENHANCED_CODE_ATTRIBUTES"
9999

100100
XRAY_SERVICE = "xray"
101101
LOGS_SERIVCE = "logs"
@@ -473,7 +473,7 @@ def _customize_logs_exporter(log_exporter: LogExporter) -> LogExporter:
473473

474474
def _customize_span_processors(provider: TracerProvider, resource: Resource) -> None:
475475

476-
if get_code_correlation_enabled_status() is True:
476+
if is_enhanced_code_attributes() is True:
477477
# pylint: disable=import-outside-toplevel
478478
from amazon.opentelemetry.distro.code_correlation import CodeAttributesSpanProcessor
479479

@@ -622,32 +622,21 @@ def _is_application_signals_runtime_enabled():
622622
)
623623

624624

625-
def get_code_correlation_enabled_status() -> Optional[bool]:
625+
def is_enhanced_code_attributes() -> bool:
626626
"""
627-
Get the code correlation enabled status from environment variable.
627+
Get the enhanced code attributes enabled status from environment variable.
628628
629629
Returns:
630-
True if OTEL_AWS_CODE_CORRELATION_ENABLED is set to 'true'
631-
False if OTEL_AWS_CODE_CORRELATION_ENABLED is set to 'false'
632-
None if OTEL_AWS_CODE_CORRELATION_ENABLED is not set (default state)
630+
True if OTEL_AWS_ENHANCED_CODE_ATTRIBUTES is set to 'true'
631+
else False
633632
"""
634-
env_value = os.environ.get(CODE_CORRELATION_ENABLED_CONFIG)
635-
636-
if env_value is None:
637-
return None # Default state - environment variable not set
633+
env_value = os.environ.get(OTEL_AWS_ENHANCED_CODE_ATTRIBUTES, "false")
638634

639635
env_value_lower = env_value.strip().lower()
640636
if env_value_lower == "true":
641637
return True
642-
if env_value_lower == "false":
643-
return False
644-
# Invalid value, treat as default and log warning
645-
_logger.warning(
646-
"Invalid value for %s: %s. Expected 'true' or 'false'. Using default.",
647-
CODE_CORRELATION_ENABLED_CONFIG,
648-
env_value,
649-
)
650-
return None
638+
639+
return False
651640

652641

653642
def _is_lambda_environment():

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/code_correlation/config.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
from typing import Any, Dict, List, Optional
1616

1717
# Environment variable constants
18-
_ENV_CONFIG = "OTEL_AWS_CODE_CORRELATION_CONFIG"
18+
_ENV_CONFIG = "OTEL_AWS_CODE_ATTRIBUTES_CONFIG"
1919

2020
_logger = logging.getLogger(__name__)
2121

2222

23-
class AwsCodeCorrelationConfig:
23+
class AwsCodeAttributesConfig:
2424
"""
2525
Configuration manager for AWS OpenTelemetry code correlation features.
2626
@@ -29,10 +29,10 @@ class AwsCodeCorrelationConfig:
2929
and stack trace depth configuration.
3030
3131
Environment Variables:
32-
OTEL_AWS_CODE_CORRELATION_CONFIG: JSON configuration with detailed settings
32+
OTEL_AWS_CODE_ATTRIBUTES_CONFIG: JSON configuration with detailed settings
3333
3434
Example Configuration:
35-
export OTEL_AWS_CODE_CORRELATION_CONFIG='{
35+
export OTEL_AWS_CODE_ATTRIBUTES_CONFIG='{
3636
"include": ["myapp", "mylib"],
3737
"exclude": ["third-party", "vendor"],
3838
"stack_depth": 5
@@ -55,12 +55,12 @@ def __init__(
5555
self.stack_depth = stack_depth
5656

5757
@classmethod
58-
def from_env(cls) -> "AwsCodeCorrelationConfig":
58+
def from_env(cls) -> "AwsCodeAttributesConfig":
5959
"""
6060
Create configuration instance from environment variables.
6161
6262
Returns:
63-
AwsCodeCorrelationConfig: Configured instance
63+
AwsCodeAttributesConfig: Configured instance
6464
"""
6565
config_data = cls._parse_config_data()
6666
include_value = cls._validate_string_list(config_data, "include")
@@ -168,7 +168,7 @@ def to_json(self, indent: Optional[int] = 2) -> str:
168168
def __repr__(self) -> str:
169169
"""Return string representation of the configuration."""
170170
return (
171-
f"AwsCodeCorrelationConfig("
171+
f"AwsCodeAttributesConfig("
172172
f"include={self.include}, "
173173
f"exclude={self.exclude}, "
174174
f"stack_depth={self.stack_depth})"

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/code_correlation/internal/packages_resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
from pathlib import Path
2121
from typing import Any, Callable, Dict, List, NamedTuple, Optional, Set, Union
2222

23-
from ..config import AwsCodeCorrelationConfig
23+
from ..config import AwsCodeAttributesConfig
2424

2525
# Module-level constants
2626
_logger = logging.getLogger(__name__)
2727

2828
# Configuration
29-
_code_attributes_config = AwsCodeCorrelationConfig.from_env()
29+
_code_attributes_config = AwsCodeAttributesConfig.from_env()
3030

3131
# Global caching variables
3232
_sys_path_hash: Optional[int] = None

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_aio_pika_patches.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import functools
1010
import logging
1111

12-
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import get_code_correlation_enabled_status
1312
from amazon.opentelemetry.distro.code_correlation.utils import record_code_attributes
1413

1514
logger = logging.getLogger(__name__)
@@ -32,9 +31,6 @@ def patched_decorate(self, callback):
3231
def _apply_aio_pika_instrumentation_patches():
3332
"""Apply aio-pika patches if code correlation is enabled."""
3433
try:
35-
if get_code_correlation_enabled_status() is not True:
36-
return
37-
3834
# Import CallbackDecorator inside function to allow proper testing
3935
try:
4036
# pylint: disable=import-outside-toplevel

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_celery_patches.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import logging
1313
from typing import Any, Callable, Optional
1414

15-
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import get_code_correlation_enabled_status
1615
from amazon.opentelemetry.distro.code_correlation.utils import add_code_attributes_to_span
1716

1817
logger = logging.getLogger(__name__)
@@ -125,9 +124,6 @@ def _apply_celery_instrumentation_patches():
125124
Apply code correlation patches to the Celery instrumentation.
126125
"""
127126
try:
128-
if get_code_correlation_enabled_status() is not True:
129-
return
130-
131127
if CeleryInstrumentor is None:
132128
logger.warning("Failed to apply Celery patches: CeleryInstrumentor not available")
133129
return

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_django_patches.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
from logging import getLogger
66

7-
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import get_code_correlation_enabled_status
8-
97
_logger = getLogger(__name__)
108

119

@@ -17,8 +15,7 @@ def _apply_django_instrumentation_patches() -> None:
1715
to spans by modifying the process_view method of the Django middleware.
1816
Also patches Django's path/re_path functions for URL pattern instrumentation.
1917
"""
20-
if get_code_correlation_enabled_status() is True:
21-
_apply_django_code_attributes_patch()
18+
_apply_django_code_attributes_patch()
2219

2320

2421
def _apply_django_code_attributes_patch() -> None: # pylint: disable=too-many-statements

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_fastapi_patches.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
from logging import getLogger
66

7-
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import get_code_correlation_enabled_status
8-
97
_logger = getLogger(__name__)
108

119

@@ -16,8 +14,7 @@ def _apply_fastapi_instrumentation_patches() -> None:
1614
This patches the FastAPI instrumentation to automatically add code attributes
1715
to spans by decorating view functions with record_code_attributes.
1816
"""
19-
if get_code_correlation_enabled_status() is True:
20-
_apply_fastapi_code_attributes_patch()
17+
_apply_fastapi_code_attributes_patch()
2118

2219

2320
def _apply_fastapi_code_attributes_patch() -> None:

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_flask_patches.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
from logging import getLogger
66

7-
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import get_code_correlation_enabled_status
8-
97
_logger = getLogger(__name__)
108

119

@@ -16,8 +14,7 @@ def _apply_flask_instrumentation_patches() -> None:
1614
This patches the Flask instrumentation to automatically add code attributes
1715
to spans by decorating view functions with record_code_attributes.
1816
"""
19-
if get_code_correlation_enabled_status() is True:
20-
_apply_flask_code_attributes_patch()
17+
_apply_flask_code_attributes_patch()
2118

2219

2320
def _apply_flask_code_attributes_patch() -> None: # pylint: disable=too-many-statements

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_pika_patches.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import functools
1010
import logging
1111

12-
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import get_code_correlation_enabled_status
1312
from amazon.opentelemetry.distro.code_correlation.utils import add_code_attributes_to_span
1413

1514
logger = logging.getLogger(__name__)
@@ -43,9 +42,6 @@ def enhanced_consume_hook(span, body, properties):
4342
def _apply_pika_instrumentation_patches():
4443
"""Apply pika patches if code correlation is enabled."""
4544
try:
46-
if get_code_correlation_enabled_status() is not True:
47-
return
48-
4945
# Import pika_utils inside function to allow proper testing
5046
try:
5147
# pylint: disable=import-outside-toplevel

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_starlette_patches.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Collection
66

77
from amazon.opentelemetry.distro._utils import is_agent_observability_enabled
8-
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import get_code_correlation_enabled_status
98

109
_logger: Logger = getLogger(__name__)
1110

@@ -16,8 +15,7 @@ def _apply_starlette_instrumentation_patches() -> None:
1615
This applies both version compatibility patches and code attributes support.
1716
"""
1817
_apply_starlette_version_patches()
19-
if get_code_correlation_enabled_status() is True:
20-
_apply_starlette_code_attributes_patch()
18+
_apply_starlette_code_attributes_patch()
2119

2220

2321
# Upstream fix available in OpenTelemetry 1.34.0/0.55b0 (2025-06-04)

0 commit comments

Comments
 (0)