Skip to content

Commit 759d579

Browse files
authored
fix(python): add otel-handler wrapper around otel-instrument (#1837)
* create `otel-handler` script * add docs to mention `otel-handler` in `otel-instrument` * include `otel-handler` in layer build * update `sample-apps` * 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 * update permissions and unit tests unit tests actually required us to register both files so it worked as expected
1 parent 3a2ec97 commit 759d579

File tree

7 files changed

+66
-8
lines changed

7 files changed

+66
-8
lines changed

python/sample-apps/aws-sdk/deploy/wrapper/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module "hello-lambda-function" {
1919
])
2020

2121
environment_variables = {
22-
AWS_LAMBDA_EXEC_WRAPPER = "/opt/otel-instrument"
22+
AWS_LAMBDA_EXEC_WRAPPER = "/opt/otel-handler"
2323
}
2424

2525
tracing_mode = var.tracing_mode

python/sample-apps/template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Resources:
2424
- AmazonS3FullAccess
2525
Environment:
2626
Variables:
27-
AWS_LAMBDA_EXEC_WRAPPER: /opt/python/otel-instrument
27+
AWS_LAMBDA_EXEC_WRAPPER: /opt/python/otel-handler
2828
Tracing: Active
2929
Layers:
3030
- !Ref OTelLayer

python/src/otel/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ RUN mkdir -p /build && \
1515
rm -rf /build/tmp && \
1616
mv otel_sdk/otel_wrapper.py /build/python && \
1717
mv otel_sdk/otel-instrument /build && \
18+
mv otel_sdk/otel-handler /build && \
1819
chmod 755 /build/otel-instrument && \
20+
chmod 755 /build/otel-handler && \
1921
rm -rf /build/python/boto* && \
2022
rm -rf /build/python/urllib3* && \
2123
cd /build && \
22-
zip -r opentelemetry-python-layer.zip otel-instrument python
24+
zip -r opentelemetry-python-layer.zip otel-handler otel-instrument python
2325

2426
CMD cp /build/opentelemetry-python-layer.zip /out/opentelemetry-python-layer.zip

python/src/otel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ build-OTelLayer:
99
rm -rf $(ARTIFACTS_DIR)/tmp
1010
cp -r $(SDK)/* $(ARTIFACTS_DIR)/python
1111
chmod 755 $(ARTIFACTS_DIR)/python/otel-instrument
12+
chmod 755 $(ARTIFACTS_DIR)/python/otel-handler
1213
rm -rf $(ARTIFACTS_DIR)/python/boto*
1314
rm -rf $(ARTIFACTS_DIR)/python/urllib3*

python/src/otel/otel_sdk/otel-handler

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
set -ef -o pipefail
4+
5+
# Copyright The OpenTelemetry Authors
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
: <<'END_DOCUMENTATION'
20+
`otel-handler`
21+
22+
This script is a wrapper around the existing `otel-instrument` script to maintain
23+
backward compatibility while providing a more descriptive name.
24+
25+
It simply forwards all arguments to the `otel-instrument` script.
26+
27+
Usage
28+
-----
29+
Use this script exactly as you would use `otel-instrument`:
30+
31+
.. code::
32+
33+
AWS_LAMBDA_EXEC_WRAPPER = /opt/otel-handler
34+
35+
END_DOCUMENTATION
36+
37+
# Get the directory where this script is located
38+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
39+
40+
# Call the original otel-instrument script with all passed arguments
41+
exec "${SCRIPT_DIR}/otel-instrument" "$@"

python/src/otel/otel_sdk/otel-instrument

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ variables OpenTelemetry uses, and then initializing OpenTelemetry using the
2525
`opentelemetry-instrument` auto instrumentation script from the
2626
`opentelemetry-instrumentation` package.
2727
28+
All other runtimes use the `otel-handler` script which is the equivalent of this.
29+
In the next major version, the contents of this script will be moved to the
30+
`otel-handler` script.
31+
2832
Additionally, this configuration assumes the user is using packages conforming
2933
to the `opentelemetry-instrumentation` and `opentelemetry-sdk` specifications.
3034

python/src/otel/tests/test_otel.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
"""
16-
This file tests that the `otel-instrument` script included in this repository
16+
This file tests that the `otel-handler` script included in this repository
1717
successfully instruments OTel Python in a mock Lambda environment.
1818
"""
1919

@@ -94,7 +94,7 @@ def replace_in_file(filename, old_text, new_text):
9494

9595
def mock_aws_lambda_exec_wrapper():
9696
"""Mocks automatically instrumenting user Lambda function by pointing
97-
`AWS_LAMBDA_EXEC_WRAPPER` to the `otel-instrument` script.
97+
`AWS_LAMBDA_EXEC_WRAPPER` to the `otel-handler` script.
9898
9999
TODO: It would be better if `moto`'s `mock_lambda` supported setting
100100
AWS_LAMBDA_EXEC_WRAPPER so we could make the call to Lambda instead.
@@ -107,7 +107,7 @@ def mock_aws_lambda_exec_wrapper():
107107
# with instrumentation. In this test we just make sure we can complete auto
108108
# instrumentation without error and the correct environment variabels are
109109
# set. A future improvement might have us run `opentelemetry-instrument` in
110-
# this process to imitate `otel-instrument`, but our lambda handler does not
110+
# this process to imitate `otel-handler`, but our lambda handler does not
111111
# call other instrumented libraries so we have no use for it for now.
112112

113113
print_environ_program = (
@@ -118,7 +118,7 @@ def mock_aws_lambda_exec_wrapper():
118118

119119
completed_subprocess = subprocess.run(
120120
[
121-
os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-instrument"),
121+
os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-handler"),
122122
"python3",
123123
"-c",
124124
print_environ_program,
@@ -128,7 +128,7 @@ def mock_aws_lambda_exec_wrapper():
128128
text=True,
129129
)
130130

131-
# NOTE: Because `otel-instrument` cannot affect this python environment, we
131+
# NOTE: Because `otel-handler` cannot affect this python environment, we
132132
# parse the stdout produced by our test python program to update the
133133
# environment in this parent python process.
134134

@@ -171,6 +171,11 @@ class TestAwsLambdaInstrumentor(TestBase):
171171
def setUpClass(cls):
172172
super().setUpClass()
173173
sys.path.append(INIT_OTEL_SCRIPTS_DIR)
174+
replace_in_file(
175+
os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-handler"),
176+
'export LAMBDA_LAYER_PKGS_DIR="/opt/python"',
177+
f'export LAMBDA_LAYER_PKGS_DIR="{TOX_PYTHON_DIRECTORY}"',
178+
)
174179
replace_in_file(
175180
os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-instrument"),
176181
'export LAMBDA_LAYER_PKGS_DIR="/opt/python"',
@@ -198,6 +203,11 @@ def tearDown(self):
198203
def tearDownClass(cls):
199204
super().tearDownClass()
200205
sys.path.remove(INIT_OTEL_SCRIPTS_DIR)
206+
replace_in_file(
207+
os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-handler"),
208+
f'export LAMBDA_LAYER_PKGS_DIR="{TOX_PYTHON_DIRECTORY}"',
209+
'export LAMBDA_LAYER_PKGS_DIR="/opt/python"',
210+
)
201211
replace_in_file(
202212
os.path.join(INIT_OTEL_SCRIPTS_DIR, "otel-instrument"),
203213
f'export LAMBDA_LAYER_PKGS_DIR="{TOX_PYTHON_DIRECTORY}"',

0 commit comments

Comments
 (0)