Skip to content

Commit 96eecdc

Browse files
authored
[beta] Python Profiling support (#244)
* feat: Python Profiler WIP * feat: start on cold start, stop never * breaking: bump dd trace to 1.2.1 * feat: Add major version notes * fix: readme header size * fix: Fix test using old method. Describe change in changes list * fix: use tracer instead of correlation ids * feat: Add the profiler * fix: remove debug * feat: Remove profiling comment line * feat: Go to ddtrace 1.4. Conditionally import profiler. * fix: remove duplicate declaration of profiling_env_var * feat: Black, with the right version * feat: Readme on profiler beta period * fix: Remove dev changes used to package dd-trace locally * feat: Remove == from if True * feat: Bump layer size maximums * feat: Remove python36 integration tests, as we can no longer create stacks with this version of python * feat: Update snapshots * feat: More python 3.6 deprecation * feat: Readme update - dropping python36
1 parent 6809a11 commit 96eecdc

11 files changed

+480
-390
lines changed

Dockerfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,3 @@ RUN find ./python/lib/$runtime/site-packages -name \*.pyc -delete
1717
# Remove botocore (40MB) to reduce package size. aws-xray-sdk
1818
# installs it, while it's already provided by the Lambda Runtime.
1919
RUN rm -rf ./python/lib/$runtime/site-packages/botocore*
20-
21-
# Remove profiling (7MB) to reduce package size.
22-
# Continous profiling is not yet supported anyway.
23-
RUN rm -rf ./python/lib/$runtime/site-packages/ddtrace/profiling

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ When opening an issue, include the Datadog Lambda Library version, Python versio
2424

2525
You can also open an issue for a feature request.
2626

27+
## Lambda Profiling Beta
28+
29+
Datadog's Continuous Profiler is now available in beta for Python. This optional feature is enabled by setting the `DD_PROFILING_ENABLED` environment variable to `True`. During the beta period, profiling is available at no cost.
30+
2731
## Major Version Notes
2832

29-
### 4.x
33+
### 4.x / Layer version 61+
3034

31-
- `dd-trace` upgraded from 0.61 to 1.2, full release notes are available [here](https://ddtrace.readthedocs.io/en/stable/release_notes.html#v1-0-0)
35+
- Python3.6 support has been [deprecated](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html) by AWS, and support removed from this library.
36+
- `dd-trace` upgraded from 0.61 to 1.4, full release notes are available [here](https://ddtrace.readthedocs.io/en/stable/release_notes.html#v1-0-0)
3237
- `get_correlation_ids()` has been changed to `get_log_correlation_context()`, which now returns a dictionary containing the active `span_id`, `trace_id`, as well as `service` and `env`.
3338

3439
## Contributing

datadog_lambda/wrapper.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@
3636
from datadog_lambda.trigger import extract_trigger_tags, extract_http_status_code_tag
3737
from datadog_lambda.tag_object import tag_object
3838

39+
profiling_env_var = os.environ.get("DD_PROFILING_ENABLED", "false").lower() == "true"
40+
if profiling_env_var:
41+
from ddtrace.profiling import profiler
42+
3943
logger = logging.getLogger(__name__)
4044

4145
dd_capture_lambda_payload_enabled = (
4246
os.environ.get("DD_CAPTURE_LAMBDA_PAYLOAD", "false").lower() == "true"
4347
)
48+
service_env_var = os.environ.get("DD_SERVICE", "DefaultServiceName")
49+
env_env_var = os.environ.get("DD_ENV", None)
4450

4551
"""
4652
Usage:
@@ -111,7 +117,8 @@ def __init__(self, func):
111117
os.environ.get("DD_TRACE_MANAGED_SERVICES", "true").lower() == "true"
112118
)
113119
self.response = None
114-
120+
if profiling_env_var:
121+
self.prof = profiler.Profiler(env=env_env_var, service=service_env_var)
115122
if self.extractor_env:
116123
extractor_parts = self.extractor_env.rsplit(".", 1)
117124
if len(extractor_parts) == 2:
@@ -181,7 +188,8 @@ def _before(self, event, context):
181188
)
182189
else:
183190
set_correlation_ids()
184-
191+
if profiling_env_var and is_cold_start():
192+
self.prof.start(stop_on_exit=False, profile_children=True)
185193
logger.debug("datadog_lambda_wrapper _before() done")
186194
except Exception:
187195
traceback.print_exc()

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/check_layer_size.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
# Compares layer size to threshold, and fails if below that threshold
99

10-
# 5 mb size limit
11-
MAX_LAYER_COMPRESSED_SIZE_KB=$(expr 5 \* 1024)
12-
MAX_LAYER_UNCOMPRESSED_SIZE_KB=$(expr 16 \* 1024)
10+
# 6 mb size limit
11+
MAX_LAYER_COMPRESSED_SIZE_KB=$(expr 6 \* 1024)
12+
MAX_LAYER_UNCOMPRESSED_SIZE_KB=$(expr 24 \* 1024)
1313

1414

1515
LAYER_FILES_PREFIX="datadog_lambda_py"

tests/integration/snapshots/logs/async-metrics_python37.log

Lines changed: 54 additions & 45 deletions
Large diffs are not rendered by default.

tests/integration/snapshots/logs/async-metrics_python38.log

Lines changed: 54 additions & 45 deletions
Large diffs are not rendered by default.

tests/integration/snapshots/logs/async-metrics_python39.log

Lines changed: 54 additions & 45 deletions
Large diffs are not rendered by default.

tests/integration/snapshots/logs/sync-metrics_python37.log

Lines changed: 99 additions & 81 deletions
Large diffs are not rendered by default.

tests/integration/snapshots/logs/sync-metrics_python38.log

Lines changed: 99 additions & 81 deletions
Large diffs are not rendered by default.

tests/integration/snapshots/logs/sync-metrics_python39.log

Lines changed: 99 additions & 81 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)