Skip to content

Commit 3b9cbac

Browse files
committed
Use vendored version of requests package in layer.
1 parent ed8c462 commit 3b9cbac

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ RUN rm ./python/lib/$runtime/site-packages/ddtrace/internal/datadog/profiling/dd
3030
RUN rm ./python/lib/$runtime/site-packages/ddtrace/internal/datadog/profiling/stack_v2/_stack_v2.*.so
3131
RUN find . -name "*.dist-info" -type d | xargs rm -rf
3232

33+
# The requests package is available in the lambda runtime already as
34+
# pip._vendor.requests. Remove the requests package to reduce package size.
35+
# Before importing requests, `pip/_vendor` must be added to `sys.path`.
36+
RUN rm -rf
37+
./python/lib/$runtime/site-packages/requests* \
38+
./python/lib/$runtime/site-packages/certifi* \
39+
./python/lib/$runtime/site-packages/idna* \
40+
./python/lib/$runtime/site-packages/charset_normalizer*
41+
3342
# Precompile all .pyc files and remove .py files. This speeds up load time.
3443
# Compile with optimization level 2 (-OO) and PYTHONNODEBUGRANGES=1 to redtce
3544
# size of .pyc files.

datadog_lambda/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
import sys
2+
3+
4+
# The requests package is no longer included in the lambda layer, instead use
5+
# the vendored version of requests included in the pip package. Appending to
6+
# sys.path will make the vendered requests package discoverable.
7+
for i in range(len(sys.path)):
8+
path = sys.path[i]
9+
sys.path.append(f"{path}/pip/_vendor")
10+
111
from datadog_lambda.cold_start import initialize_cold_start_tracing
212
import os
313

tests/integration/handle.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import requests
2+
import time
23

34
from datadog_lambda.metric import lambda_metric
45
from datadog_lambda.wrapper import datadog_lambda_wrapper
@@ -21,6 +22,8 @@ def handle(event, context):
2122

2223
# Generate custom metrics
2324
lambda_metric("hello.dog", 1, tags=["team:serverless", "role:hello"])
25+
lambda_metric("hello.cat", 1, tags=["team:serverless", "role:hello"],
26+
timestamp=time.time() - 60)
2427
lambda_metric(
2528
"tests.integration.count", 21, tags=["test:integration", "role:hello"]
2629
)

0 commit comments

Comments
 (0)