Skip to content

Commit

Permalink
Merge branch 'main' into NH-65068-is-lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi authored Nov 6, 2023
2 parents 6d5af6e + 64c0cd5 commit dbbd2d9
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Change c-lib usage and `agent_enabled` calculation if `is_lambda` ([#211](https://github.com/solarwindscloud/solarwinds-apm-python/pull/211))
- Updated Makefile for APM Python lambda builds ([#212](https://github.com/solarwindscloud/solarwinds-apm-python/pull/212))

## [0.18.0](https://github.com/solarwindscloud/solarwinds-apm-python/releases/tag/rel-0.18.0) - 2023-10-31

Expand Down
5 changes: 3 additions & 2 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
* @solarwindscloud/eng-apm-instrumentation

# placeholder for GH EMU where this will move to new org for public repos
#* @solarwinds/eng-apm-instrumentation
# prepare for move to solarwinds
* @solarwinds/eng-pub-apm-instrumentation

43 changes: 29 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ nothing:
#----------------------------------------------------------------------------------------------------------------------#

# Platform for wheel tagging: x86_64 or aarch64
platform := ${PLATFORM}
platform := ${shell uname -p}
ifeq (${platform},aarch64)
wheel_tag := manylinux_2_28_aarch64
else
Expand Down Expand Up @@ -127,7 +127,7 @@ download-bson-headers:
download-all: download-headers download-liboboe

#----------------------------------------------------------------------------------------------------------------------#
# check if SWIG is installed
# check if build deps are installed
#----------------------------------------------------------------------------------------------------------------------#

check-swig:
Expand All @@ -136,6 +136,12 @@ check-swig:
{ echo >&2 "Swig is required to build the distribution. Aborting."; exit 1;}
@echo -e "Yes."

check-zip:
@echo -e "Is zip installed?"
@command -v zip >/dev/null 2>&1 || \
{ echo >&2 "zip is required to build lambda layer. Installing."; dnf install zip -y;}
@echo -e "Yes."

#----------------------------------------------------------------------------------------------------------------------#
# recipes for building the package distribution
#----------------------------------------------------------------------------------------------------------------------#
Expand Down Expand Up @@ -176,30 +182,39 @@ check-wheel-local:
# Build and check the full Python agent distribution (sdist and wheels)
package: sdist check-sdist-local manylinux-wheels check-wheel-local

# Build the AWS lambda layer.
# Build the AWS lambda layer locally as zip file.
# temporary target directory for AWS Lambda build artifacts
# TODO cp39 and cp310
target_dir := "./tmp/python"
aws-lambda: wrapper
target_dir := "./tmp"
aws-lambda: check-zip wrapper
@if [ -f ./dist/solarwinds_apm_lambda.zip ]; then \
echo -e "Deleting old solarwinds_apm_lambda.zip"; \
rm ./dist/solarwinds_apm_lambda.zip; \
fi
rm -rf ./tmp
@echo -e "Creating target directory ${target_dir} for AWS Lambda layer artifacts."
mkdir -p ${target_dir}
mkdir -p ${target_dir}/python
@echo -e "Install upstream dependencies to include in layer"
@/opt/python/cp38-cp38/bin/pip3.8 install -t ${target_dir}/python -r lambda/requirements.txt
@echo -e "Install upstream dependencies without deps to include in layer"
@/opt/python/cp38-cp38/bin/pip3.8 install -t ${target_dir}/nodeps -r lambda/requirements-nodeps.txt --no-deps
@echo -e "Install solarwinds_apm to be packed up in zip archive to target directory."
@/opt/python/cp38-cp38/bin/pip3.8 install . -t ${target_dir}
@/opt/python/cp38-cp38/bin/pip3.8 install . -t ${target_dir}/nodeps --no-deps
@echo -e "Removing non-lambda C-extension library files generated by pip install under target directory."
@rm ${target_dir}/solarwinds_apm/extension/*.so*
@rm ${target_dir}/nodeps/solarwinds_apm/extension/*.so*
@echo -e "Building AWS Lambda version of C-extensions for all supported Python versions in target directory."
@set -e; for PYBIN in cp37-cp37m cp38-cp38; do /opt/python/$${PYBIN}/bin/python setup.py build_ext_lambda -b ${target_dir}; done
@set -e; for PYBIN in cp37-cp37m cp38-cp38; do /opt/python/$${PYBIN}/bin/python setup.py build_ext_lambda -b ${target_dir}/nodeps; done
@echo -e "Copying AWS Lambda specific Oboe library liboboe-1.0-lambda-${platform}.so into target directory."
@cp solarwinds_apm/extension/liboboe-1.0-lambda-${platform}.so ${target_dir}/solarwinds_apm/extension/liboboe.so
@rm -rf ${target_dir}/*-info
@find ${target_dir} -type d -name '__pycache__' | xargs rm -rf
@cp solarwinds_apm/extension/liboboe-1.0-lambda-${platform}.so ${target_dir}/nodeps/solarwinds_apm/extension/liboboe.so
@echo -e "Moving no-deps dependencies, needed for full opentelemetry/instrumentation path"
@cp -r ${target_dir}/nodeps/* ${target_dir}/python && rm -rf ${target_dir}/nodeps
@echo -e "Copying OpenTelemetry lambda wrapper and entry script into target directory."
@cp lambda/otel_wrapper.py ${target_dir}/python/otel_wrapper.py
@cp lambda/otel-instrument ${target_dir}/otel-instrument
@chmod 755 ${target_dir}/otel-instrument
@find ${target_dir}/python -type d -name '__pycache__' | xargs rm -rf
@if [[ ! -d dist ]]; then mkdir dist; fi
@pushd ./tmp && zip -r ../dist/solarwinds_apm_lambda.zip ./python && popd
@pushd ./tmp && zip -r ../dist/solarwinds_apm_lambda.zip . && popd
@rm -rf ./tmp ./build
@echo -e "\nDone."

Expand Down Expand Up @@ -301,4 +316,4 @@ clean-tox:
@rm -rf .tox/
@echo -e "Done."

.PHONY: nothing verify-oboe-version download-liboboe download-headers download-bson-headers download-all check-swig wrapper sdist manylinux-wheels package aws-lambda publish-lambda-layer-rc copy-liboboe copy-headers copy-bson-headers copy-all wrapper-from-local tox format lint clean
.PHONY: nothing verify-oboe-version download-liboboe download-headers download-bson-headers download-all check-swig check-zip wrapper sdist manylinux-wheels package aws-lambda publish-lambda-layer-rc copy-liboboe copy-headers copy-bson-headers copy-all wrapper-from-local tox format lint clean
154 changes: 154 additions & 0 deletions lambda/otel-instrument
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/bin/bash

# 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-instrument`
This script configures and sets up OpenTelemetry Python with the values we
expect will be used by the common user. It does this by setting the environment
variables OpenTelemetry uses, and then initializing OpenTelemetry using the
`opentelemetry-instrument` auto instrumentation script from the
`opentelemetry-instrumentation` package.
Additionally, this configuration assumes the user is using packages conforming
to the `opentelemetry-instrumentation` and `opentelemetry-sdk` specifications.
DO NOT use this script for anything else besides SETTING ENVIRONMENT VARIABLES.
See more:
https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper
Usage
-----
We expect this file to be at the root of a Lambda Layer. Having it anywhere else
seems to mean AWS Lambda cannot find it.
In the configuration of an AWS Lambda function with this file at the
root level of a Lambda Layer:
.. code::
AWS_LAMBDA_EXEC_WRAPPER = /opt/otel-instrument
END_DOCUMENTATION

# Use constants to access the environment variables we want to use in this
# script.

# See more:
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime

# - Reserved environment variables

# - - $AWS_LAMBDA_FUNCTION_NAME
# - - $LAMBDA_RUNTIME_DIR

# - Unreserved environment variables

# - - $PYTHONPATH

# Update the python paths for packages with `sys.path` and `PYTHONPATH`

# - We know that the path to the Lambda Layer OpenTelemetry Python packages are
# well defined, so we can add them to the PYTHONPATH.
#
# See more:
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path

export LAMBDA_LAYER_PKGS_DIR="/opt/python";

# - Set Lambda Layer python packages in PYTHONPATH so `opentelemetry-instrument`
# script can find them (it needs to find `opentelemetry` to find the auto
# instrumentation `run()` method later)

export PYTHONPATH="$LAMBDA_LAYER_PKGS_DIR:$PYTHONPATH";

# - Set Lambda runtime python packages in PYTHONPATH so
# `opentelemetry-instrument` script can find them during auto instrumentation
# and instrument them.

export PYTHONPATH="$LAMBDA_RUNTIME_DIR:$PYTHONPATH";

# Configure OpenTelemetry Python with environment variables

# - We leave `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` to its default. This is
# `http://localhost:4318/v1/traces` because we are using the HTTP exporter

# - If OTEL_EXPORTER_OTLP_PROTOCOL is not set by user, the default exporting protocol is http/protobuf.
if [ -z "${OTEL_EXPORTER_OTLP_PROTOCOL}" ]; then
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
fi

# - Set the service name

if [ -z "${OTEL_SERVICE_NAME}" ]; then
export OTEL_SERVICE_NAME=$AWS_LAMBDA_FUNCTION_NAME;
fi

# - Set the Resource Detectors (Resource Attributes)
#
# TODO: waiting on OTel Python support for configuring Resource Detectors from
# an environment variable. Replace the bottom code with the following when
# this is possible. We will have to download `opentelemetry-sdk-extension-aws`
# when that happens to get the resource detector.
#
# export OTEL_RESOURCE_DETECTORS="aws_lambda";
#
# In the meantime, if doing manual instrumentation, (which means we CANNOT use
# this script) we could do the following:
#
# import opentelemetry.trace as trace
# from opentelemetry.sdk.trace import TracerProvider
# from opentelemetry.sdk.extension.aws.resource._lambda import (
# AwsLambdaResourceDetector,
# )
# from opentelemetry.sdk.resources import get_aggregated_resources
# trace.set_tracer_provider(
# TracerProvider(
# resource=get_aggregated_resources(
# [
# AwsLambdaResourceDetector(),
# ]
# ),
# )
# )

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";

if [ -z "${OTEL_RESOURCE_ATTRIBUTES}" ]; then
export OTEL_RESOURCE_ATTRIBUTES=$LAMBDA_RESOURCE_ATTRIBUTES;
else
export OTEL_RESOURCE_ATTRIBUTES="$LAMBDA_RESOURCE_ATTRIBUTES,$OTEL_RESOURCE_ATTRIBUTES";
fi


# - Uses the default `OTEL_PROPAGATORS` which is set to `tracecontext,baggage`

# - Use a wrapper because AWS Lambda's `python3 /var/runtime/bootstrap.py` will
# use `imp.load_module` to load the function from the `_HANDLER` environment
# variable. This RELOADS the module and REMOVES any instrumentation patching
# done earlier. So we delay instrumentation until `bootstrap.py` imports
# `otel_wrapper.py` at which we know the patching will be picked up.
#
# See more:
# https://docs.python.org/3/library/imp.html#imp.load_module

export ORIG_HANDLER=$_HANDLER;
export _HANDLER="otel_wrapper.lambda_handler";

# - Call the upstream auto instrumentation script

exec python3 $LAMBDA_LAYER_PKGS_DIR/bin/opentelemetry-instrument "$@"
66 changes: 66 additions & 0 deletions lambda/otel_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 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.

"""
`otel_wrapper.py`
This file serves as a wrapper over the user's Lambda function.
Usage
-----
Patch the reserved `_HANDLER` Lambda environment variable to point to this
file's `otel_wrapper.lambda_handler` property. Do this having saved the original
`_HANDLER` in the `ORIG_HANDLER` environment variable. Doing this makes it so
that **on import of this file, the handler is instrumented**.
Instrumenting any earlier will cause the instrumentation to be lost because the
AWS Service uses `imp.load_module` to import the handler which RELOADS the
module. This is why AwsLambdaInstrumentor cannot be instrumented with the
`opentelemetry-instrument` script.
See more:
https://docs.python.org/3/library/imp.html#imp.load_module
"""

import os
from importlib import import_module

from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor


def modify_module_name(module_name):
"""Returns a valid modified module to get imported"""
return ".".join(module_name.split("/"))


class HandlerError(Exception):
pass


AwsLambdaInstrumentor().instrument()

path = os.environ.get("ORIG_HANDLER")

if path is None:
raise HandlerError("ORIG_HANDLER is not defined.")

try:
(mod_name, handler_name) = path.rsplit(".", 1)
except ValueError as e:
raise HandlerError("Bad path '{}' for ORIG_HANDLER: {}".format(path, str(e)))

modified_mod_name = modify_module_name(mod_name)
handler_module = import_module(modified_mod_name)
lambda_handler = getattr(handler_module, handler_name)
30 changes: 30 additions & 0 deletions lambda/requirements-nodeps.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
opentelemetry-instrumentation-aiohttp-client==0.41b0
opentelemetry-util-http==0.41b0
asgiref~=3.7
opentelemetry-instrumentation-asgi==0.41b0
opentelemetry-instrumentation-asyncpg==0.41b0
opentelemetry-instrumentation-boto==0.41b0
opentelemetry-instrumentation-botocore==0.41b0
opentelemetry-instrumentation-celery==0.41b0
opentelemetry-instrumentation-dbapi==0.41b0
opentelemetry-instrumentation-django==0.41b0
opentelemetry-instrumentation-elasticsearch==0.41b0
opentelemetry-instrumentation-fastapi==0.41b0
opentelemetry-instrumentation-falcon==0.41b0
opentelemetry-instrumentation-flask==0.41b0
opentelemetry-instrumentation-grpc==0.41b0
opentelemetry-instrumentation-jinja2==0.41b0
opentelemetry-instrumentation-logging==0.41b0
opentelemetry-instrumentation-mysql==0.41b0
opentelemetry-instrumentation-psycopg2==0.41b0
opentelemetry-instrumentation-pymemcache==0.41b0
opentelemetry-instrumentation-pymongo==0.41b0
opentelemetry-instrumentation-pymysql==0.41b0
opentelemetry-instrumentation-pyramid==0.41b0
opentelemetry-instrumentation-redis==0.41b0
opentelemetry-instrumentation-requests==0.41b0
opentelemetry-instrumentation-sqlalchemy==0.41b0
opentelemetry-instrumentation-sqlite3==0.41b0
opentelemetry-instrumentation-starlette==0.41b0
opentelemetry-instrumentation-tornado==0.41b0
opentelemetry-instrumentation-wsgi==0.41b0
3 changes: 3 additions & 0 deletions lambda/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
opentelemetry-exporter-otlp==1.20.0
opentelemetry-exporter-otlp-proto-http==1.20.0
opentelemetry-instrumentation-aws-lambda==0.41b0

0 comments on commit dbbd2d9

Please sign in to comment.