From e859a12b221e98ee3ec9d737f436955b8e4fd2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 28 May 2025 16:47:40 -0400 Subject: [PATCH 1/6] create `otel-handler` script --- python/src/otel/otel_sdk/otel-handler | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 python/src/otel/otel_sdk/otel-handler diff --git a/python/src/otel/otel_sdk/otel-handler b/python/src/otel/otel_sdk/otel-handler new file mode 100644 index 0000000000..364fc27af1 --- /dev/null +++ b/python/src/otel/otel_sdk/otel-handler @@ -0,0 +1,41 @@ +#!/bin/bash + +set -ef -o pipefail + +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +: <<'END_DOCUMENTATION' +`otel-handler` + +This script is a wrapper around the existing `otel-instrument` script to maintain +backward compatibility while providing a more descriptive name. + +It simply forwards all arguments to the `otel-instrument` script. + +Usage +----- +Use this script exactly as you would use `otel-instrument`: + +.. code:: + + AWS_LAMBDA_EXEC_WRAPPER = /opt/otel-handler + +END_DOCUMENTATION + +# Get the directory where this script is located +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# Call the original otel-instrument script with all passed arguments +exec "${SCRIPT_DIR}/otel-instrument" "$@" From b922cffa572329efb2282db7685528ca4ea42203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 28 May 2025 16:47:47 -0400 Subject: [PATCH 2/6] add docs to mention `otel-handler` in `otel-instrument` --- python/src/otel/otel_sdk/otel-instrument | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/src/otel/otel_sdk/otel-instrument b/python/src/otel/otel_sdk/otel-instrument index 47c35d2da6..9d89c96b62 100755 --- a/python/src/otel/otel_sdk/otel-instrument +++ b/python/src/otel/otel_sdk/otel-instrument @@ -25,6 +25,10 @@ variables OpenTelemetry uses, and then initializing OpenTelemetry using the `opentelemetry-instrument` auto instrumentation script from the `opentelemetry-instrumentation` package. +All other runtimes use the `otel-handler` script which is the equivalent of this. +In the next major version, the contents of this script will be moved to the +`otel-handler` script. + Additionally, this configuration assumes the user is using packages conforming to the `opentelemetry-instrumentation` and `opentelemetry-sdk` specifications. From 5a6298d6a9793bda823a106aef4970488b5e0f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 28 May 2025 16:47:54 -0400 Subject: [PATCH 3/6] include `otel-handler` in layer build --- python/src/otel/Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/src/otel/Dockerfile b/python/src/otel/Dockerfile index b4bf1d2358..0480e1ce29 100644 --- a/python/src/otel/Dockerfile +++ b/python/src/otel/Dockerfile @@ -15,10 +15,12 @@ RUN mkdir -p /build && \ rm -rf /build/tmp && \ mv otel_sdk/otel_wrapper.py /build/python && \ mv otel_sdk/otel-instrument /build && \ + mv otel_sdk/otel-handler /build && \ chmod 755 /build/otel-instrument && \ + chmod 755 /build/otel-handler && \ rm -rf /build/python/boto* && \ rm -rf /build/python/urllib3* && \ cd /build && \ - zip -r opentelemetry-python-layer.zip otel-instrument python + zip -r opentelemetry-python-layer.zip otel-handler otel-instrument python CMD cp /build/opentelemetry-python-layer.zip /out/opentelemetry-python-layer.zip From d47bc4dc1f9dcb11d0589acd23996388524f9b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 4 Jun 2025 14:07:49 -0400 Subject: [PATCH 4/6] update `sample-apps` --- python/sample-apps/aws-sdk/deploy/wrapper/main.tf | 2 +- python/sample-apps/template.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/sample-apps/aws-sdk/deploy/wrapper/main.tf b/python/sample-apps/aws-sdk/deploy/wrapper/main.tf index 0470841f8a..47a2097456 100644 --- a/python/sample-apps/aws-sdk/deploy/wrapper/main.tf +++ b/python/sample-apps/aws-sdk/deploy/wrapper/main.tf @@ -19,7 +19,7 @@ module "hello-lambda-function" { ]) environment_variables = { - AWS_LAMBDA_EXEC_WRAPPER = "/opt/otel-instrument" + AWS_LAMBDA_EXEC_WRAPPER = "/opt/otel-handler" } tracing_mode = var.tracing_mode diff --git a/python/sample-apps/template.yml b/python/sample-apps/template.yml index 0ddcb2ee05..e4c6007689 100644 --- a/python/sample-apps/template.yml +++ b/python/sample-apps/template.yml @@ -24,7 +24,7 @@ Resources: - AmazonS3FullAccess Environment: Variables: - AWS_LAMBDA_EXEC_WRAPPER: /opt/python/otel-instrument + AWS_LAMBDA_EXEC_WRAPPER: /opt/python/otel-handler Tracing: Active Layers: - !Ref OTelLayer From d43be42ddf2b4d6b6e1c247e438b3e1fd57cb65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 4 Jun 2025 14:08:00 -0400 Subject: [PATCH 5/6] update `Makefile` and unit tests removing references to the old handler should be ok, because the new one should be a wrapper which just acts upon the old one --- python/src/otel/Makefile | 1 + python/src/otel/tests/test_otel.py | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/python/src/otel/Makefile b/python/src/otel/Makefile index cb97199814..bdb34e793e 100644 --- a/python/src/otel/Makefile +++ b/python/src/otel/Makefile @@ -9,5 +9,6 @@ build-OTelLayer: rm -rf $(ARTIFACTS_DIR)/tmp cp -r $(SDK)/* $(ARTIFACTS_DIR)/python chmod 755 $(ARTIFACTS_DIR)/python/otel-instrument + chmod 755 $(ARTIFACTS_DIR)/python/otel-handler rm -rf $(ARTIFACTS_DIR)/python/boto* rm -rf $(ARTIFACTS_DIR)/python/urllib3* diff --git a/python/src/otel/tests/test_otel.py b/python/src/otel/tests/test_otel.py index a0c411b144..27b2ec9ba5 100644 --- a/python/src/otel/tests/test_otel.py +++ b/python/src/otel/tests/test_otel.py @@ -13,7 +13,7 @@ # limitations under the License. """ -This file tests that the `otel-instrument` script included in this repository +This file tests that the `otel-handler` script included in this repository successfully instruments OTel Python in a mock Lambda environment. """ @@ -94,7 +94,7 @@ def replace_in_file(filename, old_text, new_text): def mock_aws_lambda_exec_wrapper(): """Mocks automatically instrumenting user Lambda function by pointing - `AWS_LAMBDA_EXEC_WRAPPER` to the `otel-instrument` script. + `AWS_LAMBDA_EXEC_WRAPPER` to the `otel-handler` script. TODO: It would be better if `moto`'s `mock_lambda` supported setting AWS_LAMBDA_EXEC_WRAPPER so we could make the call to Lambda instead. @@ -107,7 +107,7 @@ def mock_aws_lambda_exec_wrapper(): # with instrumentation. In this test we just make sure we can complete auto # instrumentation without error and the correct environment variabels are # set. A future improvement might have us run `opentelemetry-instrument` in - # this process to imitate `otel-instrument`, but our lambda handler does not + # this process to imitate `otel-handler`, but our lambda handler does not # call other instrumented libraries so we have no use for it for now. print_environ_program = ( @@ -118,7 +118,7 @@ def mock_aws_lambda_exec_wrapper(): completed_subprocess = subprocess.run( [ - os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-instrument"), + os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-handler"), "python3", "-c", print_environ_program, @@ -128,7 +128,7 @@ def mock_aws_lambda_exec_wrapper(): text=True, ) - # NOTE: Because `otel-instrument` cannot affect this python environment, we + # NOTE: Because `otel-handler` cannot affect this python environment, we # parse the stdout produced by our test python program to update the # environment in this parent python process. @@ -172,7 +172,7 @@ def setUpClass(cls): super().setUpClass() sys.path.append(INIT_OTEL_SCRIPTS_DIR) replace_in_file( - os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-instrument"), + os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-handler"), 'export LAMBDA_LAYER_PKGS_DIR="/opt/python"', f'export LAMBDA_LAYER_PKGS_DIR="{TOX_PYTHON_DIRECTORY}"', ) @@ -199,7 +199,7 @@ def tearDownClass(cls): super().tearDownClass() sys.path.remove(INIT_OTEL_SCRIPTS_DIR) replace_in_file( - os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-instrument"), + os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-handler"), f'export LAMBDA_LAYER_PKGS_DIR="{TOX_PYTHON_DIRECTORY}"', 'export LAMBDA_LAYER_PKGS_DIR="/opt/python"', ) From 1c236687554ca73e8d69ac4288af21bacdebb001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 4 Jun 2025 15:23:04 -0400 Subject: [PATCH 6/6] update permissions and unit tests unit tests actually required us to register both files so it worked as expected --- python/src/otel/otel_sdk/otel-handler | 0 python/src/otel/tests/test_otel.py | 10 ++++++++++ 2 files changed, 10 insertions(+) mode change 100644 => 100755 python/src/otel/otel_sdk/otel-handler diff --git a/python/src/otel/otel_sdk/otel-handler b/python/src/otel/otel_sdk/otel-handler old mode 100644 new mode 100755 diff --git a/python/src/otel/tests/test_otel.py b/python/src/otel/tests/test_otel.py index 27b2ec9ba5..c59c99015a 100644 --- a/python/src/otel/tests/test_otel.py +++ b/python/src/otel/tests/test_otel.py @@ -176,6 +176,11 @@ def setUpClass(cls): 'export LAMBDA_LAYER_PKGS_DIR="/opt/python"', f'export LAMBDA_LAYER_PKGS_DIR="{TOX_PYTHON_DIRECTORY}"', ) + replace_in_file( + os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-instrument"), + 'export LAMBDA_LAYER_PKGS_DIR="/opt/python"', + f'export LAMBDA_LAYER_PKGS_DIR="{TOX_PYTHON_DIRECTORY}"', + ) def setUp(self): super().setUp() @@ -203,6 +208,11 @@ def tearDownClass(cls): f'export LAMBDA_LAYER_PKGS_DIR="{TOX_PYTHON_DIRECTORY}"', 'export LAMBDA_LAYER_PKGS_DIR="/opt/python"', ) + replace_in_file( + os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-instrument"), + f'export LAMBDA_LAYER_PKGS_DIR="{TOX_PYTHON_DIRECTORY}"', + 'export LAMBDA_LAYER_PKGS_DIR="/opt/python"', + ) def test_active_tracing(self): test_env_patch = mock.patch.dict(