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

Simplify python wrapper #157

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/sample-apps/function/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ async def callAioHttp():

# lambda function
def lambda_handler(event, context):
print(boto3.__file__)

loop = asyncio.get_event_loop()
loop.run_until_complete(callAioHttp())

print("fetching buckets")
for bucket in s3.buckets.all():
print(bucket.name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@
# Ref Doc: https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html

import boto3
from opentelemetry.instrumentation.aws_lambda import (
AwsLambdaInstrumentor
)

# Enable instrumentation
AwsLambdaInstrumentor().instrument(skip_dep_check=True)
from opentelemetry.instrumentation.aws_lambda import otel_handler

# Lambda function
@otel_handler
def lambda_handler(event, context):
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
Expand All @@ -44,16 +40,12 @@ def lambda_handler(event, context):
---
"""

import functools
import logging
import os
from importlib import import_module
from typing import Any, Collection

from opentelemetry.context.context import Context
from opentelemetry.instrumentation.aws_lambda.package import _instruments
from opentelemetry.instrumentation.aws_lambda.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.utils import unwrap
from opentelemetry.propagate import get_global_textmap
from opentelemetry.sdk.extension.aws.trace.propagation.aws_xray_format import (
TRACE_HEADER_KEY,
Expand All @@ -67,58 +59,10 @@ def lambda_handler(event, context):
get_tracer_provider,
)
from opentelemetry.trace.propagation import get_current_span
from wrapt import wrap_function_wrapper

logger = logging.getLogger(__name__)


class AwsLambdaInstrumentor(BaseInstrumentor):
def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

def _instrument(self, **kwargs):
"""Instruments Lambda Handlers on AWS Lambda.

See more:
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#instrumenting-aws-lambda

Args:
**kwargs: Optional arguments
``tracer_provider``: a TracerProvider, defaults to global
``event_context_extractor``: a method which takes the Lambda
Event as input and extracts an OTel Context from it. By default,
the context is extracted from the HTTP headers of an API Gateway
request.
"""
tracer = get_tracer(
__name__, __version__, kwargs.get("tracer_provider")
)
event_context_extractor = kwargs.get("event_context_extractor")

lambda_handler = os.environ.get(
"ORIG_HANDLER", os.environ.get("_HANDLER")
)
wrapped_names = lambda_handler.rsplit(".", 1)
self._wrapped_module_name = wrapped_names[0]
self._wrapped_function_name = wrapped_names[1]

flush_timeout = os.environ.get("OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT", 30000)

_instrument(
tracer,
self._wrapped_module_name,
self._wrapped_function_name,
flush_timeout,
event_context_extractor
)

def _uninstrument(self, **kwargs):
unwrap(
import_module(self._wrapped_module_name),
self._wrapped_function_name,
)


def _default_event_context_extractor(lambda_event: Any) -> Context:
"""Default way of extracting the context from the Lambda Event.

Expand All @@ -145,60 +89,56 @@ def _default_event_context_extractor(lambda_event: Any) -> Context:
return get_global_textmap().extract(headers)


def _instrument(
tracer: Tracer,
wrapped_module_name,
wrapped_function_name,
flush_timeout,
event_context_extractor=None
):
def _determine_parent_context(lambda_event: Any) -> Context:
"""Determine the parent context for the current Lambda invocation.
def _determine_parent_context(lambda_event: Any) -> Context:
"""Determine the parent context for the current Lambda invocation.

See more:
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#determining-the-parent-of-a-span
See more:
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#determining-the-parent-of-a-span

Args:
lambda_event: user-defined, so it could be anything, but this
method counts it being a map with a 'headers' key
Returns:
A Context with configuration found in the carrier.
"""
parent_context = None
Args:
lambda_event: user-defined, so it could be anything, but this
method counts it being a map with a 'headers' key
Returns:
A Context with configuration found in the carrier.
"""
parent_context = None

xray_env_var = os.environ.get("_X_AMZN_TRACE_ID")
xray_env_var = os.environ.get("_X_AMZN_TRACE_ID")

if xray_env_var:
parent_context = AwsXRayFormat().extract(
{TRACE_HEADER_KEY: xray_env_var}
)
if xray_env_var:
parent_context = AwsXRayFormat().extract(
{TRACE_HEADER_KEY: xray_env_var}
)

if (
if (
parent_context
and get_current_span(parent_context)
.get_span_context()
.trace_flags.sampled
):
return parent_context
):
return parent_context

if event_context_extractor:
parent_context = event_context_extractor(lambda_event)
else:
parent_context = _default_event_context_extractor(lambda_event)
parent_context = _default_event_context_extractor(lambda_event)

return parent_context
return parent_context

def _instrumented_lambda_handler_call(call_wrapped, instance, args, kwargs):

tracer = get_tracer(__name__, "0.16.dev0")
flush_timeout = int(os.environ.get("OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT", 30000))

def otel_handler(orig_handler):
@functools.wraps(orig_handler)
def otel_wrapper(*args, **kwargs):
orig_handler_name = ".".join(
[wrapped_module_name, wrapped_function_name]
[orig_handler.__module__, orig_handler.__name__]
)

lambda_event = args[0]

parent_context = _determine_parent_context(lambda_event)

with tracer.start_as_current_span(
name=orig_handler_name, context=parent_context, kind=SpanKind.SERVER
name=orig_handler_name, context=parent_context, kind=SpanKind.SERVER
) as span:
if span.is_recording():
lambda_context = args[1]
Expand All @@ -219,16 +159,11 @@ def _instrumented_lambda_handler_call(call_wrapped, instance, args, kwargs):
os.environ.get("AWS_LAMBDA_FUNCTION_VERSION"),
)

