Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
152 lines (134 loc) · 8.05 KB
/
Copy pathDockerfile
File metadata and controls
152 lines (134 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
ARG image
FROM $image as builder
ARG runtime
# Create the directory structure required for AWS Lambda Layer
RUN mkdir -p /build/python/lib/$runtime/site-packages
WORKDIR /build
# AL2 SAM images (Python <= 3.11) ship GCC 7, which is too old to build ddtrace.
# AL2023 SAM images already include GCC, find, and strip. Lambda preview images
# do not, so install that toolchain with dnf when those tools are missing.
RUN set -eux; \
if command -v yum >/dev/null 2>&1; then \
yum -y install git gcc10 gcc10-c++; \
cd /usr/bin; \
rm gcc && ln -s gcc10-gcc gcc; \
rm g++ && ln -s gcc10-g++ g++; \
rm cc && ln -s gcc10-cc cc; \
rm c++ && ln -s gcc10-c++ c++; \
elif ! command -v gcc >/dev/null 2>&1 || ! command -v strip >/dev/null 2>&1 || ! command -v find >/dev/null 2>&1; then \
dnf install -y gcc gcc-c++ make binutils findutils git; \
dnf clean all; \
fi
# Add Rust compiler which is needed to build dd-trace-py from source
RUN curl https://sh.rustup.rs -sSf | \
sh -s -- --default-toolchain stable -y
ENV PATH=/root/.cargo/bin:$PATH
# Install datadog_lambda and dependencies from local
COPY . .
RUN pip install --no-cache-dir . -t ./python/lib/$runtime/site-packages
# Remove botocore (40MB) to reduce package size. aws-xray-sdk
# installs it, while it's already provided by the Lambda Runtime.
RUN rm -rf ./python/lib/$runtime/site-packages/botocore*
RUN rm -rf ./python/lib/$runtime/site-packages/setuptools
RUN rm -rf ./python/lib/$runtime/site-packages/jsonschema/tests
# Remove unsupported appsec modules
RUN rm -rf \
./python/lib/$runtime/site-packages/ddtrace/appsec/_iast \
./python/lib/$runtime/site-packages/ddtrace/appsec/_shared
# Minify the App and API Protection rules (~454 kB pretty-printed, ~278 kB
# minified). ddtrace passes the file's bytes to libddwaf, which parses the JSON,
# so whitespace doesn't matter.
RUN python -c "import json, sys; p = sys.argv[1]; d = json.load(open(p, encoding='utf-8')); \
open(p, 'w', encoding='utf-8').write(json.dumps(d, separators=(',', ':'), ensure_ascii=False))" \
./python/lib/$runtime/site-packages/ddtrace/appsec/rules.json
# CI Visibility paths/integrations
RUN rm -rf \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/coverage/ \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/pytest/ \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/pytest_bdd/ \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/pytest_benchmark/ \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/selenium/ \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/unittest/ \
./python/lib/$runtime/site-packages/ddtrace/ext/ci_visibility \
./python/lib/$runtime/site-packages/ddtrace/ext/test_visibility \
./python/lib/$runtime/site-packages/ddtrace/internal/ci_visibility \
./python/lib/$runtime/site-packages/ddtrace/internal/coverage \
./python/lib/$runtime/site-packages/ddtrace/internal/test_visibility \
./python/lib/$runtime/site-packages/ddtrace/testing/
# Integrations for runtimes that cannot run inside AWS Lambda: other clouds'
# function hosts (Azure Functions), Ray clusters, and the vLLM inference server.
# If one of these packages is imported anyway, ddtrace logs that the integration
# could not be enabled and carries on. Ray's __init__ and constants stay: the
# tracer imports them at startup.
RUN rm -rf \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/azure_functions/ \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/azure_durable_functions/ \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/ray/core/ \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/ray/patch.py \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/ray/span_manager.py \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/ray/ray.manifest.yaml \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/ray_serve/ \
./python/lib/$runtime/site-packages/ddtrace/contrib/_events/ray.py \
./python/lib/$runtime/site-packages/ddtrace/_trace/subscribers/ray.py \
./python/lib/$runtime/site-packages/ddtrace/contrib/internal/vllm/ \
./python/lib/$runtime/site-packages/ddtrace/llmobs/_contrib/vllm/ \
./python/lib/$runtime/site-packages/ddtrace/llmobs/_integrations/vllm.py
# Dogshell
RUN rm -rf ./python/lib/$runtime/site-packages/datadog/dogshell
RUN rm -rf ./python/lib/$runtime/site-packages/bin/dog*
# remove *.dist-info directories except any entry_points.txt files and METADATA files required for Appsec Software Composition Analysis
RUN find ./python/lib/$runtime/site-packages/*.dist-info \
-type f \
! \( -name 'entry_points.txt' -o -name 'METADATA' \) \
-delete
RUN find ./python/lib/$runtime/site-packages -type d -empty -delete
# Remove requests and dependencies
RUN rm -rf \
./python/lib/$runtime/site-packages/requests* \
./python/lib/$runtime/site-packages/urllib3* \
./python/lib/$runtime/site-packages/certifi* \
./python/lib/$runtime/site-packages/idna* \
./python/lib/$runtime/site-packages/charset_normalizer* \
./python/lib/$runtime/site-packages/*__mypyc*.so # from charset_normalizer
# Precompile all .pyc files and remove .py files. This speeds up load time.
# Compile with optimization level 2 (-OO) and PYTHONNODEBUGRANGES=1 to redtce
# size of .pyc files.
# See https://docs.python.org/3/tutorial/modules.html#compiled-python-files
# https://docs.python.org/3.11/using/cmdline.html#cmdoption-O
# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONNODEBUGRANGES
RUN PYTHONNODEBUGRANGES=1 python -OO -m compileall -b ./python/lib/$runtime/site-packages
# remove all .py files
# DEV: ddtrace>=4.7.0rc3 checks for .pyc files in addition to .py files for instrumentation
# discovery (DataDog/dd-trace-py#17196), so we can safely remove all .py files.
# For older versions, we need to keep patch.py files for instrumentation discovery.
# Wheels from dd-trace-py CI built with DD_SERVERLESS_BUILD=1 install as the
# ddtrace_serverless distribution, so read the version from either dist-info.
# Without it the version check fails and every contrib patch.py (~700 kB) stays.
RUN pip install --quiet packaging && \
DDTRACE_VERSION=$(cat ./python/lib/$runtime/site-packages/ddtrace-*.dist-info/METADATA \
./python/lib/$runtime/site-packages/ddtrace_serverless-*.dist-info/METADATA 2>/dev/null \
| grep -m1 "^Version:" | awk '{print $2}') && \
echo "Detected ddtrace version: ${DDTRACE_VERSION:-unknown}" && \
if python -c "from packaging.version import Version; exit(0 if Version('$DDTRACE_VERSION') >= Version('4.7.0rc3') else 1)"; then \
find ./python/lib/$runtime/site-packages -name \*.py | xargs rm -rf; \
else \
find ./python/lib/$runtime/site-packages -name \*.py | grep -v ddtrace/contrib | xargs rm -rf && \
find ./python/lib/$runtime/site-packages/ddtrace/contrib -name \*.py | grep -v patch.py | xargs rm -rf; \
fi
RUN find ./python/lib/$runtime/site-packages -name __pycache__ -type d -exec rm -r {} \+
# Remove C/C++/Cython sources and type stubs. Nothing imports them at runtime.
# ddtrace ships them when built from a branch, and other packages ship them
# in their release wheels (e.g. wrapt/_wrappers.c is ~150 kB, wrapt-stubs ~30 kB).
RUN find ./python/lib/$runtime/site-packages -type f \( \
-name \*.c -o -name \*.cpp -o -name \*.cc -o -name \*.h -o -name \*.hpp \
-o -name \*.pyx -o -name \*.pxd -o -name \*.pyi \
\) -delete
RUN find ./python/lib/$runtime/site-packages -type d -empty -delete
# Strip debug symbols and symbols that are not needed for relocation
# processing using strip --strip-unneeded for all .so files. This is to
# reduce the size when ddtrace is built from sources. The release wheels are
# already stripped of debug symbols. We should revisit this when serverless
# benchmark uses pre-built wheels instead of building from sources.
RUN find ./python/lib/$runtime/site-packages -name "*.so" -exec strip --strip-unneeded {} \;
FROM scratch
COPY --from=builder /build/python /