From 5cfa2a70342ae64861a88ffa7cb04981c0e8f18a Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Tue, 6 Aug 2024 09:41:11 -0700 Subject: [PATCH] Local otel installs --- .../pyproject.toml | 6 +- .../instrumentation/aws_lambda/__init__.py | 56 ++++++++++++++++++- .../instrumentation/aws_lambda/version.py | 2 +- .../pyproject.toml | 2 +- .../instrumentation/logging/version.py | 2 +- .../opentelemetry/instrumentation/version.py | 2 +- .../pyproject.toml | 6 +- 7 files changed, 63 insertions(+), 13 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/pyproject.toml b/instrumentation/opentelemetry-instrumentation-aws-lambda/pyproject.toml index 4d6b431d91..290b6077e1 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/pyproject.toml +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/pyproject.toml @@ -25,9 +25,9 @@ classifiers = [ "Programming Language :: Python :: 3.12", ] dependencies = [ - "opentelemetry-instrumentation == 0.48b0.dev", - "opentelemetry-propagator-aws-xray == 1.0.1", - "opentelemetry-semantic-conventions == 0.48b0.dev", + # "opentelemetry-instrumentation == 0.48b0.dev", + # "opentelemetry-propagator-aws-xray == 1.0.1", + # "opentelemetry-semantic-conventions == 0.48b0.dev", ] [project.optional-dependencies] diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py index fb5da8ce48..73f9aee03a 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py @@ -84,6 +84,10 @@ def custom_event_context_extractor(lambda_event): from opentelemetry.instrumentation.utils import unwrap from opentelemetry.metrics import MeterProvider, get_meter_provider from opentelemetry.propagate import get_global_textmap +from opentelemetry.propagators.aws.aws_xray_propagator import ( + TRACE_HEADER_KEY, + AwsXRayPropagator, +) from opentelemetry.semconv.resource import ResourceAttributes from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import ( @@ -93,6 +97,7 @@ def custom_event_context_extractor(lambda_event): get_tracer, get_tracer_provider, ) +from opentelemetry.trace.propagation import get_current_span from opentelemetry.trace.status import Status, StatusCode logger = logging.getLogger(__name__) @@ -138,6 +143,7 @@ def _default_event_context_extractor(lambda_event: Any) -> Context: def _determine_parent_context( lambda_event: Any, event_context_extractor: Callable[[Any], Context], + disable_aws_context_propagation: bool = False, ) -> Context: """Determine the parent context for the current Lambda invocation. @@ -154,11 +160,37 @@ def _determine_parent_context( Returns: A Context with configuration found in the carrier. """ + ## CURRENT + # if event_context_extractor is None: + # return _default_event_context_extractor(lambda_event) + + # return event_context_extractor(lambda_event) + + ## OLD + parent_context = None + + if not disable_aws_context_propagation: + xray_env_var = os.environ.get(_X_AMZN_TRACE_ID) + + if xray_env_var: + parent_context = AwsXRayPropagator().extract( + {TRACE_HEADER_KEY: xray_env_var} + ) - if event_context_extractor is None: - return _default_event_context_extractor(lambda_event) + if ( + parent_context + and get_current_span(parent_context) + .get_span_context() + .trace_flags.sampled + ): + return parent_context + + if event_context_extractor: + parent_context = event_context_extractor(lambda_event) + else: + parent_context = _default_event_context_extractor(lambda_event) - return event_context_extractor(lambda_event) + return parent_context def _set_api_gateway_v1_proxy_attributes( @@ -256,6 +288,7 @@ def _instrument( flush_timeout, event_context_extractor: Callable[[Any], Context], tracer_provider: TracerProvider = None, + disable_aws_context_propagation: bool = False, meter_provider: MeterProvider = None, ): @@ -274,8 +307,14 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches parent_context = _determine_parent_context( lambda_event, event_context_extractor, + False, ) + logger.warning("got parent_context: %s", parent_context) + ## {} on main + ## or on old version + ## {'current-span-417d6a30-a3c2-4c03-9fa3-addf2d6fbe05': NonRecordingSpan(SpanContext(trace_id=0x5fb7331105e8bb83207fa31d4d9cdb4c, span_id=0x3328b8445a6dbad2, trace_flags=0x01, trace_state=[], is_remote=True))} + try: event_source = lambda_event["Records"][0].get( "eventSource" @@ -306,10 +345,20 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches token = context_api.attach(parent_context) try: + + # starting a span... gets/sets new trace_id? with tracer.start_as_current_span( name=orig_handler_name, + # name="FOOOO!", kind=span_kind, ) as span: + + logger.warning("Started span (type %s) as: %s", type(span), span) + ## + ## _Span(name="mocks.lambda_function.handler", context=SpanContext(trace_id=0xebb8e321007b3ff6d058be8466a0c040, span_id=0x7b5c836b83ce7f79, trace_flags=0x01, trace_state=[], is_remote=False)) + logger.warning("trace_id: %s", span.get_span_context().trace_id) + logger.warning("span.parent: %s", span.parent) # !!! None + if span.is_recording(): lambda_context = args[1] # NOTE: The specs mention an exception here, allowing the @@ -467,6 +516,7 @@ def _instrument(self, **kwargs): "event_context_extractor", _default_event_context_extractor ), tracer_provider=kwargs.get("tracer_provider"), + disable_aws_context_propagation=False, meter_provider=kwargs.get("meter_provider"), ) diff --git a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/version.py b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/version.py index 4d21fcb843..bd9c51f813 100644 --- a/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/version.py +++ b/instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.48b0.dev" +__version__ = "0.47b0" diff --git a/instrumentation/opentelemetry-instrumentation-logging/pyproject.toml b/instrumentation/opentelemetry-instrumentation-logging/pyproject.toml index 2c1b2ad897..7104112e9c 100644 --- a/instrumentation/opentelemetry-instrumentation-logging/pyproject.toml +++ b/instrumentation/opentelemetry-instrumentation-logging/pyproject.toml @@ -26,7 +26,7 @@ classifiers = [ ] dependencies = [ "opentelemetry-api ~= 1.12", - "opentelemetry-instrumentation == 0.48b0.dev", + # "opentelemetry-instrumentation == 0.48b0.dev", ] [project.optional-dependencies] diff --git a/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/version.py b/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/version.py index e0bef4b1e3..39773973f1 100644 --- a/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/version.py +++ b/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/version.py @@ -12,6 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.48b0.dev" +__version__ = "0.47b0" _instruments = tuple() diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/version.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/version.py index 4d21fcb843..bd9c51f813 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/version.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.48b0.dev" +__version__ = "0.47b0" diff --git a/propagator/opentelemetry-propagator-aws-xray/pyproject.toml b/propagator/opentelemetry-propagator-aws-xray/pyproject.toml index 546c0790a2..eb3de3a6a3 100644 --- a/propagator/opentelemetry-propagator-aws-xray/pyproject.toml +++ b/propagator/opentelemetry-propagator-aws-xray/pyproject.toml @@ -24,9 +24,9 @@ classifiers = [ "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", ] -dependencies = [ - "opentelemetry-api ~= 1.12", -] +# dependencies = [ +# "opentelemetry-api ~= 1.12", +# ] [project.entry-points.opentelemetry_propagator] xray = "opentelemetry.propagators.aws:AwsXRayPropagator"