|
| 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-instrument` |
| 21 | +
|
| 22 | +This script configures and sets up OpenTelemetry Python with the values we |
| 23 | +expect will be used by the common user. It does this by setting the environment |
| 24 | +variables OpenTelemetry uses, and then initializing OpenTelemetry using the |
| 25 | +`opentelemetry-instrument` auto instrumentation script from the |
| 26 | +`opentelemetry-instrumentation` package. |
| 27 | +
|
| 28 | +Additionally, this configuration assumes the user is using packages conforming |
| 29 | +to the `opentelemetry-instrumentation` and `opentelemetry-sdk` specifications. |
| 30 | +
|
| 31 | +DO NOT use this script for anything else besides SETTING ENVIRONMENT VARIABLES. |
| 32 | +
|
| 33 | +See more: |
| 34 | +https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper |
| 35 | +
|
| 36 | +Usage |
| 37 | +----- |
| 38 | +We expect this file to be at the root of a Lambda Layer. Having it anywhere else |
| 39 | +seems to mean AWS Lambda cannot find it. |
| 40 | +
|
| 41 | +In the configuration of an AWS Lambda function with this file at the |
| 42 | +root level of a Lambda Layer: |
| 43 | +
|
| 44 | +.. code:: |
| 45 | +
|
| 46 | + AWS_LAMBDA_EXEC_WRAPPER = /opt/otel-instrument |
| 47 | +
|
| 48 | +END_DOCUMENTATION |
| 49 | + |
| 50 | +# Use constants to access the environment variables we want to use in this |
| 51 | +# script. |
| 52 | + |
| 53 | +# See more: |
| 54 | +# https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime |
| 55 | + |
| 56 | +# - Reserved environment variables |
| 57 | + |
| 58 | +# - - $AWS_LAMBDA_FUNCTION_NAME |
| 59 | +# - - $LAMBDA_RUNTIME_DIR |
| 60 | + |
| 61 | +# - Unreserved environment variables |
| 62 | + |
| 63 | +# - - $PYTHONPATH |
| 64 | + |
| 65 | +# Update the python paths for packages with `sys.path` and `PYTHONPATH` |
| 66 | + |
| 67 | +# - We know that the path to the Lambda Layer OpenTelemetry Python packages are |
| 68 | +# well defined, so we can add them to the PYTHONPATH. |
| 69 | +# |
| 70 | +# See more: |
| 71 | +# https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path |
| 72 | + |
| 73 | +export LAMBDA_LAYER_PKGS_DIR="/opt/python"; |
| 74 | + |
| 75 | +# - Set Lambda Layer python packages in PYTHONPATH so `opentelemetry-instrument` |
| 76 | +# script can find them (it needs to find `opentelemetry` to find the auto |
| 77 | +# instrumentation `run()` method later) |
| 78 | + |
| 79 | +export PYTHONPATH="$LAMBDA_LAYER_PKGS_DIR:$PYTHONPATH"; |
| 80 | + |
| 81 | +# - Set Lambda runtime python packages in PYTHONPATH so |
| 82 | +# `opentelemetry-instrument` script can find them during auto instrumentation |
| 83 | +# and instrument them. |
| 84 | + |
| 85 | +export PYTHONPATH="$LAMBDA_RUNTIME_DIR:$PYTHONPATH"; |
| 86 | + |
| 87 | +# Configure OpenTelemetry Python with environment variables |
| 88 | + |
| 89 | +# - We leave `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` to its default. This is |
| 90 | +# `http://localhost:4318/v1/traces` because we are using the HTTP exporter |
| 91 | + |
| 92 | +# - If OTEL_EXPORTER_OTLP_PROTOCOL is not set by user, the default exporting protocol is http/protobuf. |
| 93 | +if [ -z "${OTEL_EXPORTER_OTLP_PROTOCOL}" ]; then |
| 94 | + export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf |
| 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 propagators |
| 104 | + |
| 105 | +if [[ -z "$OTEL_PROPAGATORS" ]]; then |
| 106 | + export OTEL_PROPAGATORS="tracecontext,baggage,xray" |
| 107 | +fi |
| 108 | + |
| 109 | +export LAMBDA_RESOURCE_ATTRIBUTES="cloud.region=$AWS_REGION,cloud.provider=aws,faas.name=$AWS_LAMBDA_FUNCTION_NAME,faas.version=$AWS_LAMBDA_FUNCTION_VERSION,faas.instance=$AWS_LAMBDA_LOG_STREAM_NAME,aws.log.group.names=$AWS_LAMBDA_LOG_GROUP_NAME"; |
| 110 | + |
| 111 | +# - If Application Signals is enabled |
| 112 | + |
| 113 | +if [ "${OTEL_AWS_APPLICATION_SIGNALS_ENABLED}" = "true" ]; then |
| 114 | + export OTEL_PYTHON_DISTRO="aws_distro"; |
| 115 | + export OTEL_PYTHON_CONFIGURATOR="aws_configurator"; |
| 116 | + export OTEL_METRICS_EXPORTER="none"; |
| 117 | + export OTEL_LOGS_EXPORTER="none"; |
| 118 | +fi |
| 119 | + |
| 120 | +if [ -z "${OTEL_RESOURCE_ATTRIBUTES}" ]; then |
| 121 | + export OTEL_RESOURCE_ATTRIBUTES=$LAMBDA_RESOURCE_ATTRIBUTES; |
| 122 | +else |
| 123 | + export OTEL_RESOURCE_ATTRIBUTES="$LAMBDA_RESOURCE_ATTRIBUTES,$OTEL_RESOURCE_ATTRIBUTES"; |
| 124 | +fi |
| 125 | + |
| 126 | +# - Enable botocore instrumentation by default |
| 127 | + |
| 128 | +if [ -z ${OTEL_PYTHON_DISABLED_INSTRUMENTATIONS} ]; then |
| 129 | + export OTEL_PYTHON_DISABLED_INSTRUMENTATIONS="aio-pika,aiohttp-client,aiopg,asgi,asyncpg,boto3sqs,boto,cassandra,celery,confluent-kafka,dbapi,django,elasticsearch,fastapi,falcon,flask,grpc,httpx,jinja2,kafka-python,logging,mysql,mysqlclient,pika,psycopg2,pymemcache,pymongo,pymysql,pyramid,redis,remoulade,requests,sklearn,sqlalchemy,sqlite3,starlette,system-metrics,tornado,tortoiseorm,urllib,urllib3,wsgi" |
| 130 | +fi |
| 131 | + |
| 132 | +# - Use a wrapper because AWS Lambda's `python3 /var/runtime/bootstrap.py` will |
| 133 | +# use `imp.load_module` to load the function from the `_HANDLER` environment |
| 134 | +# variable. This RELOADS the module and REMOVES any instrumentation patching |
| 135 | +# done earlier. So we delay instrumentation until `bootstrap.py` imports |
| 136 | +# `otel_wrapper.py` at which we know the patching will be picked up. |
| 137 | +# |
| 138 | +# See more: |
| 139 | +# https://docs.python.org/3/library/imp.html#imp.load_module |
| 140 | + |
| 141 | +export ORIG_HANDLER=$_HANDLER; |
| 142 | +export _HANDLER="otel_wrapper.lambda_handler"; |
| 143 | + |
| 144 | +# - Call the upstream auto instrumentation script |
| 145 | + |
| 146 | +exec python3 $LAMBDA_LAYER_PKGS_DIR/bin/opentelemetry-instrument "$@" |
0 commit comments