result = call_wrapped(*args, **kwargs)
result = orig_handler(*args, **kwargs)

# force_flush before function quit in case of Lambda freeze.
tracer_provider = get_tracer_provider()
tracer_provider.force_flush(flush_timeout)

return result

wrap_function_wrapper(
wrapped_module_name,
wrapped_function_name,
_instrumented_lambda_handler_call,
)
return otel_wrapper

This file was deleted.

This file was deleted.

51 changes: 13 additions & 38 deletions python/src/otel/otel_sdk/otel-instrument
Original file line number Diff line number Diff line change
@@ -1,44 +1,19 @@
#!/usr/bin/env python3
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#!/bin/bash

from os import environ, system
import sys

# the path to the interpreter and all of the originally intended arguments
args = sys.argv[1:]
# TODO(anuraaga): Loosen parsing of env variable since it's common to use this sort of pattern in shell scripts.
# https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py#L273
#if [[ $OTEL_RESOURCE_ATTRIBUTES != *"service.name="* ]]; then
#export OTEL_RESOURCE_ATTRIBUTES="service.name=${AWS_LAMBDA_FUNCTION_NAME},${OTEL_RESOURCE_ATTRIBUTES}"
#fi

# enable OTel wrapper
environ["ORIG_HANDLER"] = environ.get("_HANDLER")
environ["_HANDLER"] = "otel_wrapper.lambda_handler"
export OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT=10000

# config default traces exporter if missing
environ.setdefault("OTEL_TRACES_EXPORTER", "otlp_proto_grpc_span")
export PYTHONPATH="/opt/python:$LAMBDA_TASK_ROOT:$LAMBDA_RUNTIME_DIR:$PYTHONPATH"

# set service name
if environ.get("OTEL_RESOURCE_ATTRIBUTES") is None:
environ["OTEL_RESOURCE_ATTRIBUTES"] = "service.name=%s" % (
environ.get("AWS_LAMBDA_FUNCTION_NAME")
)
elif "service.name=" not in environ.get("OTEL_RESOURCE_ATTRIBUTES"):
environ["OTEL_RESOURCE_ATTRIBUTES"] = "service.name=%s,%s" % (
environ.get("AWS_LAMBDA_FUNCTION_NAME"),
environ.get("OTEL_RESOURCE_ATTRIBUTES"),
)
export OTEL_INSTRUMENTATION_AWS_LAMBDA_HANDLER="$_HANDLER"
# Note, the file structure is important. Lambda does not seem to allow loading the handler if it is placed at a
# subdirectory such as opentelemetry.instrumentation.lambda.handler, when inside a layer it must be at the top level.
export _HANDLER="otel_wrapper.lambda_handler"

# TODO: Remove if sdk support resource detector env variable configuration.
lambda_resource_attributes = (
"cloud.region=%s,cloud.provider=aws,faas.name=%s,faas.version=%s"
% (
environ.get("AWS_REGION"),
environ.get("AWS_LAMBDA_FUNCTION_NAME"),
environ.get("AWS_LAMBDA_FUNCTION_VERSION"),
)
)
environ["OTEL_RESOURCE_ATTRIBUTES"] = "%s,%s" % (
lambda_resource_attributes,
environ.get("OTEL_RESOURCE_ATTRIBUTES"),
)

# start the runtime with the extra options
system(" ".join(args))
exec /opt/python/bin/opentelemetry-instrument "$@"
Loading