|
1 |
| -#!/usr/bin/env python3 |
2 |
| -# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
3 |
| -# SPDX-License-Identifier: MIT-0 |
4 |
| - |
5 |
| -from os import environ, system |
6 |
| -import sys |
7 |
| - |
8 |
| -# the path to the interpreter and all of the originally intended arguments |
9 |
| -args = sys.argv[1:] |
10 |
| - |
11 |
| -# enable OTel wrapper |
12 |
| -environ["ORIG_HANDLER"] = environ.get("_HANDLER") |
13 |
| -environ["_HANDLER"] = "otel_wrapper.lambda_handler" |
14 |
| - |
15 |
| -# config default traces exporter if missing |
16 |
| -environ.setdefault("OTEL_TRACES_EXPORTER", "otlp_proto_grpc_span") |
17 |
| - |
18 |
| -# set service name |
19 |
| -if environ.get("OTEL_RESOURCE_ATTRIBUTES") is None: |
20 |
| - environ["OTEL_RESOURCE_ATTRIBUTES"] = "service.name=%s" % ( |
21 |
| - environ.get("AWS_LAMBDA_FUNCTION_NAME") |
22 |
| - ) |
23 |
| -elif "service.name=" not in environ.get("OTEL_RESOURCE_ATTRIBUTES"): |
24 |
| - environ["OTEL_RESOURCE_ATTRIBUTES"] = "service.name=%s,%s" % ( |
25 |
| - environ.get("AWS_LAMBDA_FUNCTION_NAME"), |
26 |
| - environ.get("OTEL_RESOURCE_ATTRIBUTES"), |
27 |
| - ) |
28 |
| - |
29 |
| -# TODO: Remove if sdk support resource detector env variable configuration. |
30 |
| -lambda_resource_attributes = ( |
31 |
| - "cloud.region=%s,cloud.provider=aws,faas.name=%s,faas.version=%s" |
32 |
| - % ( |
33 |
| - environ.get("AWS_REGION"), |
34 |
| - environ.get("AWS_LAMBDA_FUNCTION_NAME"), |
35 |
| - environ.get("AWS_LAMBDA_FUNCTION_VERSION"), |
36 |
| - ) |
37 |
| -) |
38 |
| -environ["OTEL_RESOURCE_ATTRIBUTES"] = "%s,%s" % ( |
39 |
| - lambda_resource_attributes, |
40 |
| - environ.get("OTEL_RESOURCE_ATTRIBUTES"), |
41 |
| -) |
42 |
| - |
43 |
| -# start the runtime with the extra options |
44 |
| -system(" ".join(args)) |
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright The OpenTelemetry Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +: ' |
| 18 | +`otel-instrument` |
| 19 | +
|
| 20 | +This script configures and sets up OpenTelemetry Python with the values we |
| 21 | +expect will be used by the common user. It does this by setting the environment |
| 22 | +variables OpenTelemetry uses, and then initializing OpenTelemetry using the |
| 23 | +`opentelemetry-instrument` auto instrumentation script from the |
| 24 | +`opentelemetry-instrumentation` package. |
| 25 | +
|
| 26 | +Additionally, this configuration assumes the user is using packages conforming |
| 27 | +to the `opentelemetry-instrumentation` and `opentelemetry-sdk` specifications. |
| 28 | +
|
| 29 | +DO NOT use this script for anything else besides SETTING ENVIRONMENT VARIABLES. |
| 30 | +
|
| 31 | +See more: |
| 32 | +https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper |
| 33 | +
|
| 34 | +Usage |
| 35 | +----- |
| 36 | +We expect this file to be at the root of a Lambda Layer. Having it anywhere else |
| 37 | +seems to mean AWS Lambda cannot find it. |
| 38 | +
|
| 39 | +In the configuration of an AWS Lambda function with this file at the |
| 40 | +root level of a Lambda Layer: |
| 41 | +
|
| 42 | +.. code:: |
| 43 | +
|
| 44 | + AWS_LAMBDA_EXEC_WRAPPER = /opt/otel-instrument |
| 45 | +
|
| 46 | +' |
| 47 | + |
| 48 | +# Use constants to access the environment variables we want to use in this |
| 49 | +# script. |
| 50 | + |
| 51 | +# See more: |
| 52 | +# https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime |
| 53 | + |
| 54 | +# - Reserved environment variables |
| 55 | + |
| 56 | +# - - $AWS_LAMBDA_FUNCTION_NAME |
| 57 | +# - - $LAMBDA_RUNTIME_DIR |
| 58 | + |
| 59 | +# - Unreserved environment variables |
| 60 | + |
| 61 | +# - - $PYTHONPATH |
| 62 | + |
| 63 | +# Update the python paths for packages with `sys.path` and `PYTHONPATH` |
| 64 | + |
| 65 | +# - We know that the path to the Lambda Layer OpenTelemetry Python packages are |
| 66 | +# well defined, so we can add them to the PYTHONPATH. |
| 67 | +# |
| 68 | +# See more: |
| 69 | +# https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path |
| 70 | + |
| 71 | +export LAMBDA_LAYER_PKGS_DIR="/opt/python" |
| 72 | + |
| 73 | +# - Set Lambda Layer python packages in PYTHONPATH so `opentelemetry-instrument` |
| 74 | +# script can find them (it needs to find `opentelemetry` to find the auto |
| 75 | +# instrumentation `run()` method later) |
| 76 | + |
| 77 | +if [ -z ${PYTHONPATH} ]; then |
| 78 | + export PYTHONPATH=$LAMBDA_LAYER_PKGS_DIR; |
| 79 | +else |
| 80 | + export PYTHONPATH="$LAMBDA_LAYER_PKGS_DIR:$PYTHONPATH"; |
| 81 | +fi |
| 82 | + |
| 83 | +# - Set Lambda runtime python packages in PYTHONPATH so |
| 84 | +# `opentelemetry-instrument` script can find them during auto instrumentation |
| 85 | +# and instrument them. |
| 86 | + |
| 87 | +export PYTHONPATH="$LAMBDA_RUNTIME_DIR:$PYTHONPATH"; |
| 88 | + |
| 89 | +# Configure OpenTelemetry Python with environment variables |
| 90 | + |
| 91 | +# - Set the default Trace Exporter |
| 92 | + |
| 93 | +if [ -z ${OTEL_TRACES_EXPORTER} ]; then |
| 94 | + export OTEL_TRACES_EXPORTER="otlp_proto_grpc_span"; |
| 95 | +fi |
| 96 | + |
| 97 | +# - Set the service name |
| 98 | + |
| 99 | +if [ -z ${OTEL_SERVICE_NAME} ]; then |
| 100 | + export OTEL_SERVICE_NAME=$AWS_LAMBDA_FUNCTION_NAME; |
| 101 | +fi |
| 102 | + |
| 103 | +# - Set the Resource Detectors (Resource Attributes) |
| 104 | +# |
| 105 | +# TODO: waiting on OTel Python support for configuring Resource Detectors from |
| 106 | +# an environment variable. Replace the bottom code with the following when |
| 107 | +# this is possible. |
| 108 | +# |
| 109 | +# export OTEL_RESOURCE_DETECTORS="aws_lambda"; |
| 110 | +# |
| 111 | +export LAMBDA_RESOURCE_ATTRIBUTES="cloud.region=$AWS_REGION,cloud.provider=aws,faas.name=$AWS_LAMBDA_FUNCTION_NAME,faas.version=$AWS_LAMBDA_FUNCTION_VERSION" |
| 112 | + |
| 113 | +if [ -z ${OTEL_RESOURCE_ATTRIBUTES} ]; then |
| 114 | + export OTEL_RESOURCE_ATTRIBUTES=$LAMBDA_RESOURCE_ATTRIBUTES; |
| 115 | +else |
| 116 | + export OTEL_RESOURCE_ATTRIBUTES="$LAMBDA_RESOURCE_ATTRIBUTES,$OTEL_RESOURCE_ATTRIBUTES"; |
| 117 | +fi |
| 118 | + |
| 119 | +# - Set the default propagators |
| 120 | + |
| 121 | +if [ -z ${OTEL_PROPAGATORS} ]; then |
| 122 | + export OTEL_PROPAGATORS="tracecontext,b3,xray"; |
| 123 | +fi |
| 124 | + |
| 125 | +# - Use a wrapper because AWS Lambda's `python3 /var/runtime/bootstrap.py` will |
| 126 | +# use `imp.load_module` to load the function from the `_HANDLER` environment |
| 127 | +# variable. This RELOADS the module and REMOVES any instrumentation patching |
| 128 | +# done earlier. So we delay instrumentation until `boostrap.py` imports |
| 129 | +# `otel_wrapper.py` at which we know the patching will be picked up. |
| 130 | +# |
| 131 | +# See more: |
| 132 | +# https://docs.python.org/3/library/imp.html#imp.load_module |
| 133 | + |
| 134 | +export ORIG_HANDLER=$_HANDLER; |
| 135 | +export _HANDLER="otel_wrapper.lambda_handler"; |
| 136 | + |
| 137 | +# - Call the upstream auto instrumentation script |
| 138 | + |
| 139 | +python3 $LAMBDA_LAYER_PKGS_DIR/bin/opentelemetry-instrument "$@" |
| 140 | + |
| 141 | + |
0 commit comments